-
Notifications
You must be signed in to change notification settings - Fork 1
/
betterTrainingData.py
334 lines (277 loc) · 10.3 KB
/
betterTrainingData.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 2 15:53:02 2019
@author: jeetu
"""
import io
import nltk
import random
from nltk.corpus import movie_reviews
from nltk.classify.scikitlearn import SklearnClassifier
import pickle
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from nltk.classify import ClassifierI
from statistics import mode
from nltk.tokenize import word_tokenize
class VoteClassifier(ClassifierI):
def __init__(self, *classifiers):
self._classifiers = classifiers
def classify(self, features):
votes = []
for c in self._classifiers:
v = c.classify(features)
votes.append(v)
return mode(votes)
def confidence(self, features):
votes = []
for c in self._classifiers:
v = c.classify(features)
votes.append(v)
choice_votes = votes.count(mode(votes))
conf = choice_votes / len(votes)
return conf
short_pos = io.open("positive.txt", "r", encoding='latin-1').read()
short_neg = io.open("negative.txt", "r", encoding='latin-1').read()
documents = []
for r in short_pos.split('\n'):
documents.append((r, "pos"))
for r in short_neg.split('\n'):
documents.append((r, "neg"))
all_words = []
short_pos_words = word_tokenize(short_pos)
short_neg_words = word_tokenize(short_neg)
for w in short_pos_words:
all_words.append(w.lower())
for w in short_neg_words:
all_words.append(w.lower())
all_words = nltk.FreqDist(all_words)
word_features = list(all_words.keys())[:5000]
def find_features(document):
words = word_tokenize(document)
features = {}
for w in word_features:
features[w] = (w in words)
return features
# print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))
featuresets = [(find_features(rev), category) for (rev, category) in documents]
random.shuffle(featuresets)
# positive data example:
training_set = featuresets[:10000]
testing_set = featuresets[10000:]
##
### negative data example:
##training_set = featuresets[100:]
##testing_set = featuresets[:100]
classifier = nltk.NaiveBayesClassifier.train(training_set)
print("Original Naive Bayes Algo accuracy percent:", (nltk.classify.accuracy(classifier, testing_set)) * 100)
classifier.show_most_informative_features(15)
MNB_classifier = SklearnClassifier(MultinomialNB())
MNB_classifier.train(training_set)
print("MNB_classifier accuracy percent:", (nltk.classify.accuracy(MNB_classifier, testing_set)) * 100)
BernoulliNB_classifier = SklearnClassifier(BernoulliNB())
BernoulliNB_classifier.train(training_set)
print("BernoulliNB_classifier accuracy percent:", (nltk.classify.accuracy(BernoulliNB_classifier, testing_set)) * 100)
LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
LogisticRegression_classifier.train(training_set)
print("LogisticRegression_classifier accuracy percent:",
(nltk.classify.accuracy(LogisticRegression_classifier, testing_set)) * 100)
SGDClassifier_classifier = SklearnClassifier(SGDClassifier())
SGDClassifier_classifier.train(training_set)
print("SGDClassifier_classifier accuracy percent:",
(nltk.classify.accuracy(SGDClassifier_classifier, testing_set)) * 100)
##SVC_classifier = SklearnClassifier(SVC())
##SVC_classifier.train(training_set)
##print("SVC_classifier accuracy percent:", (nltk.classify.accuracy(SVC_classifier, testing_set))*100)
LinearSVC_classifier = SklearnClassifier(LinearSVC())
LinearSVC_classifier.train(training_set)
print("LinearSVC_classifier accuracy percent:", (nltk.classify.accuracy(LinearSVC_classifier, testing_set)) * 100)
NuSVC_classifier = SklearnClassifier(NuSVC())
NuSVC_classifier.train(training_set)
print("NuSVC_classifier accuracy percent:", (nltk.classify.accuracy(NuSVC_classifier, testing_set)) * 100)
voted_classifier = VoteClassifier(
NuSVC_classifier,
LinearSVC_classifier,
MNB_classifier,
BernoulliNB_classifier,
LogisticRegression_classifier)
print("voted_classifier accuracy percent:", (nltk.classify.accuracy(voted_classifier, testing_set)) * 100)
#.............................................
#import io
##io.open(filename, encoding='latin-1')
#
#import nltk
#import random
##from nltk.corpus import movie_reviews
#from nltk.classify.scikitlearn import SklearnClassifier
#import pickle
#from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
#from sklearn.linear_model import LogisticRegression, SGDClassifier
#from sklearn.svm import SVC, LinearSVC,NuSVC
#from nltk.classify import ClassifierI
#from statistics import mode
#from nltk.tokenize import word_tokenize
#
#
#class VoteClassifier(ClassifierI):
# def __init__(self, *classifiers):
# self._classifiers = classifiers
#
# def classify(self,features):
# votes=[]
# for c in self._classifiers:
# v=c.classify(features)
# votes.append(v)
# return mode (votes)
#
# def confidence (self, features):
#
# votes=[]
# for c in self._classifiers:
# v=c.classify(features)
# votes.append(v)
#
# choice_votes = votes.count (mode(votes))
# conf =choice_votes / len(votes)
# return conf
#
#
#short_pos = io.open("positive.txt","r", encoding='latin-1').read()
#short_neg = io.open("negative.txt","r", encoding='latin-1').read()
##os.path.join(root, f)
#
#documents=[]
#
#for r in short_pos.split('/n'):
# documents.append((r,"pos"))
#
#for r in short_neg.split('/n'):
# documents.append((r,"neg"))
#
#
#all_words=[]
#short_pos_words=word_tokenize(short_pos)
#short_neg_words=word_tokenize(short_neg)
#
#for w in short_pos_words:
# all_words.append(w.lower())
#
#for w in short_neg_words:
# all_words.append(w.lower())
#
##
##
##documents = [(list(movie_reviews.words(fileid)), category)
## for category in movie_reviews.categories()
## for fileid in movie_reviews.fileids(category)]
##
###this is similar to this method
##'''
##
##documents=[]
##
##for category in movie_reviews.categories():
## for fileid in movie_reviews.fileids(category):
## documents.append(list(movie_reviews.word(fileid)),category)
##
## '''
##random.shuffle(documents)
###print (documents)
##
##all_words=[]
##for w in movie_reviews.words():
## all_words.append(w.lower())
#
#all_words=nltk.FreqDist(all_words)
#
#word_features=list (all_words.keys())[:5000]
#
#def find_features(document):
# words = word_tokenize(document)
# features ={}
# for w in word_features:
# features[w]=(w in words)
#
# return features
##print((find_features(movie_reviews.words('neg/cv000_29416.txt'))))
#featuresets = [(find_features(rev), category) for (rev, category) in documents]
#
#random.shuffle(featuresets)
#
##positive data set example
#training_set =featuresets[:10000]
#testing_set = featuresets[10000:]
#
##negative data set example
##training_set =featuresets[100:]
##testing_set = featuresets[:100]
#
#
#classifier=nltk.NaiveBayesClassifier.train(training_set)
##classifier_file=open("naivebayes.pickle","rb")
##classifier=pickle.load(classifier_file)
##classifier_file.close()
#
#print ("Original Naive Bayes Algo accuracy in percentage:- ",(nltk.classify.accuracy(classifier,testing_set))*100)
#classifier.show_most_informative_features(15)
#
##
##MNB_Classifier=SklearnClassifier(MultinomialNB())
##MNB_Classifier.train(training_set)
##print ("MNB_Classifier Algo accuracy in percentage:- ",(nltk.classify.accuracy(MNB_Classifier,testing_set))*100)
#
#MNB_classifier = SklearnClassifier(MultinomialNB())
#MNB_classifier.train(training_set)
#print("MNB_classifier accuracy percent:", (nltk.classify.accuracy(MNB_classifier, testing_set)) * 100)
#
##BernoulliNB
#BNB_Classifier=SklearnClassifier(BernoulliNB())
#BNB_Classifier.train(training_set)
#print ("BernoulliNB Algo accuracy in percentage:- ",(nltk.classify.accuracy(BNB_Classifier,testing_set))*100)
#
##LogisticRegression, SGDClassifier, SVC, LinearSVC, NuSVC
#
#LogisticRegression_Classifier=SklearnClassifier(LogisticRegression())
#LogisticRegression_Classifier.train(training_set)
#print ("LogisticRegression_Classifier Algo accuracy in percentage:- ",(nltk.classify.accuracy(LogisticRegression_Classifier,testing_set))*100)
#
#
#SGDClassifier=SklearnClassifier(SGDClassifier())
#SGDClassifier.train(training_set)
#print ("SGDClassifier Algo accuracy in percentage:- ",(nltk.classify.accuracy(SGDClassifier,testing_set))*100)
#
#
##SVC_Classifier=SklearnClassifier(SVC())
##SVC_Classifier.train(testing_set)
##print ("SVC_Classifier Algo accuracy in percentage:- ",(nltk.classify.accuracy(SVC_Classifier,testing_set))*100)
#
#
#LinearSVC_Classifier=SklearnClassifier(LinearSVC())
#LinearSVC_Classifier.train(training_set)
#print ("LinearSVC_Classifier Algo accuracy in percentage:- ",(nltk.classify.accuracy(LinearSVC_Classifier,testing_set))*100)
#
#
#NuSVC_Classifier=SklearnClassifier(NuSVC())
#NuSVC_Classifier.train(training_set)
#print ("NuSVC_Classifier Algo accuracy in percentage:- ",(nltk.classify.accuracy(NuSVC_Classifier,testing_set))*100)
#
######### VOTE
#voted_classifier = VoteClassifier(
# MNB_Classifier,
# BNB_Classifier,
# LogisticRegression_Classifier,
# LinearSVC_Classifier,
# NuSVC_Classifier)
#
#print ("voted_classifier accuracy percent: " , (nltk.classify.accuracy(voted_classifier, testing_set))*100)
##print ("Classification: ", voted_classifier.classify(testing_set[0][0]), "confidence %: " ,voted_classifier.confidence(testing_set[0][0])*100)
##print ("Classification: ", voted_classifier.classify(testing_set[1][0]), "confidence %: " ,voted_classifier.confidence(testing_set[1][0])*100)
##print ("Classification: ", voted_classifier.classify(testing_set[2][0]), "confidence %: " ,voted_classifier.confidence(testing_set[2][0])*100)
##print ("Classification: ", voted_classifier.classify(testing_set[3][0]), "confidence %: " ,voted_classifier.confidence(testing_set[3][0])*100)
##print ("Classification: ", voted_classifier.classify(testing_set[4][0]), "confidence %: " ,voted_classifier.confidence(testing_set[4][0])*100)
##print ("Classification: ", voted_classifier.classify(testing_set[5][0]), "confidence %: " ,voted_classifier.confidence(testing_set[5][0])*100)
##
#
#..................