aishoo1612 commited on
Commit
05012d9
1 Parent(s): 8544366

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +139 -0
README.md CHANGED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip install vaderSentiment
2
+
3
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
4
+ analyser = SentimentIntensityAnalyzer()
5
+
6
+ analyser.polarity_scores("I hate watching movies")
7
+
8
+ import nltk
9
+ from nltk.tokenize import word_tokenize, RegexpTokenizer
10
+ from nltk.sentiment.vader import SentimentIntensityAnalyzer
11
+
12
+ nltk.download('all')
13
+
14
+ import numpy as np
15
+
16
+ sentence = """I love dancing & painting"""
17
+ tokenized_sentence = nltk.word_tokenize(sentence)
18
+
19
+ from nltk import word_tokenize
20
+ from typing import List
21
+
22
+ Analyzer = SentimentIntensityAnalyzer()
23
+
24
+ pos_word_list=[]
25
+ neu_word_list=[]
26
+ neg_word_list=[]
27
+ pos_score_list=[]
28
+ neg_score_list=[]
29
+ score_list=[]
30
+ for word in tokenized_sentence:
31
+ if (Analyzer.polarity_scores(word)['compound']) >= 0.1:
32
+ pos_word_list.append(word)
33
+ score_list.append(Analyzer.polarity_scores(word)['compound'])
34
+ elif (Analyzer.polarity_scores(word)['compound']) <= -0.1:
35
+ neg_word_list.append(word)
36
+ score_list.append(Analyzer.polarity_scores(word)['compound'])
37
+ else:
38
+ neu_word_list.append(word)
39
+ score_list.append(Analyzer.polarity_scores(word)['compound'])
40
+
41
+ print('Positive:',pos_word_list)
42
+ print('Neutral:',neu_word_list)
43
+ print('Negative:',neg_word_list)
44
+ print('Score:', score_list)
45
+ score = Analyzer.polarity_scores(sentence)
46
+ print('\nScores:', score)
47
+
48
+ predict_log=score.values()
49
+ value_iterator=iter(predict_log)
50
+ neg_prediction=next(value_iterator)
51
+ neu_prediction=next(value_iterator)
52
+ pos_prediction=next(value_iterator)
53
+
54
+ prediction_list=[neg_prediction, pos_prediction]
55
+ prediction_list_array=np.array(prediction_list)
56
+
57
+
58
+ def predict():
59
+ probs = []
60
+ for text in texts:
61
+ offset = (self.score(text) + 1) / 2.
62
+ binned = np.digitize(5 * offset, self.classes) + 1
63
+ simulated_probs = scipy.stats.norm.pdf(self.classes, binned, scale=0.5)
64
+ probs.append(simulated_probs)
65
+ return np.array(probs)
66
+
67
+ latex_special_token = ["!@#$%^&*()"]
68
+
69
+ import operator
70
+
71
+ def generate(text_list, attention_list, latex_file, color_neg='red', color_pos='green', rescale_value = False):
72
+ print("hello")
73
+ attention_list = rescale(attention_list)
74
+ word_num = len(text_list)
75
+ print(len(attention_list))
76
+ print(len(text_list))
77
+
78
+
79
+ text_list = clean_word(text_list)
80
+ with open(latex_file,'w') as f:
81
+ f.write(r'''\documentclass[varwidth]{standalone}
82
+ \special{papersize=210mm,297mm}
83
+ \usepackage{color}
84
+ \usepackage{tcolorbox}
85
+ \usepackage{CJK}
86
+ \usepackage{adjustbox}
87
+ \tcbset{width=0.9\textwidth,boxrule=0pt,colback=red,arc=0pt,auto outer arc,left=0pt,right=0pt,boxsep=5pt}
88
+ \begin{document}
89
+ \begin{CJK*}{UTF8}{gbsn}'''+'\n')
90
+ string = r'''{\setlength{\fboxsep}{0pt}\colorbox{white!0}{\parbox{0.9\textwidth}{'''+"\n"
91
+ for idx in range(len(attention_list)):
92
+ if attention_list[idx] > 0:
93
+ string += "\\colorbox{%s!%s}{"%(color_pos, attention_list[idx])+"\\strut " + text_list[idx]+"} "
94
+ else:
95
+ string += "\\colorbox{%s!%s}{"%(color_neg, -attention_list[idx])+"\\strut " + text_list[idx]+"} "
96
+
97
+ string += "\n}}}"
98
+ f.write(string+'\n')
99
+ f.write(r'''\end{CJK*}
100
+ \end{document}''')
101
+
102
+
103
+
104
+ def rescale(input_list):
105
+
106
+ the_array = np.asarray(input_list)
107
+ the_max = np.max(abs(the_array))
108
+ rescale = the_array/the_max
109
+ rescale = rescale*100
110
+ rescale = np.round(rescale, 3)
111
+
112
+ '''
113
+ the_array = np.asarray(input_list)
114
+ the_max = np.max(the_array)
115
+ the_min = np.min(the_array)
116
+ rescale = ((the_array - the_min)/(the_max-the_min))*100
117
+ for i in rescale:
118
+ print(rescale)
119
+ '''
120
+
121
+ return rescale.tolist()
122
+
123
+ def clean_word(word_list):
124
+ new_word_list = []
125
+ for word in word_list:
126
+ for latex_sensitive in ["\\", "%", "&", "^", "#", "_", "{", "}"]:
127
+ if latex_sensitive in word:
128
+ word = word.replace(latex_sensitive, '\\'+latex_sensitive)
129
+ new_word_list.append(word)
130
+ return new_word_list
131
+
132
+ if __name__ == '__main__':
133
+ color_1 = 'red'
134
+ color_2 = 'green'
135
+ words = word_tokenize(sentence)
136
+ word_num = len(words)
137
+ generate(words, score_list, "sple.tex", color_1, color_2)
138
+
139
+