-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
397 lines (282 loc) · 11.1 KB
/
test.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
import json
import re
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction import DictVectorizer
from sklearn import cross_validation
from sklearn.metrics import accuracy_score, classification_report
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from MyLogesticRegression import MyLogisticRegression
from nltk.corpus import sentiwordnet as swn
from nltk.corpus import wordnet as wn
from scipy.spatial.distance import cosine as cos_distance
import gensim
from nltk.data import find
from nltk.corpus import brown
import math
import logging
from nltk.corpus import opinion_lexicon
#nltk.download('sentiwordnet')
word_set=set(nltk.corpus.words.words())
lemmatizer = nltk.stem.wordnet.WordNetLemmatizer()
sent_segmenter = nltk.data.load('tokenizers/punkt/english.pickle')
word_tokenizer = nltk.tokenize.regexp.WordPunctTokenizer()
def preprocess(tweet):
#print(tweet)
tweet = re.sub("@[^ ]+", "", tweet).strip()
tweet=re.sub("http[^ ]+","",tweet).strip()
hashtag = re.findall('#[^ ]*', tweet)
tag_list=[]
if hashtag:
#print hashtag,' hashtag----'
for tag in hashtag:
tag=re.sub('#','',tag)
#print tag,' tag----'
# if contains capital letters
if re.search('.*[A-Z].*',tag):
tag_list=tag_list+re.findall('[A-Z][^A-Z]*',tag)
else:
# if not contain capital letters
i=0
#print tag,'tag'
while i<len(tag):
for j in range(len(tag),i,-1):
lemma=lemmatize(tag[i:j])
if lemma in word_set:
#print(lemma)
tag_list.append(tag[i:j])
i=j-1
i+=1
if tag_list:
for i in range(len(tag_list)):
tag_list[i]=tag_list[i].lower()
#print tag_list
tweet = re.sub('#[^ ]*','',tweet).lower()
tweet = sent_segmenter.tokenize(tweet)
words = []
for sentence in tweet:
words= words+ word_tokenizer.tokenize(sentence)
if tag_list:
words=words+tag_list
#print words
return words
def preprocess_file(filename):
tweets = []
labels=[]
f = open(filename)
for line in f:
tweet_dict = json.loads(line)
#print tweet_dict
tweets.append(preprocess(tweet_dict["text"]))
labels.append(int(tweet_dict["label"]))
print 'done preprocessing'
return tweets,labels
def lemmatize(word):
lemma = lemmatizer.lemmatize(word,'v')
if lemma == word:
lemma = lemmatizer.lemmatize(word,'n')
return lemma
def convert_to_feature_dicts(tweets,remove_stop_words,n):
feature_dicts = []
if remove_stop_words:
stop_word_set=set(stopwords.words('english'))
if n>0:
small_set=set()
whole_feature_dic={}
for tweet in tweets:
for w in tweet:
whole_feature_dic[w]=whole_feature_dic.get(w,0)+1
for w in whole_feature_dic:
if whole_feature_dic[w]<=n:
small_set.add(w)
for tweet in tweets:
# build feature dictionary for tweet
feature_dict={}
for w in tweet:
if remove_stop_words and n<=0:
if w not in stop_word_set:
feature_dict[w]=feature_dict.get(w,0)+1
elif remove_stop_words and n>0:
if w not in stop_word_set and w not in small_set:
feature_dict[w]=feature_dict.get(w,0)+1
elif n>0:
if w not in small_set:
feature_dict[w]=feature_dict.get(w,0)+1
else:
feature_dict[w]=feature_dict.get(w,0)+1
feature_dicts.append(feature_dict)
#print feature_dicts
return feature_dicts
def prepare_data(trn_feature_dicts,dev_feature_dicts):
vectorizer = DictVectorizer()
trn_feature_dicts = vectorizer.fit_transform(trn_feature_dicts)
dev_feature_dicts = vectorizer.transform(dev_feature_dicts)
return trn_feature_dicts,dev_feature_dicts
def do_multiple_10foldcrossvalidation(clf,data,classifications):
predictions = cross_validation.cross_val_predict(clf, data,classifications, cv=10)
print clf
print "accuracy"
print accuracy_score(classifications,predictions)
print classification_report(classifications,predictions)
def test(clf,training_data,training_classifications,test_data,test_classifications):
clf.fit(training_data,training_classifications)
predictions = clf.predict(test_data)
accuracy = accuracy_score(test_classifications,predictions)
return accuracy
def get_polarity_type(synset_name):
swn_synset = swn.senti_synset(synset_name)
if not swn_synset:
return None
elif swn_synset.pos_score() > swn_synset.neg_score() and swn_synset.pos_score() > swn_synset.obj_score():
return 1
elif swn_synset.neg_score() > swn_synset.pos_score() and swn_synset.neg_score() > swn_synset.obj_score():
return -1
else:
return 0
def second_lexicon(positive_seeds,negative_seeds):
word2vec_sample = str(find('models/word2vec_sample/pruned.word2vec.txt'))
model = gensim.models.Word2Vec.load_word2vec_format(word2vec_sample, binary=False)
positive_list=[]
negative_list=[]
for aword in model.vocab:
score=0
for pseed in positive_seeds:
score+=model.similarity(aword, pseed)
for nseed in negative_seeds:
score-=model.similarity(aword,nseed)
score=score/16.0
if score>0.03:
positive_list.append(aword)
elif score<-0.03:
negative_list.append(aword)
return positive_list,negative_list
def get_BOW(text):
BOW = {}
for word in text:
BOW[word.lower()] = BOW.get(word.lower(),0) + 1
return BOW
def third_lexicon(positive_seeds,negative_seeds):
positive_list=[]
negative_list=[]
all_dic={}
seed_total_dic={}
for fileid in brown.fileids():
bow=get_BOW(brown.words(fileid))
for aword in bow:
all_dic[aword]=all_dic.get(aword,{})
all_dic[aword]['word_count']=all_dic[aword].get('word_count',0)+1
for pseed in positive_seeds:
if pseed in bow:
all_dic[aword][pseed]=all_dic[aword].get(pseed,0)+1
for nseed in negative_seeds:
if nseed in bow:
all_dic[aword][nseed]=all_dic[aword].get(nseed,0)+1
for pseed in positive_seeds:
if pseed in bow:
seed_total_dic[pseed]=seed_total_dic.get(pseed,0)+1
for nseed in negative_seeds:
if nseed in bow:
seed_total_dic[nseed]=seed_total_dic.get(nseed,0)+1
total_count=float(len(brown.fileids()))
for aword in all_dic:
score=0
for pseed in positive_seeds:
if all_dic[aword].get(pseed) != None:
a_score=math.log((all_dic[aword][pseed]/total_count)/((all_dic[aword]['word_count']/total_count)*(seed_total_dic[pseed]/total_count)), 2)
if a_score>0:
score+=a_score
for nseed in negative_seeds:
if all_dic[aword].get(nseed) != None:
a_score=math.log((all_dic[aword][nseed]/total_count)/((all_dic[aword]['word_count']/total_count)*(seed_total_dic[nseed]/total_count)), 2)
if a_score>0:
score-=a_score
score=score/16.0
if score>0.3:
positive_list.append(aword)
elif score<-0.3:
negative_list.append(aword)
return positive_list,negative_list
def calculate_percentage(manual,automatic):
automatic=set(automatic)
count=0
for word in manual:
if word in automatic:
count+=1
return float(count)/len(manual)
def my_polarity(tweet,pset,nset,):
score=0
for word in tweet:
if word in pset:
score+=1
elif word in nset:
score-=1
if score>0:
return 1
elif score<0:
return -1
else:
return 0
def accuracy_of_lexicon(tweets,labels,plist,nlist):
count=0
pset=set(plist)
nset=set(nlist)
for tweet,label in zip(tweets,labels):
if label==my_polarity(tweet,pset,nset):
count+=1
return float(count)/len(labels)
trn_tweets,trn_labels=preprocess_file('train.json')
trn_feature_dicts=convert_to_feature_dicts(trn_tweets,True,1)
dev_tweets,dev_labels=preprocess_file('dev.json')
dev_feature_dicts=convert_to_feature_dicts(dev_tweets,True,0)
trn_feature_dicts,dev_feature_dicts=prepare_data(trn_feature_dicts,dev_feature_dicts)
clf=DecisionTreeClassifier()
print test(clf,trn_feature_dicts,trn_labels,dev_feature_dicts,dev_labels)
#do_multiple_10foldcrossvalidation(clf,trn_feature_dicts,trn_labels)
clf2=LogisticRegression()
print test(clf2,trn_feature_dicts,trn_labels,dev_feature_dicts,dev_labels)
clf3 = LogisticRegression(solver='lbfgs', multi_class='multinomial')
clf3.fit(trn_feature_dicts,trn_labels)
myLR=MyLogisticRegression(clf3.coef_, clf3.intercept_, clf3.classes_)
print myLR.predict(dev_feature_dicts)[0:10]
'''
positive_list1=[]
negative_list1=[]
count = 0
for synset in wn.all_synsets():
count += 1
if count % 1000 == 0:
print count
# count synset polarity for each lemma
name=synset.name()
polarity_type=get_polarity_type(name)
if polarity_type is not None:
if polarity_type ==1:
positive_list1+=synset.lemma_names()
elif polarity_type== -1:
negative_list1+=synset.lemma_names()
print 'positive list negative list 1'
print positive_list1[0:10],negative_list1[0:10]
positive_seeds = ["good","nice","excellent","positive","fortunate","correct","superior","great"]
negative_seeds = ["bad","nasty","poor","negative","unfortunate","wrong","inferior","awful"]
positive_list2, negative_list2=second_lexicon(positive_seeds, negative_seeds)
print 'positive list negative list 2'
print positive_list2[0:10], negative_list2[0:10]
positive_list3, negative_list3=third_lexicon(positive_seeds, negative_seeds)
print 'positive list negative list 3'
print positive_list3[0:10], negative_list3[0:10]
positive_words = opinion_lexicon.positive()
negative_words = opinion_lexicon.negative()
percentage1p= calculate_percentage(positive_words,positive_list1)
percentage1n=calculate_percentage(negative_words,negative_list1)
percentage2p=calculate_percentage(positive_words, positive_list2)
percentage2n=calculate_percentage(negative_words, negative_list2)
percentage3p=calculate_percentage(positive_words, positive_list3)
percentage3n=calculate_percentage(negative_words, negative_list3)
print(percentage1p,percentage1n,percentage2p,percentage2n,percentage3p,percentage3n)
print 'accuracy of lexicon'
print accuracy_of_lexicon(dev_tweets,dev_labels,positive_words,negative_words)
print accuracy_of_lexicon(dev_tweets,dev_labels,positive_list1,negative_list1)
print accuracy_of_lexicon(dev_tweets,dev_labels,positive_list2,negative_list2)
print accuracy_of_lexicon(dev_tweets,dev_labels,positive_list3,negative_list3)
'''