-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentimentAnalysis.py
315 lines (265 loc) · 9.25 KB
/
sentimentAnalysis.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import re
import nltk
import pprint
import numpy as np
np.seterr(divide='ignore', invalid='ignore')
nltk.download('averaged_perceptron_tagger')
#consolidated regex expressions into extracting text beginning, between, or ending with ' quote
pattern = re.compile(r"(?<![a-zA-Z])'(\D*?)'(?![a-zA-Z])")
#Input: file location
#Output: document is converted to tuples of (sentence, 0 or 1) 0 for negative and 1 for positive sentiment
def load_corpus(corpus_path):
file = open(corpus_path, "r")
tuples = []
for line in file:
values = line.split('\t')
snippet = values[0]
num = values[1].strip('\n')
tup = (snippet, num)
tuples.append(tup)
return tuples
#Input: (sentence, value) tuples
#Output: Tokenized words and wrapped "words" that match the specific regex pattern
def tokenize(snippet):
tokens = []
quotes = []
#findall matches for the regular expression
match = re.findall(pattern, snippet)
if(match != None):
quotes.append(match)
#grab all initial tokens
for word in snippet.split():
tokens.append(word)
#if there are quotes in the snippet, add surrounding quotes ""
if(len(quotes[0])>0):
#grab all quotes
quoteList = quotes[0]
#if theres more than one quote
if(len(quoteList) > 1):
#handle all quotes
newSnip = ' '.join(word for word in tokens)
for quote in quoteList:
newSnip = re.sub("'" + quote + "'", " ' " + quote + " ' ", newSnip)
newSnip = re.sub("'" + quote, " ' " + quote, newSnip)
#new token list
tokens = []
for word in newSnip.split():
tokens.append(word)
#if theres only one quote
else:
#if theres more than one word in the quote
first = " ' " + quoteList[0].strip("'")
#if theres only one word in the quote
firstboth = " ' " + quoteList[0].strip("'") + " ' "
#handle each case
for i, w in enumerate(tokens):
if (w == ("'" + quoteList[0].split()[0])):
tokens.remove(w)
tokens.insert(i, first)
if (w == ("'" + quoteList[0].split()[0] + "'")):
tokens.remove(w)
tokens.insert(i, firstboth)
#get the string of all tokens
rv = ' '.join(word for word in tokens)
#return the split of the string
return rv.split()
#Test cases
print(tokenize('hello \'world'))
print(tokenize('hello world\''))
print(tokenize('don\'t hello'))
print(tokenize('\'hello\' world'))
print(tokenize('\'em world'))
#Input: tokenized words
#Output: for tokens in quotes, we label 'EDIT_' before each token. Return updated tokens
def tag_edits(tokenized_snippet):
rvTokens = []
#index of [
#initialized at -1
starti = -1
for i,token in enumerate(tokenized_snippet):
#if found [ then update index
if '[' in token:
starti = i
#if not found [ then append token
if (starti == -1):
rvTokens.append(token)
#if found ] then update index and handle
if ']' in token:
nToken = 'EDIT_' + token.strip('[]')
if(nToken != 'EDIT_'):
rvTokens.append(nToken)
starti = -1
#if we have seen [ and still have not seen ]
if(starti != -1):
newToken = 'EDIT_' + token.strip('[]')
if(newToken != 'EDIT_'):
rvTokens.append(newToken)
return rvTokens
#NEGATIION
negation = re.compile(r"(n't|not|\bno\b|never)")
#keywords to end negation in sentences
endneg = ["but","however","nonetheless", "nevertheless", ".", "?", "!"]
#Input: tokenized words
#Output: label NOT_ before each token that appears after a negation until endneg tokens occur
def tag_negation(tokenized_snippet):
rvTokens = []
editWords = []
#seperate out EDIT tags
for i, token in enumerate(tokenized_snippet):
if('EDIT_' not in token):
rvTokens.append(token)
else:
editWords.append(token.strip('EDIT_'))
rvTokens.append(token.strip('EDIT_'))
#get POS tags
initTuples = nltk.pos_tag(rvTokens)
rvTuples = []
for tup in initTuples:
word = tup[0]
pos = tup[1]
#add back the EDIT tag
if(word in editWords):
rvTuples.append(('EDIT_'+ word, pos))
else:
rvTuples.append(tup)
#find NOT tuples
notTuples = []
foundNeg = False
foundEnd = False
for i, tup in enumerate(rvTuples):
word = tup[0]
pos = tup[1]
#match negation regex
if(re.search(negation, word) != None):
if(i != len(rvTuples)- 1):
nextTup = rvTuples[i+1]
nextWord = nextTup[0]
#check cornercase for 'not only'
if(nextWord != 'only'):
foundNeg = True
foundEnd = False
notTuples.append((word, pos))
continue
if (pos in ['JJR', 'RBR']):
foundEnd = True
#if found negation word but haven't found end neg then tag NOT
if(foundNeg == True and foundEnd == False):
notTuples.append(('NOT_' + word, pos))
else:
#just append tokens
notTuples.append((word, pos))
#found end neg word
if(word[-1] in endneg):
foundEnd = True
return notTuples
#Returns feature vector for processed training data
def get_features(preprocessed_snippet):
#initialize feature vector
feature_vect = np.zeros(len(feature_dict.keys()))
for tuple in preprocessed_snippet:
w = tuple[0]
for i, key in enumerate(feature_dict.keys()):
if(w == key):
#found match, increment count
feature_vect[i] += 1
return feature_vect
#Normalize Matrix
def normalize(X):
arrMax = np.max(X, axis=0)
rvX = np.zeros((len(X), len(arrMax)))
for i, col in enumerate(X.T):
values = col
rvX[:,i] = np.nan_to_num((values - min(values)) / (max(values) - min(values)))
return rvX
############Training data############
listOfTuples = load_corpus("train.txt")
print('load_corpus() result')
print(listOfTuples[:50])
#Call functions to process training data
allTuples = []
for i in listOfTuples:
tokenList = tokenize(i[0])
taggedTokens = tag_edits(tokenList)
taggedNots = tag_negation(taggedTokens)
for tag in taggedNots:
allTuples.append(tag)
#Create feature dict
ind = 0
feature_dict = dict()
for tuple in allTuples:
word = tuple[0]
pos = tuple[1]
#create vocabulary with unique words
if('EDIT_' not in word) and (word not in feature_dict.keys()):
feature_dict[word] = ind
ind += 1
#Create training matrix X and label vector Y
X_train = np.zeros((len(listOfTuples), len(feature_dict.keys())))
Y_train = np.zeros(len(listOfTuples))
for ind, i in enumerate(listOfTuples):
tokenList = tokenize(i[0])
label = i[1]
taggedTokens = tag_edits(tokenList)
taggedNots = tag_negation(taggedTokens)
vector = get_features(taggedNots)
#populate matrix and label vector
X_train[ind] = vector
Y_train[ind] = label
#normalize X train
X_train = normalize(X_train)
###########Evaluation measures: Precision, Recall, and Fmeasure
from sklearn.metrics import precision_score, recall_score, f1_score
def evaluate_predictions(Y_pred, Y_true):
precision = precision_score(Y_true, Y_pred)
recall = recall_score(Y_true, Y_pred)
fmeasure = f1_score(Y_true, Y_pred)
print('Evaluate prediction scores:')
print((precision, recall, fmeasure))
return (precision, recall, fmeasure)
#Simple Test Case
evaluate_predictions([0,1,0,1], [0,1,1,0])
#############Test data###############
testTuples = load_corpus("test.txt")
#Create test matrix X_test and label vector Y_true
X_test = np.zeros((len(testTuples), len(feature_dict.keys())))
Y_true = np.zeros(len(testTuples))
for ind, tup in enumerate(testTuples):
tokenList = tokenize(tup[0])
label = tup[1]
taggedTokens = tag_edits(tokenList)
taggedNots = tag_negation(taggedTokens)
vector = get_features(taggedNots)
#populate test matrix and label vector
X_test[ind] = vector
Y_true[ind] = label
#normalize test matrix
X_testnorm = normalize(X_test)
############Logistic Regression Model##########
from sklearn.linear_model import LogisticRegression
lrModel = LogisticRegression()
lrModel.fit(X_train, Y_train)
#Generate predictions on test set
Y_pred = lrModel.predict(X_testnorm)
evaluate_predictions(Y_pred, Y_true)
#Display top k features for a trained model
def top_features(model, k):
#sort by second val in tuple
def sortSecond(val):
return abs(val[1])
coef = model.coef_[0]
listTups = []
for i, val in enumerate(coef):
listTups.append((i, val))
#sort in descending by abs weight value
listTups.sort(key = sortSecond, reverse=True)
#return tuples
rvTups = []
for tup in listTups:
index = tup[0]
for key, val in feature_dict.items():
#found match
if(val == index):
rvTups.append((key, tup[1]))
#return to the kth element
return rvTups[:k]
print(top_features(lrModel, 10))