forked from arjunkalburgi/nltktutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutprt19.py
157 lines (114 loc) · 5.18 KB
/
tutprt19.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
'''
Text Classifier: An algorithm that is able to classify a text based on it's contents.
Classic example, an email as spam or not - applies to two distinct things.
Sentiment Analysis - positive or negative connotation.
'''
import nltk
import random
from nltk.tokenize import word_tokenize
import pickle
from nltk.classify.scikitlearn import SklearnClassifier
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
class VoteClassifier(ClassifierI):
"""docstring for VoteClassifier"""
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
def find_features(document):
''' Function returns a dictionary of all the words in the top 5000 and if they're
in the document (passed in) '''
words = word_tokenize(document)
features = {}
for w in word_features:
features[w] = (w in words)
return features
##########################################################################
short_pos = open("short_reviews/newpositive.txt", "r").read()
short_neg = open("short_reviews/newnegative.txt", "r").read()
documents = []
for r in short_pos.split('\n'):
documents.append((r, "pos"))
for r in short_neg.split('\n'):
documents.append((r, "neg"))
# documents_pickle = open("pickled_algorithms/documents.pickle", "wb")
# pickle.dump(documents, documents_pickle)
# documents_pickle.close()
##########################################################################
all_words_pickle = open("pickled_algorithms/all_words.pickle", "rb")
all_words = pickle.load(all_words_pickle)
all_words_pickle.close()
print("all_words added")
all_words = nltk.FreqDist(all_words)
##########################################################################
word_features_pickle = open("pickled_algorithms/word_features.pickle", "rb")
word_features = pickle.load(word_features_pickle)
word_features_pickle.close()
print("word_features added")
##########################################################################
featuresets_pickle = open("pickled_algorithms/featuresets.pickle", "rb")
featuresets = pickle.load(featuresets_pickle)
featuresets_pickle.close()
print("featuresets added")
random.shuffle(featuresets)
training_set = featuresets[:10000]
testing_set = featuresets[10000:]
##########################################################################
classifier_pickle = open("pickled_algorithms/classifier.pickle", "rb")
classifier = pickle.load(classifier_pickle)
classifier_pickle.close()
print("classifier added")
print("Original Naive Bayes Algorithm accuracy %: ",
(nltk.classify.accuracy(classifier, testing_set)) * 100)
classifier.show_most_informative_features(15)
##########################################################################
MNB_classifier_pickle = open("pickled_algorithms/MNB_classifier.pickle", "rb")
MNB_classifier = pickle.load(MNB_classifier_pickle)
MNB_classifier_pickle.close()
print("MNB_classifier added")
print("Multinomial Naive Bayes Algorithm accuracy %:",
(nltk.classify.accuracy(MNB_classifier, testing_set)) * 100)
##########################################################################
BNB_classifier_pickle = open("pickled_algorithms/BNB_classifier.pickle", "rb")
BNB_classifier = pickle.load(BNB_classifier_pickle)
BNB_classifier_pickle.close()
print("BNB_classifier added")
print("Bernoulli Naive Bayes Algorithm accuracy %: ",
(nltk.classify.accuracy(BNB_classifier, testing_set)) * 100)
##########################################################################
LogisticRegressin_pickle = open(
"pickled_algorithms/LogisticRegressin.pickle", "rb")
LogisticRegression_classifier = pickle.load(LogisticRegressin_pickle)
LogisticRegressin_pickle.close()
print("LogisticRegression_classifier added")
print("LogisticRegression Algorithm accuracy %: ",
(nltk.classify.accuracy(LogisticRegression_classifier, testing_set)) * 100)
##########################################################################
LinearSVC_classifier_pickle = open(
"pickled_algorithms/LinearSVC_classifier.pickle", "rb")
LinearSVC_classifier = pickle.load(LinearSVC_classifier_pickle)
LinearSVC_classifier_pickle.close()
print("LinearSVC_classifier added")
print("LinearSVC Algorithm accuracy %: ",
(nltk.classify.accuracy(LinearSVC_classifier, testing_set)) * 100)
##########################################################################
voted_classifier = VoteClassifier(
classifier, LinearSVC_classifier, MNB_classifier, BNB_classifier, LogisticRegression_classifier)
print("VoteClassifier Algorithm accuracy %: ",
(nltk.classify.accuracy(voted_classifier, testing_set)) * 100)