Repository navigation
Expand file tree
/
Copy pathnlp+ocr (1).py
More file actions
245 lines (117 loc) · 3.28 KB
/
Copy pathnlp+ocr (1).py
File metadata and controls
245 lines (117 loc) · 3.28 KB
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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import nltk
import random
import pickle
from nltk.tokenize import word_tokenize
import re
import os
from nltk.classify.scikitlearn import SklearnClassifier
# In[3]:
data = pd.read_csv("C:\\Users\\Keshav Rao\\Desktop\\Keshav\\Kaam\\cpp_tasks\\nlp+ocr\\data.csv")
# In[4]:
data.info()
# In[ ]:
# In[5]:
all_words = []
from nltk.corpus import stopwords
nltk.download('stopwords')
import re
stop_words = list(set(stopwords.words('english')))
allowed_word_types = ["J"]
# In[6]:
from nltk.corpus import stopwords
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
#nltk.download('all')
from nltk import sent_tokenize
for t in data.review:
clean = re.sub(r'[^(a-zA-Z)\s]','',t)
tokenized = word_tokenize(clean)
stop = [w for w in tokenized if not w in stop_words]
docs = nltk.pos_tag(stop)
for w in docs:
if w[1][0] in allowed_word_types:
all_words.append(w[0].lower())
# In[11]:
words_freq = nltk.FreqDist(all_words)
type(words_freq)
# In[9]:
words_freq.plot(30, cumulative = False)
plt.show()
# In[13]:
print (len(all_words))
# In[14]:
words_freq.values()
# In[15]:
words_most = list(words_freq.keys())[:1000]
words_most
# In[ ]:
# In[16]:
documents = []
for r,s in zip(data.review, data.sentiment):
documents.append((r,s))
documents
# In[17]:
def rev_features (document):
words = word_tokenize(document)
features = {}
for w in words_most:
features[w] = (w in words)
return features
sets_features = [(rev_features(r), s) for (r,s) in documents]
random.shuffle(sets_features)
# In[18]:
sets_features[1]
# In[31]:
x = [x[0] for x in sets_features]
y = [y[1] for y in sets_features]
df = pd.DataFrame(list(zip(x,y)), columns = ['features', 'sent'])
# In[ ]:
new_doc = ' worst movie'
features_new = rev_features(new_doc)
model2.
# In[23]:
train = sets_features[:700]
test = sets_features[700:]
# In[34]:
model0 = nltk.NaiveBayesClassifier.train(train)
print("acc:", (nltk.classify.accuracy(model0, test)))
model0.show_most_informative_features(10)
# In[35]:
model0.predict(features_new)
# In[31]:
from sklearn.naive_bayes import MultinomialNB
model2 = SklearnClassifier(MultinomialNB())
model2 = model2.train(train)
print("acc:", (nltk.classify.accuracy(model2, test)))
# In[60]:
model3 = MultinomialNB()
#x1 = x.reshape(-1,1)
for i,j in zip(x,y):
model3.fit(i, j)
# In[41]:
from sklearn.naive_bayes import MultinomialNB
model2 = SklearnClassifier(MultinomialNB())
model2 = model2.train(sets_features)
# In[40]:
new_doc = ' worst movie'
features_new = rev_features(new_doc)
model2.predict(X_test)
# In[39]:
print(model2.predict(features_new))
# In[33]:
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.linear_model import LogisticRegression
model1 = SklearnClassifier(LogisticRegression())
model1 = model1.train(train)
print("acc:", (nltk.classify.accuracy(model1, test)))
# In[32]:
from sklearn.ensemble import RandomForestClassifier
model5 = RandomForestClassifier(n_estimators=100,bootstrap = True,max_features = 'sqrt')
model5.fit(df.features, df.sent)