forked from nisarg64/Sentiment-Analysis-ANEW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anew.py
186 lines (128 loc) · 3.89 KB
/
anew.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
#!/usr/bin/python
#- ANEW.PY -----------------------------------------------------------------#
# Routines to calulate average valence and arousal for one or more terms #
# using the ANEW and Happiness sentiment dictionaries #
# #
#- Modification History: ---------------------------------------------------#
# When: Who: Comments: #
# #
# 28-Sep-14 Christopher G. Healey Converted from Javascript #
#---------------------------------------------------------------------------#
import math
# Import raw ANEW and Happiness dictionary data from term file
from anew_term import anew_word as anew_word
from anew_term import anew_stem as anew_stem
from anew_term import hapi_word as hapi_word
def arousal( term ):
# Return the average arousal for a term
#
# term: Term to check (can be string or list of strings)
if isinstance( term, str ):
return arousal_raw( term )[ 0 ]
elif not isinstance( term, list ):
return 0.0
# At this point we know we're working with a list of terms
c = 2.0 * math.pi
prob = [ ]
prob_sum = 0.0
a_mu = [ ]
for t in term:
if exist( t ):
a = arousal_raw( t )
p = 1.0 / math.sqrt( c * math.pow( a[ 1 ], 2.0 ) )
prob.append( p )
prob_sum = prob_sum + p
a_mu.append( a[ 0 ] )
arousal = 0.0
for i in range( 0, len( a_mu ) ):
arousal = arousal + ( prob[ i ] / prob_sum * a_mu[ i ] )
return arousal
# End function arousal
def arousal_raw( term ):
# Return the raw arousal for a single term
#
# term: Term to check
if not exist( term ):
avg = 0.0
std = 0.0
elif term in anew_word:
avg = anew_word[ term ][ 'avg' ][ 1 ]
std = anew_word[ term ][ 'std' ][ 1 ]
elif term in anew_stem:
avg = anew_stem[ term ][ 'avg' ][ 1 ]
std = anew_stem[ term ][ 'std' ][ 1 ]
else:
avg = hapi_word[ term ][ 'avg' ][ 1 ]
std = hapi_word[ term ][ 'std' ][ 1 ]
return [ avg, std ]
# End function arousal_raw
def exist( term ):
# Return True if a term exists in one of the sentiment dictionaries,
# False otherwise
#
# term: Term to check (can be string or list of strings)
if isinstance( term, str ):
ex = term in anew_word or term in anew_stem or term in hapi_word
return ex
elif isinstance( term, list ):
term_list = [ ]
for t in term:
ex = t in anew_word or t in anew_stem or t in hapi_word
term_list.append( ex )
return term_list
else:
return False
# End function exist
def sentiment( term ):
# Return the valence and arousal sentiment for a term
#
# term: Term to check (can be string or list of strings)
sen = { 'valence': 0.0, 'arousal': 0.0 }
if isinstance( term, str ) or isinstance( term, list ):
sen[ 'valence' ] = valence( term )
sen[ 'arousal' ] = arousal( term )
return sen
# End function sentiment
def valence( term ):
# Return the average valence for a term
#
# term: Term to check (can be string or list of strings)
if isinstance( term, str ):
return valence_raw( term )[ 0 ]
elif not isinstance( term, list ):
return 0.0
# At this point we know we're working with a list of terms
c = 2.0 * math.pi
prob = [ ]
prob_sum = 0.0
v_mu = [ ]
for t in term:
if exist( t ):
v = valence_raw( t )
p = 1.0 / math.sqrt( c * math.pow( v[ 1 ], 2.0 ) )
prob.append( p )
prob_sum = prob_sum + p
v_mu.append( v[ 0 ] )
val = 0.0
for i in range( 0, len( v_mu ) ):
val = val + ( prob[ i ] / prob_sum * v_mu[ i ] )
return val
# End function valence
def valence_raw( term ):
# Return the raw valence for a single term
#
# term: Term to check
if not exist( term ):
avg = 0.0
std = 0.0
elif term in anew_word:
avg = anew_word[ term ][ 'avg' ][ 0 ]
std = anew_word[ term ][ 'std' ][ 0 ]
elif term in anew_stem:
avg = anew_stem[ term ][ 'avg' ][ 0 ]
std = anew_stem[ term ][ 'std' ][ 0 ]
else:
avg = hapi_word[ term ][ 'avg' ][ 0 ]
std = hapi_word[ term ][ 'std' ][ 0 ]
return [ avg, std ]
# End function valence_raw