aishoo1612/VADER-With-heatmaps
pip install vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer analyser = SentimentIntensityAnalyzer()
analyser.polarity_scores("I hate watching movies")
import nltk from nltk.tokenize import word_tokenize, RegexpTokenizer from nltk.sentiment.vader import SentimentIntensityAnalyzer
nltk.download('all')
import numpy as np
sentence = """I love dancing & painting""" tokenizedsentence = nltk.wordtokenize(sentence)
from nltk import word_tokenize from typing import List
Analyzer = SentimentIntensityAnalyzer()
poswordlist=[] neuwordlist=[] negwordlist=[] posscorelist=[] negscorelist=[] scorelist=[] for word in tokenizedsentence: if (Analyzer.polarityscores(word)['compound']) >= 0.1: poswordlist.append(word) scorelist.append(Analyzer.polarityscores(word)['compound']) elif (Analyzer.polarityscores(word)['compound']) <= -0.1: negwordlist.append(word) scorelist.append(Analyzer.polarityscores(word)['compound']) else: neuwordlist.append(word) scorelist.append(Analyzer.polarityscores(word)['compound'])
print('Positive:',poswordlist) print('Neutral:',neuwordlist) print('Negative:',negwordlist) print('Score:', scorelist) score = Analyzer.polarityscores(sentence) print('\nScores:', score)
predictlog=score.values() valueiterator=iter(predictlog) negprediction=next(valueiterator) neuprediction=next(valueiterator) posprediction=next(value_iterator)
predictionlist=[negprediction, posprediction] predictionlistarray=np.array(predictionlist)
def predict(): probs = [] for text in texts: offset = (self.score(text) + 1) / 2. binned = np.digitize(5 * offset, self.classes) + 1 simulatedprobs = scipy.stats.norm.pdf(self.classes, binned, scale=0.5) probs.append(simulatedprobs) return np.array(probs)
latexspecialtoken = ["!@#$%^&*()"]
import operator
def generate(textlist, attentionlist, latexfile, colorneg='red', colorpos='green', rescalevalue = False): print("hello") attentionlist = rescale(attentionlist) wordnum = len(textlist) print(len(attentionlist)) print(len(textlist))
textlist = cleanword(textlist) with open(latexfile,'w') as f: f.write(r'''\documentclass[varwidth]{standalone} \special{papersize=210mm,297mm} \usepackage{color} \usepackage{tcolorbox} \usepackage{CJK} \usepackage{adjustbox} \tcbset{width=0.9\textwidth,boxrule=0pt,colback=red,arc=0pt,auto outer arc,left=0pt,right=0pt,boxsep=5pt} \begin{document} \begin{CJK*}{UTF8}{gbsn}'''+'\n') string = r'''{\setlength{\fboxsep}{0pt}\colorbox{white!0}{\parbox{0.9\textwidth}{'''+"\n" for idx in range(len(attentionlist)): if attentionlist[idx] > 0: string += "\\colorbox{%s!%s}{"%(colorpos, attentionlist[idx])+"\\strut " + textlist[idx]+"} " else: string += "\\colorbox{%s!%s}{"%(colorneg, -attentionlist[idx])+"\\strut " + textlist[idx]+"} "
string += "\n}}}" f.write(string+'\n') f.write(r'''\end{CJK*} \end{document}''')
def rescale(input_list):
thearray = np.asarray(inputlist) themax = np.max(abs(thearray)) rescale = thearray/themax rescale = rescale*100 rescale = np.round(rescale, 3)
''' thearray = np.asarray(inputlist) themax = np.max(thearray) themin = np.min(thearray) rescale = ((thearray - themin)/(themax-themin))*100 for i in rescale: print(rescale) '''
return rescale.tolist()
def cleanword(wordlist): newwordlist = [] for word in wordlist: for latexsensitive in ["\\", "%", "&", "^", "#", "", "{", "}"]: if latexsensitive in word: word = word.replace(latexsensitive, '\\'+latexsensitive) newwordlist.append(word) return newwordlist
if _name == 'main': color1 = 'red' color2 = 'green' words = wordtokenize(sentence) wordnum = len(words) generate(words, scorelist, "sple.tex", color1, color2)
