-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentimentalAnalysis.py
573 lines (477 loc) · 20.7 KB
/
sentimentalAnalysis.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# coding: utf-8
"""
In this, you will build a text classifier to determine whether a
movie review is expressing positive or negative sentiment. The data come from
the website IMDB.com.
You'll write code to preprocess the data in different ways (creating different
features), then compare the cross-validation accuracy of each approach. Then,
you'll compute accuracy on a test set and do some analysis of the errors.
The main method takes about 40 seconds for me to run on my laptop. Places to
check for inefficiency include the vectorize function and the
eval_all_combinations function.
"""
# No imports allowed besides these.
from collections import Counter, defaultdict
from itertools import chain, combinations
import glob
import matplotlib.pyplot as plt
import numpy as np
import os
import re
from scipy.sparse import csr_matrix
from sklearn.cross_validation import KFold
from sklearn.linear_model import LogisticRegression
import string
import tarfile
import urllib.request
#my method to add two dicts
def dsum(*dicts):
ret = defaultdict(int)
for d in dicts:
for k, v in d.items():
ret[k] += v
return dict(ret)
def download_data():
""" Download and unzip data.
DONE ALREADY.
"""
url = 'https://www.dropbox.com/s/xk4glpk61q3qrg2/imdb.tgz?dl=1'
urllib.request.urlretrieve(url, 'imdb.tgz')
tar = tarfile.open("imdb.tgz")
tar.extractall()
tar.close()
def read_data(path):
"""
Walks all subdirectories of this path and reads all
the text files and labels.
DONE ALREADY.
Params:
path....path to files
Returns:
docs.....list of strings, one per document
labels...list of ints, 1=positive, 0=negative label.
Inferred from file path (i.e., if it contains
'pos', it is 1, else 0)
"""
fnames = sorted([f for f in glob.glob(os.path.join(path, 'pos', '*.txt'))])
data = [(1, open(f).readlines()[0]) for f in sorted(fnames)]
fnames = sorted([f for f in glob.glob(os.path.join(path, 'neg', '*.txt'))])
data += [(0, open(f).readlines()[0]) for f in sorted(fnames)]
data = sorted(data, key=lambda x: x[1])
return np.array([d[1] for d in data]), np.array([d[0] for d in data])
def tokenize(doc, keep_internal_punct=False):
"""
Tokenize a string.
The string should be converted to lowercase.
If keep_internal_punct is False, then return only the alphanumerics (letters, numbers and underscore).
If keep_internal_punct is True, then also retain punctuation that
is inside of a word. E.g., in the example below, the token "isn't"
is maintained when keep_internal_punct=True; otherwise, it is
split into "isn" and "t" tokens.
Params:
doc....a string.
keep_internal_punct...see above
Returns:
a numpy array containing the resulting tokens.
>>> tokenize(" Hi there! Isn't this fun?", keep_internal_punct=False)
array(['hi', 'there', 'isn', 't', 'this', 'fun'],
dtype='<U5')
>>> tokenize("Hi there! Isn't this fun? ", keep_internal_punct=True)
array(['hi', 'there', "isn't", 'this', 'fun'],
dtype='<U5')
"""
if (keep_internal_punct):
tnp = []
for x in doc.lower().split():
tnp.append(re.sub('^\W+', '',re.sub('\W+$', '',x)))
return np.array(tnp)
else:
return np.array(re.sub('\W+', ' ', doc.lower()).split())
def token_features(tokens, feats):
"""
Add features for each token. The feature name
is pre-pended with the string "token=".
Note that the feats dict is modified in place,
so there is no return value.
Params:
tokens...array of token strings from a document.
feats....dict from feature name to frequency
Returns:
nothing; feats is modified in place.
>>> feats = defaultdict(lambda: 0)
>>> token_features(['hi', 'there', 'hi'], feats)
>>> sorted(feats.items())
[('token=hi', 2), ('token=there', 1)]
"""
#tmp = dict(Counter(dict(Counter(["token=" + s for s in tokens])))+Counter(feats))
#feats.update(dict(Counter(Counter(["token=" + s for s in tokens]))+Counter(feats)))
feats.update(dsum(dict(Counter(Counter(["token=" + s for s in tokens]))),feats))
#print (feats)
def token_pair_features(tokens, feats, k=3):
"""
Compute features indicating that two words occur near
each other within a window of size k.
For example [a, b, c, d] with k=3 will consider the
windows: [a,b,c], [b,c,d]. In the first window,
a_b, a_c, and b_c appear; in the second window,
b_c, c_d, and b_d appear. This example is in the
doctest below.
Note that the order of the tokens in the feature name
matches the order in which they appear in the document.
(e.g., a__b, not b__a)
Params:
tokens....array of token strings from a document.
feats.....a dict from feature to value
k.........the window size (3 by default)
Returns:
nothing; feats is modified in place.
>>> feats = defaultdict(lambda: 0)
>>> token_pair_features(np.array(['a', 'b', 'c', 'd']), feats)
>>> sorted(feats.items())
[('token_pair=a__b', 1), ('token_pair=a__c', 1), ('token_pair=b__c', 2), ('token_pair=b__d', 1), ('token_pair=c__d', 1)]
"""
for i in range(len(tokens)-k+1):
for e in list(combinations(list(tokens[i:k+i]), 2)):
feats['token_pair='+e[0]+'__'+e[1]] += 1
neg_words = set(['bad', 'hate', 'horrible', 'worst', 'boring'])
pos_words = set(['awesome', 'amazing', 'best', 'good', 'great', 'love', 'wonderful'])
def lexicon_features(tokens, feats):
"""
Add features indicating how many time a token appears that matches either
the neg_words or pos_words (defined above). The matching should ignore
case.
Params:
tokens...array of token strings from a document.
feats....dict from feature name to frequency
Returns:
nothing; feats is modified in place.
In this example, 'LOVE' and 'great' match the pos_words,
and 'boring' matches the neg_words list.
>>> feats = defaultdict(lambda: 0)
>>> lexicon_features(np.array(['i', 'LOVE', 'this', 'great', 'boring', 'movie']), feats)
>>> sorted(feats.items())
[('neg_words', 1), ('pos_words', 2)]
"""
#feats.update(dict(Counter({'pos_words': len(pos_words & set(s.lower() for s in tokens)) , 'neg_words' : len(neg_words & set(s.lower() for s in tokens)) })+Counter(feats)))
#feats.update(dsum(dict(Counter({'pos_words': len(pos_words & set(s.lower() for s in tokens)) , 'neg_words' : len(neg_words & set(s.lower() for s in tokens)) })),feats))
feats.update(dsum(dict(Counter({'pos_words': len([x for x in list(s.lower() for s in tokens) if x in list(pos_words)]) , 'neg_words' : len([x for x in list(s.lower() for s in tokens) if x in list(neg_words)]) })),feats))
def featurize(tokens, feature_fns):
"""
Compute all features for a list of tokens from
a single document.
Params:
tokens........array of token strings from a document.
feature_fns...a list of functions, one per feature
Returns:
list of (feature, value) tuples, SORTED alphabetically
by the feature name.
>>> feats = featurize(np.array(['i', 'LOVE', 'this', 'great', 'movie']), [token_features, lexicon_features])
>>> feats
[('neg_words', 0), ('pos_words', 2), ('token=LOVE', 1), ('token=great', 1), ('token=i', 1), ('token=movie', 1), ('token=this', 1)]
"""
feats = defaultdict(lambda : 0)
for fn in feature_fns:
fn(tokens,feats)
return sorted(list(feats.items()), key=lambda x: (x[0]))
def vectorize(tokens_list, feature_fns, min_freq, vocab=None):
"""
Given the tokens for a set of documents, create a sparse
feature matrix, where each row represents a document, and
each column represents a feature.
Params:
tokens_list...a list of lists; each sublist is an
array of token strings from a document.
feature_fns...a list of functions, one per feature
min_freq......Remove features that do not appear in
at least min_freq different documents.
Returns:
- a csr_matrix: See https://goo.gl/f5TiF1 for documentation.
This is a sparse matrix (zero values are not stored).
- vocab: a dict from feature name to column index. NOTE
that the columns are sorted alphabetically (so, the feature
"token=great" is column 0 and "token=horrible" is column 1
because "great" < "horrible" alphabetically),
>>> docs = ["Isn't this movie great?", "Horrible, horrible movie"]
>>> tokens_list = [tokenize(d) for d in docs]
>>> feature_fns = [token_features]
>>> X, vocab = vectorize(tokens_list, feature_fns, min_freq=1)
>>> type(X)
<class 'scipy.sparse.csr.csr_matrix'>
>>> X.toarray()
array([[1, 0, 1, 1, 1, 1],
[0, 2, 0, 1, 0, 0]], dtype=int64)
>>> sorted(vocab.items(), key=lambda x: x[1])
[('token=great', 0), ('token=horrible', 1), ('token=isn', 2), ('token=movie', 3), ('token=t', 4), ('token=this', 5)]
"""
vf = []
vocabSec = {}
for t in tokens_list:
vf.append(list(featurize(t,feature_fns)))
if vocab is None:
vocabSec = {i:x for x,i in enumerate(sorted(list([k for k,v in dict(Counter(list([e[0] for e in list(chain(*vf)) if e[1]>0]))).items() if v >=min_freq])))}
else:
vocabSec = vocab
#print (vocabSec)
column=[]
data=[]
rows=[]
row=0
for f in vf:
for e in f:
if e[0] in vocabSec:
rows.append(row)
column.append(vocabSec[e[0]])
data.append(e[1])
row+=1
data=np.array(data,dtype='int64')
rows=np.array(rows,dtype='int64')
column=np.array(column,dtype='int64')
X=csr_matrix((data, (rows,column)), shape=(len(tokens_list), len(vocabSec)))
#print (X.toarray())
#print (sorted(vocabSec.items(), key=lambda x: x[1]))
return X,vocabSec
def accuracy_score(truth, predicted):
""" Compute accuracy of predictions.
DONE ALREADY
Params:
truth.......array of true labels (0 or 1)
predicted...array of predicted labels (0 or 1)
"""
return len(np.where(truth==predicted)[0]) / len(truth)
def cross_validation_accuracy(clf, X, labels, k):
"""
Compute the average testing accuracy over k folds of cross-validation. You
can use sklearn's KFold class here (no random seed, and no shuffling
needed).
Params:
clf......A LogisticRegression classifier.
X........A csr_matrix of features.
labels...The true labels for each instance in X
k........The number of cross-validation folds.
Returns:
The average testing accuracy of the classifier
over each fold of cross-validation.
"""
cv = KFold(len(labels), k)
accuracies = []
for train_ind, test_ind in cv:
clf.fit(X[train_ind], labels[train_ind])
predictions = clf.predict(X[test_ind])
accuracies.append(accuracy_score(labels[test_ind],predictions))
return np.mean(accuracies)
def eval_all_combinations(docs, labels, punct_vals,
feature_fns, min_freqs):
"""
Enumerate all possible classifier settings and compute the
cross validation accuracy for each setting. We will use this
to determine which setting has the best accuracy.
For each setting, construct a LogisticRegression classifier
and compute its cross-validation accuracy for that setting.
In addition to looping over possible assignments to
keep_internal_punct and min_freqs, we will enumerate all
possible combinations of feature functions. So, if
feature_fns = [token_features, token_pair_features, lexicon_features],
then we will consider all 7 combinations of features (see Log.txt
for more examples).
Params:
docs..........The list of original training documents.
labels........The true labels for each training document (0 or 1)
punct_vals....List of possible assignments to
keep_internal_punct (e.g., [True, False])
feature_fns...List of possible feature functions to use
min_freqs.....List of possible min_freq values to use
(e.g., [2,5,10])
Returns:
A list of dicts, one per combination. Each dict has
four keys:
'punct': True or False, the setting of keep_internal_punct
'features': The list of functions used to compute features.
'min_freq': The setting of the min_freq parameter.
'accuracy': The average cross_validation accuracy for this setting, using 5 folds.
This list should be SORTED in descending order of accuracy.
This function will take a bit longer to run (~20s for me).
"""
result = []
for pv in punct_vals:
tokens_list = [ tokenize(d,pv) for d in docs ]
for mf in min_freqs:
for L in range(1, len(feature_fns)+1):
for subset in combinations(feature_fns,L):
X,vocab = vectorize(tokens_list, list(subset),mf)
ac = cross_validation_accuracy(LogisticRegression(), X, labels, 5)
result.append({'features':subset,'punct': pv,'accuracy': ac,'min_freq': mf})
return sorted(result,key=lambda k: -k['accuracy'])
def plot_sorted_accuracies(results):
"""
Plot all accuracies from the result of eval_all_combinations
in ascending order of accuracy.
Save to "accuracies.png".
"""
plt.plot([d['accuracy'] for d in sorted(results,key=lambda k: k['accuracy'])])
plt.xlabel('settings')
plt.ylabel('accuracy')
plt.savefig('accuracies.png')
def mean_accuracy_per_setting(results):
"""
To determine how important each model setting is to overall accuracy,
we'll compute the mean accuracy of all combinations with a particular
setting. For example, compute the mean accuracy of all runs with
min_freq=2.
Params:
results...The output of eval_all_combinations
Returns:
A list of (accuracy, setting) tuples, SORTED in
descending order of accuracy.
"""
meanResult = []
for pv in set([d['punct'] for d in results]):
meanResult.append((np.mean([d['accuracy'] for d in results if d['punct']==pv]),'punct='+str(pv)))
for mf in set([d['min_freq'] for d in results]):
meanResult.append((np.mean([d['accuracy'] for d in results if d['min_freq']==mf]),'min_freq='+str(mf)))
for fname in set([d['features'] for d in results]):
meanResult.append((np.mean([d['accuracy'] for d in results if d['features']==fname]),'features='+' '.join([f.__name__ for f in list(fname)])))
return sorted(meanResult, key=lambda x: (-x[0]))
def fit_best_classifier(docs, labels, best_result):
"""
Using the best setting from eval_all_combinations,
re-vectorize all the training data and fit a
LogisticRegression classifier to all training data.
(i.e., no cross-validation done here)
Params:
docs..........List of training document strings.
labels........The true labels for each training document (0 or 1)
best_result...Element of eval_all_combinations
with highest accuracy
Returns:
clf.....A LogisticRegression classifier fit to all
training data.
vocab...The dict from feature name to column index.
"""
tokens_list = [ tokenize(d,best_result['punct']) for d in docs ]
X,vocab = vectorize(tokens_list,list(best_result['features']),best_result['min_freq'])
model = LogisticRegression()
model.fit(X,labels)
return model,vocab
def top_coefs(clf, label, n, vocab):
"""
Find the n features with the highest coefficients in
this classifier for this label.
See the .coef_ attribute of LogisticRegression.
Params:
clf.....LogisticRegression classifier
label...1 or 0; if 1, return the top coefficients
for the positive class; else for negative.
n.......The number of coefficients to return.
vocab...Dict from feature name to column index.
Returns:
List of (feature_name, coefficient) tuples, SORTED
in descending order of the coefficient for the
given class label.
"""
ic = []
if label == 0:
ic = [x * -1 for x in clf.coef_[0]]
elif label == 1:
ic = clf.coef_[0].tolist()
coef = np.array(ic)
top_coef_ind = np.argsort(coef)[::-1][:n]
top_coef_terms = np.array([k for k,v in sorted(vocab.items(), key=lambda x: x[1])])[top_coef_ind]
top_coef = coef[top_coef_ind]
return [x for x in zip(top_coef_terms, top_coef)]
def parse_test_data(best_result, vocab):
"""
Using the vocabulary fit to the training data, read
and vectorize the testing data. Note that vocab should
be passed to the vectorize function to ensure the feature
mapping is consistent from training to testing.
Note: use read_data function defined above to read the
test data.
Params:
best_result...Element of eval_all_combinations
with highest accuracy
vocab.........dict from feature name to column index,
built from the training data.
Returns:
test_docs.....List of strings, one per testing document,
containing the raw.
test_labels...List of ints, one per testing document,
1 for positive, 0 for negative.
X_test........A csr_matrix representing the features
in the test data. Each row is a document,
each column is a feature.
"""
test_docs, test_labels = read_data(os.path.join('data', 'test'))
tokens_list = [tokenize(d,best_result['punct']) for d in test_docs]
X_test,rvocab = vectorize(tokens_list,list(best_result['features']),best_result['min_freq'],vocab)
return test_docs, test_labels, X_test
def print_top_misclassified(test_docs, test_labels, X_test, clf, n):
"""
Print the n testing documents that are misclassified by the
largest margin. By using the .predict_proba function of
LogisticRegression <https://goo.gl/4WXbYA>, we can get the
predicted probabilities of each class for each instance.
We will first identify all incorrectly classified documents,
then sort them in descending order of the predicted probability
for the incorrect class.
E.g., if document i is misclassified as positive, we will
consider the probability of the positive class when sorting.
Params:
test_docs.....List of strings, one per test document
test_labels...Array of true testing labels
X_test........csr_matrix for test data
clf...........LogisticRegression classifier fit on all training
data.
n.............The number of documents to print.
Returns:
Nothing; see Log.txt for example printed output.
"""
p_test = clf.predict(X_test)
pp_test = clf.predict_proba(X_test)
p_indocs = {}
for i in range(len(p_test)):
if p_test[i] != test_labels[i]:
p_indocs[i] = pp_test[i][p_test[i]]
for p in list(sorted(p_indocs.items(), key=lambda x: -x[1])[:n]):
print("truth=%s predicated=%s proba=%.6f" % (str(test_labels[p[0]]),str(p_test[p[0]]),p[1]))
print(test_docs[p[0]]+"\n")
def main():
"""
Put it all together.
ALREADY DONE.
"""
feature_fns = [token_features, token_pair_features, lexicon_features]
# Download and read data.
#download_data()
docs, labels = read_data(os.path.join('data', 'train'))
# Evaluate accuracy of many combinations
# of tokenization/featurization.
results = eval_all_combinations(docs, labels,
[True, False],
feature_fns,
[2,5,10])
# Print information about these results.
best_result = results[0]
worst_result = results[-1]
print('best cross-validation result:\n%s' % str(best_result))
print('worst cross-validation result:\n%s' % str(worst_result))
plot_sorted_accuracies(results)
print('\nMean Accuracies per Setting:')
print('\n'.join(['%s: %.5f' % (s,v) for v,s in mean_accuracy_per_setting(results)]))
# Fit best classifier.
clf, vocab = fit_best_classifier(docs, labels, results[0])
# Print top coefficients per class.
print('\nTOP COEFFICIENTS PER CLASS:')
print('negative words:')
print('\n'.join(['%s: %.5f' % (t,v) for t,v in top_coefs(clf, 0, 5, vocab)]))
print('\npositive words:')
print('\n'.join(['%s: %.5f' % (t,v) for t,v in top_coefs(clf, 1, 5, vocab)]))
# Parse test data
test_docs, test_labels, X_test = parse_test_data(best_result, vocab)
# Evaluate on test set.
predictions = clf.predict(X_test)
print('testing accuracy=%f' %
accuracy_score(test_labels, predictions))
print('\nTOP MISCLASSIFIED TEST DOCUMENTS:')
print_top_misclassified(test_docs, test_labels, X_test, clf, 5)
if __name__ == '__main__':
main()