-
Notifications
You must be signed in to change notification settings - Fork 5
/
emotion_analysis_comments.py
224 lines (139 loc) · 6.14 KB
/
emotion_analysis_comments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#Sentiment and Emotion Analysis on Youtube Transcripts based on NRC lexicon
import sys
import nltk
from youtube_transcript_api import YouTubeTranscriptApi
from nltk.stem.snowball import SnowballStemmer
from nltk.stem import WordNetLemmatizer
from collections import defaultdict
import json
import ast
#from scipy.special import softmax
#from sklearn.utils.extmath import softmax
import numpy as np
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()
def get_transcript():
#s1="2DG3pMcNNlw"
#id="stXgn2iZAAY"
#s2="LfKLV6rmLxE"
try:
#output= YouTubeTranscriptApi.get_transcript(id)
#print(output)
f=open("islam.txt")
g= [line.strip("\n") for line in f ]
all_text= []
#list of sentences
for l in g:
k=ast.literal_eval(l)
all_text.append(k["text"])
comments=" ".join(all_text)
list_comments= comments.split( ). #list of words
print(list_comments)
return(list_comments)
except:
print("Error occurred")
return(None)
def parse_nrc():
word_emotion_dict={}
f=open("NRC-Emotion-Lexicon-Senselevel-v0.92.txt")
list_lines= f.readlines()
word_emotion_dict={}
for line in list_lines:
splitted= line.split("--")
word=splitted[0]
word_emotion_dict[word]={}
for line in list_lines:
splitted= line.split("--")
word=splitted[0]
splitted2= splitted[1].split("\t")
emot_score=splitted2[-1].strip("\n")
emot=splitted2[-2]
word_emotion_dict[word][emot]= emot_score
#print(splitted2)
#print(emot)
#print(emot_score)
return(word_emotion_dict)
def get_transcript_clean_and_organise():
s=get_transcript()
s_lowercase=[]
#s = "".join(" " if x in string.punctuation else x for x in s.lower() )
#print(s)
for wrd in s:
s_lowercase.append(wrd.lower() )
#print(s_lowercase)
#print(nltk.pos_tag(nltk.word_tokenize(s)))
return(s_lowercase)
#return s.split()
def get_emotion_counts():
num_words_hit=0
#emot is a dictionaries where the keys are the words and the value is a dictionary with key asd emotions and values a 1/0
emot= parse_nrc()
s= get_transcript_clean_and_organise()
stemmer = SnowballStemmer("english")
lemmatizer = WordNetLemmatizer()
total_count_dict=defaultdict(int)
#initialises values with integer 0
#print(nltk.pos_tag(nltk.word_tokenize(s)))
for wrd in s:
if wrd in emot.keys():
total_count_dict["positive"]=total_count_dict["positive"] + int( emot[wrd]["positive"] )
total_count_dict["negative"]=total_count_dict["negative"] + int( emot[wrd]["negative"] )
total_count_dict["fear"]= total_count_dict["fear"]+ int ( emot[wrd]["fear"] )
total_count_dict["anger"]= total_count_dict["anger"] + int( emot[wrd]["anger"] )
total_count_dict["surprise"]= total_count_dict["surprise"] + int( emot[wrd]["surprise"] )
total_count_dict["sadness"]= total_count_dict["sadness"] + int( emot[wrd]["sadness"] )
total_count_dict["disgust"]= total_count_dict["disgust"] + int( emot[wrd]["disgust"] )
total_count_dict["trust"]= total_count_dict["trust"] + int( emot[wrd]["trust"] )
total_count_dict["anticip"]= total_count_dict["anticip"] + int( emot[wrd]["anticip"] )
num_words_hit=num_words_hit+1
else:
stemmed_word=stemmer.stem(wrd)
#lemmatized_wrd= lemmatizer.lemmatize("better", pos="a")
if(stemmed_word in emot.keys()):
total_count_dict["positive"]=total_count_dict["positive"] + int( emot[stemmed_word]["positive"] )
total_count_dict["negative"]=total_count_dict["negative"] + int( emot[stemmed_word]["negative"] )
total_count_dict["fear"]= total_count_dict["fear"]+ int ( emot[stemmed_word]["fear"] )
total_count_dict["anger"]= total_count_dict["anger"] + int( emot[stemmed_word]["anger"] )
total_count_dict["trust"]= total_count_dict["trust"] + int ( emot[stemmed_word]["trust"] )
total_count_dict["surprise"]= total_count_dict["surprise"] + int( emot[stemmed_word]["surprise"] )
total_count_dict["sadness"]= total_count_dict["sadness"] + int( emot[stemmed_word]["sadness"] )
total_count_dict["disgust"]= total_count_dict["disgust"] + int( emot[stemmed_word]["disgust"] )
total_count_dict["joy"]= total_count_dict["joy"] + int( emot[stemmed_word]["joy"] )
total_count_dict["anticip"]= total_count_dict["anticip"] + int( emot[stemmed_word]["anticip"] )
num_words_hit=num_words_hit+1
return(total_count_dict, num_words_hit)
def softmaxed_normalized_emotion_counts(id):
normalized_emotion_counts={}
softmax_counts={}
total_count_dict, num_words_hit= get_emotion_counts()
normalized_emotion_counts["positive"]= total_count_dict["positive"]/ num_words_hit
normalized_emotion_counts["negative"]= total_count_dict["negative"]/ num_words_hit
normalized_emotion_counts["fear"]= total_count_dict["fear"]/ num_words_hit
normalized_emotion_counts["anger"]= total_count_dict["anger"]/ num_words_hit
normalized_emotion_counts["surprise"]= total_count_dict["surprise"]/ num_words_hit
normalized_emotion_counts["sadness"]= total_count_dict["sadness"]/ num_words_hit
normalized_emotion_counts["disgust"]= total_count_dict["disgust"]/ num_words_hit
normalized_emotion_counts["joy"]= total_count_dict["joy"]/ num_words_hit
normalized_emotion_counts["anticip"]= total_count_dict["anticip"]/ num_words_hit
normalized_emotion_counts["trust"]= total_count_dict["trust"]/ num_words_hit
value_list=[]
for value in normalized_emotion_counts.values():
value_list.append(value)
softmax_value_list= softmax(value_list)
#print(sum(softmax_value_list))
i=0
for key in normalized_emotion_counts.keys():
softmax_counts[key]= softmax_value_list[i]
i=i+1
#print(normalized_emotion_counts)
return(normalized_emotion_counts, softmax_counts)
if __name__=="__main__":
#id=sys.argv[1]
#print(get_transcript())
normalized_counts, softmaxed_counts= softmaxed_normalized_emotion_counts(id)
print("Normalized emotion counts: \n")
print(normalized_counts)
print("\n Softmaxed emotion counts: \n")
print(softmaxed_counts)