-
Notifications
You must be signed in to change notification settings - Fork 1
/
classifier_pi.py
272 lines (237 loc) · 8.41 KB
/
classifier_pi.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
__author__ = "G4"
import re
import nltk
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.svm import LinearSVC
import os
import pickle as pk
pattern='\[(.*?)\]'
import classifier_prabha
import classifier_jt
import extract_features_david
# gloabl variable for feature words of unigram model
feature_words = []
#Start: Functions to extract features
def num_nt(review):
val=0
for word in review:
word=word.lower()
if word.endswith('n\'t'):
val+=1
return 'isnt',val
def num_ques(review):
val=0
for word in review:
word=word.lower()
val+=word.count('?')
return 'num_ques',val
def isinsight(review):
stemmer=nltk.PorterStemmer()
insight=[ 'think', 'know', 'consider']
val=0
for word in review:
word=word.lower()
word=stemmer.stem(word)
if word in insight:
val+=1
return 'isinsight',val
def istentative(review):
tent=['maybe', 'perhaps', 'guess']
val=0
for word in review:
word=word.lower()
if word in tent:
val+=1
return 'istentative',val
def iscertainty(review):
certain=['always', 'never']
val=0
for word in review:
word=word.lower()
if word in certain:
val+=1
return 'iscertainty',val
def isinhibition(review):
stemmer=nltk.PorterStemmer()
inhibit=['block', 'constrain', 'stop']
val=0
for word in review:
word=word.lower()
word=stemmer.stem(word)
if word in inhibit:
val+=1
return 'isinhibition',val
def isassent(review):
stemmer=nltk.PorterStemmer()
assent=['agree','ok','yes']
val=0
for word in review:
word=word.lower()
word=stemmer.stem(word)
if word in assent:
val+=1
return 'isassent',val
#End: Functions to extract features
#Load the training/heldout file and extract the text
def load_text_from_file(filename):
'''Parses the file to extract reviews from them'''
products=[]
scores=[]
reviews=[]
with open(filename,'r') as f:
#Change the labels to -1,0,+1
filelines=f.read().splitlines()
for lines in filelines:
line=lines.rstrip('\r\n').split('\t')
product=line[0]
products.append(product)
attr_labels=line[1]
review_score=[]
labels=re.findall(pattern,attr_labels)
for l in labels:
try:
l=int(l)
if l>0:
l=1
elif l<0:
l=-1
else:
l=0
review_score.append(l)
except ValueError:
l=0
mean_score=0
if len(review_score)!=0:
mean_score=sum(review_score)/len(review_score)
if mean_score>0:
mean_score=1
elif mean_score<0:
mean_score=-1
else:
mean_score=0
scores.append(mean_score)
review=line[2].strip()
reviews.append(review)
f.close()
return products,scores,reviews
#Feature Extraction
def extract_features(reviews,scores=[],mode='train'):
'''Extracts features from the reviews'''
train_set=[]
i=0
#Extract features for each review
for review in reviews:
features={}
if mode=='train':
review=review.split()
key,val=num_nt(review)
features[key]=val
key,val=num_ques(review)
features[key]=val
key,val=isinsight(review)
features[key]=val
key,val=istentative(review)
features[key]=val
key,val=isassent(review)
features[key]=val
key,val=isinhibition(review)
features[key]=val
key,val=iscertainty(review)
features[key]=val
if mode=='train':
train_set.append((features,scores[i]))
else:
train_set.append(features)
i+=1
return train_set
def load_test(filename):
linenos=[]
reviews=[]
with open(filename,'r') as test:
for line in test:
line=line.replace('##','')
line=line.rstrip('\r\n').split('\t')
linenos.append(line[0])
reviews.append(line[1])
test.close()
return linenos,reviews
def combine_sets(set1, set2, set3, set4, mode='train'):
final_set = []
if mode=="train":
for i in range(len(set1)):
dict1 = set1[i][0]
dict2 = set2[i][0]
dict3 = set3[i][0]
dict4 = set4[i][0]
score = set1[i][1]
final_set.append((dict(dict1.items() + dict2.items() + dict3.items() + dict4.items()), score))
else:
for i in range(len(set1)):
dict1 = set1[i]
dict2 = set2[i]
dict3 = set3[i]
dict4 = set4[i]
final_set.append(dict(dict1.items() + dict2.items() + dict3.items() + dict4.items()))
return final_set
def train_classifier(trainfile):
'''Training the classifier '''
products,scores,reviews=load_text_from_file(trainfile)
train_set_pi=extract_features(reviews,scores)
train_set_prabha=classifier_prabha.extract_features(reviews,scores)
train_set_david=extract_features_david.extract_features_david(reviews,scores)
# get feature words from review texts
feature_words = classifier_jt.get_feature_words(reviews, scores)
train_set_jt=classifier_jt.extract_unigram_feature(reviews,feature_words,scores)
train_set = combine_sets(train_set_pi, train_set_prabha, train_set_david, train_set_jt)
clf=SklearnClassifier(LinearSVC())
#trainlen=int(len(train_set)*0.9)
#model=clf.train(train_set)
model=nltk.NaiveBayesClassifier.train(train_set)
pk.dump(model,open('classifier.p','wb'))
#print 'Accuracy for the training set: ',nltk.classify.accuracy(model,train_set)
#print model.show_most_informative_features(5)
def evaluate_clf(heldout):
'''Testing the model on the heldout file'''
products,scores,reviews=load_text_from_file(heldout)
heldout_set_pi=extract_features(reviews,scores)
heldout_set_prabha=classifier_prabha.extract_features(reviews,scores)
heldout_set_david=extract_features_david.extract_features_david(reviews,scores)
heldout_set_jt=classifier_jt.extract_unigram_feature(reviews,feature_words,scores)
heldout_set = combine_sets(heldout_set_pi, heldout_set_prabha, heldout_set_david, heldout_set_jt)
model=pk.load(open('classifier.p','rb'))
print 'Accuracy for the heldout set: ',nltk.classify.accuracy(model,heldout_set)
print model.show_most_informative_features(5)
def classify_reviews(testfolder):
'''Classifying the actual test data'''
model=pk.load(open('classifier.p','rb'))
outputf=open('g4_output.txt','w+')
for testfile in os.listdir(testfolder):
# skip the file like '.DS_store' in Mac OS
if testfile.startswith('.'):
continue
testpath=os.path.join(testfolder,testfile)
linenos,test_reviews=load_test(testpath)
test_set_pi=extract_features(test_reviews,mode='test')
test_set_prabha=classifier_prabha.extract_features(test_reviews,mode='test')
test_set_david=extract_features_david.extract_features_david(test_reviews,mode='test')
test_set_jt=classifier_jt.extract_unigram_feature(test_reviews,feature_words,mode='test')
test_set = combine_sets(test_set_pi, test_set_prabha, test_set_david, test_set_jt, mode='test')
i=0
for each_res in test_set:
# TODO: [t] as netural
if test_reviews[i].startswith('[t]'):
outputf.write(str(testfile)+'\t'+str(i+1)+'\t0\n')
else:
result=model.classify(each_res)
outputf.write(str(testfile)+'\t'+str(i+1)+'\t'+str(result)+'\n')
i+=1
outputf.close()
if __name__=='__main__':
trainfile='trainingfile.txt' #Name of the training file
heldout='heldoutfile.txt' #Name of the heldout file
print "training classifier..."
train_classifier(trainfile) #function that trains the model
testfolder='./testset' #Folder which contains the test sets
print "classifying reviews...."
#evaluate_clf(heldout) #function that evaluates the mdoel on the heldout set
classify_reviews(testfolder) #function that loads the model and classifies