-
Notifications
You must be signed in to change notification settings - Fork 0
/
polarity.py
271 lines (237 loc) · 11 KB
/
polarity.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
####################################################################
# Licence: Creative Commons (see COPYRIGHT) #
# Authors: Nikolaos Pappas, Georgios Katsimpras #
# {nik0spapp, gkatsimpras}@gmail.com #
# Supervisor: Efstathios stamatatos #
# stamatatos@aegean.gr #
# University of the Aegean #
# Department of Information and Communication Systems Engineering #
# Information Management Track (MSc) #
# Karlovasi, Samos #
# Greece #
####################################################################
from __future__ import division
import nltk
import string
import sys
from terminal_colors import Tcolors
from stemming.porter2 import stem
sys.path.append(sys.path[0] + "/../")
class PolarityClassifier:
"""
PolarityClassifier: Rule-based polarity classification of sentences
according to the following paper:
T. Wilson, J. Wiebe, and P. Hoffmann. Recognizing contextual polarity
in phrase-level sentiment analysis. In Proceedings of the conference
on Human Language Technology and Empirical Methods in Natural Language
Processing, HLT '05, pages 347--354, 2005.
Enhancements: We have incorporated emoticons and slung dictionary
apart from the MPQA lexicon that is used in the paper.
"""
def __init__(self, tagger, lexicon, debug=False):
self.lexicon = lexicon.words
self.sentence = None
self.words = []
self.feature_words = {}
self.polar_expressions = []
self.polar_with_tags = {}
self.polar_with_score = {}
self.strong_polar_expressions = []
self.negation_words = ["not", "no", "but"]
self.tagger = tagger
self.words_pos_tags = []
self.stokens = []
self.emotions_score = []
self.emoticons = []
self.debug = debug
def apply_emotions(self):
"""
Compute emotion scores based on emoticon list.
"""
score = 0
for token in self.stokens:
if self.lexicon.has_key(token) \
and self.lexicon[token].has_key('emoticon'):
if self.lexicon[token]['priorpolarity'] == "negative":
score = -2
else:
score = 2
self.emoticons.append(token)
self.emotions_score.append(score)
def apply_weights(self):
"""
Adjust (2*n times) scores for polar expressions.
"""
# Strong emotion heuristic based on punctuation
strong_emotion = 1
if self.sentence.endswith("!"):
strong_emotion = 1.5
elif self.sentence.endswith("?"):
strong_emotion = 0.5
for i,polar in enumerate(self.polar_expressions):
#first rule: if strong double score
if polar in self.strong_polar_expressions:
self.polar_with_score[polar] *= 2
#second rule: if intensified polar double score
if self.intensified_polar(polar):
self.polar_with_score[polar] *= 2
#third rule: if polar expr is adjective double score
if self.polar_with_tags[polar] == "adj":
self.polar_with_score[polar] *= 2
if i + 1 == len(self.polar_expressions):
self.polar_with_score[polar] *= strong_emotion
def check_precedings(self, polar, words):
"""
Find the indexes of polar words and 5 preceding of it.
"""
if words.index(polar) >= 6:
return True
else:
return False
def classify(self, sentence):
"""
Sum up the contextual scores of polar expressions and classify the sentence.
"""
self.sentence = sentence
# Extracting Features from sentence
self.extract_features()
# Performing Word Sense Disambiguation
# Extracting Polar Expressions
self.word_sense_disambiguation()
# Checking for negation words
self.negation_modeling()
# Adjusting weights to Polar Expressions
self.apply_weights()
self.apply_emotions()
# Performing polarity classification
[prediction, score] = self.predict_class()
if self.debug:
print "\n[*] --------------------RESULTS----------------------"
print Tcolors.ADD + " FEATURE WORDS:", self.feature_words
print Tcolors.ADD + " POLAR EXPRESSIONS FOUND:", self.polar_expressions
print Tcolors.ADD + " POLAR WEIGHTS:", self.polar_with_score
print Tcolors.ADD + " EMOTICONS:", self.emoticons
print Tcolors.ADD + " EMOTION WEIGHTS:", self.emotions_score
print Tcolors.ADD + " PREDICTION: ", prediction, ", WITH CONFIDENCE: ", score
print Tcolors.ADD + " NORMALIZED CONFIDENCE: ", score/len(self.words)
self.words = [w for w in self.words if w != '']
return prediction, score, score/len(self.words) #normalizedScore
def extract_features(self):
"""
Match positive and negative words of a sentence with a score of +1 or -1
respectively, if found in the lexicon.
"""
self.words = self.tokenize_words(self.sentence)
words = self.words
for word in words:
word = word.lower()
if word in self.lexicon:
if self.lexicon[word]['priorpolarity'] == "positive":
if not self.feature_words.has_key(word):
self.feature_words[word] = 1
else:
self.feature_words[word] += 1
elif self.lexicon[word]['priorpolarity'] == "negative":
if not self.feature_words.has_key(word):
self.feature_words[word] = -1
else:
self.feature_words[word] -= 1
else:
self.feature_words[word] = 0
def intensified_polar(self, polar):
if self.words.index(polar) > 0:
previous_word = self.words[self.words.index(polar) - 1]
if self.lexicon.has_key(previous_word) and self.lexicon[previous_word]["type"]=="strongsubj" \
and 'adj' in self.lexicon[previous_word]["pos1"]:
return True
return False
def match_tags(self, pos_tag):
if pos_tag:
if pos_tag.startswith("VB"):
return "verb"
elif pos_tag.startswith("JJ"):
return "adj"
elif pos_tag.startswith("NN"):
return "noun"
elif pos_tag.startswith("RB"):
return "adverb"
else:
return 'anypos'
else:
return "anypos"
def negation_modeling(self):
"""
Examine negation words and reassign polarity.
"""
for polar in self.polar_expressions:
has_precedings = self.check_precedings(polar, self.words)
for neg in self.negation_words:
if has_precedings:
if (neg in self.words[(self.words.index(polar)-5):self.words.index(polar)])\
or self.polarity_shifting(polar, self.words[(self.words.index(polar)-5):self.words.index(polar)]):
self.polar_with_score[polar] = self.polar_with_score[polar]*(-1)
break
else:
if neg in self.words[0:self.words.index(polar)] or \
self.polarity_shifting(polar, self.words[0:self.words.index(polar)]):
self.polar_with_score[polar] = self.polar_with_score[polar]*(-1)
def predict_class(self):
summary = 0
summary = sum([value for value in self.polar_with_score.values()])
summary += sum(self.emotions_score)
if summary > 0:
return "positive", summary
elif summary < 0:
return "negative", summary
else:
return "neutral", summary
def polarity_shifting(self, polar, words):
for word in words:
if (self.lexicon.has_key(word) and self.lexicon[word]["type"]=="strongsubj" \
and self.lexicon[word]["priorpolarity"] != self.lexicon[polar]["priorpolarity"] \
and self.lexicon[word]["priorpolarity"]!="neutral") or \
word.endswith("n't"):
# reverse polarity of polar
if self.debug:
print "[!] NEGATION WORD FOUND: ",word
return True
return False
def tokenize_words(self, sentence):
words = nltk.word_tokenize(sentence.lower())
self.stokens = sentence.split()
for i,word in enumerate(words):
if word[len(word)-1] in string.punctuation:
words[words.index(word)] = word[0:(len(word)-1)]
self.words_pos_tags = self.tagger.tag(sentence)
for (word, TAG) in self.words_pos_tags:
word = word.lower()
# Handle punctuation in word_tokenization
if TAG and TAG.startswith("VB"):
if word in words:
words[words.index(word)] = stem(word)
return words
def word_sense_disambiguation(self):
"""
Disambiguate words in a sentence if only if the POS tag of the word matches
the POS tag in the lexicon
"""
for (word, TAG) in self.words_pos_tags:
# Handle punctuation in word_tokenization
if word[len(word)-1] in string.punctuation:
word = word[0:(len(word)-1)]
matched_tag = self.match_tags(TAG)
word = word.lower()
words = [word, stem(word)]
for w in words:
if self.feature_words.has_key(w) and (matched_tag in self.lexicon[w]["pos1"] \
or self.lexicon[w]["pos1"][0] == "anypos" \
or matched_tag == "anypos"):
self.polar_expressions.append(w)
self.polar_with_score[w] = self.feature_words[w]
self.polar_with_tags[w] = matched_tag
if self.lexicon[w]['type'] == "strongsubj":
self.strong_polar_expressions.append(w)
self.polar_expressions = list(set(self.polar_expressions))
if __name__ == '__main__':
polarity = PolarityClassifier(sys.argv[1])
polarity.classify()