-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntial Draft
312 lines (280 loc) · 7.5 KB
/
Intial Draft
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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import nltk, re, pprint
from nltk import word_tokenize
from urllib import request
from nltk import sent_tokenize
from nltk.corpus import stopwords
import numpy as np
import random
##url1 Benjamin Franklin Autobiography
url = "http://www.gutenberg.org/cache/epub/148/pg148.txt"
response = request.urlopen(url)
raw = response.read().decode('utf8')
raw1=raw.rstrip()
raw1=raw1.lower()
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(raw1)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
words = [word for word in filtered_sentence if word.isalpha()]
from nltk.tokenize.treebank import TreebankWordDetokenizer as Detok
detokenizer = Detok()
words2=detokenizer.detokenize(words)
x1=[]
#x=0
#i=0
#n=150
#while x<200:
# space=""
# words3=words2.split(" ")[i:n]
# for word in words3:
# space=word+" "+space
# space=space+',A'
# x1.append(space)
# i+=150
# n+=150
# x+=1
for i in range (0,200*150,150):
x1.append(words[i:i+150])
a=[0]*200
##url2 ALice's Adeventures in wonderland
url1 = "http://www.gutenberg.org/cache/epub/345/pg345.txt"
response = request.urlopen(url1)
law = response.read().decode('utf8')
law1=law.rstrip()
law1=law1.lower()
stop_words1 = set(stopwords.words('english'))
word_tokens1 = word_tokenize(law1)
filtered_sentence1 = [w for w in word_tokens1 if not w in stop_words1]
filtered_sentence1 = []
for w in word_tokens1:
if w not in stop_words1:
filtered_sentence1.append(w)
wordsAlice = [word for word in filtered_sentence1 if word.isalpha()]
detokenizer1 = Detok()
wordsAlice1=detokenizer1.detokenize(wordsAlice)
x2=[]
#x=0
#i=0
#n=150
#while x<200:
# space1=""
# wordsAlice2=wordsAlice1.split(" ")[i:n]
# for word in wordsAlice2:
# space1=word+" "+space1
# space1=space1
# x2.append(space1)
# i+=150
# n+=150
# x+=1
for i in range (0,200*150,150):
x2.append(words[i:i+150])
b=[1]*200
#print(x2)
#url3
url2="http://www.gutenberg.org/cache/epub/852/pg852.txt"
response = request.urlopen(url2)
saw = response.read().decode('utf8')
saw1=saw.rstrip()
saw1=saw1.lower()
stop_words2 = set(stopwords.words('english'))
word_tokens2 = word_tokenize(saw1)
filtered_sentence2 = [w for w in word_tokens2 if not w in stop_words2]
filtered_sentence2 = []
for w in word_tokens2:
if w not in stop_words2:
filtered_sentence2.append(w)
wordsDemo = [word for word in filtered_sentence2 if word.isalpha()]
detokenizer2 = Detok()
wordsDemo1=detokenizer2.detokenize(wordsDemo)
x3=[]
#x=0
#i=0
#n=150
#while x<200:
# space2=""
# wordsDemo2=wordsDemo1.split(" ")[i:n]
# for word in wordsDemo2:
# space2=word+" "+space2
# space2=space2
# x3.append(space2)
# i+=150
# n+=150
# x+=1
for i in range (0,200*150,150):
x3.append(words[i:i+150])
c=[2]*200
#print(x3)
#Merging Books and Labels
Merge=x1+x2+x3
#Merge=[" ".join([str(c) for c in lst]) for lst in Merge]
import pandas as pd
#Merge=pd.DataFrame(Merge)
Documents=np.asarray(Merge)
Label=a+b+c
Label=np.asarray(Label)
#Label=pd.DataFrame(Label)
##print(Label.shape)
##print(Merge.shape)
DocMerge=np.append(Documents,Label[:,None],axis=1)
#DocMerge=pd.concat([Merge,Label],axis=1)
#DocMerge=DocMerge.sample(frac=1)
#lsplit=DocMerge.iloc[:,-1].values
#Dsplit=DocMerge.iloc[:,0:1].values
#DiffMerge=DocMerge[:,-1:]
#print(DiffMerge)
import random
random.shuffle(DocMerge)
AfterLabel=DocMerge[:,-1:]
AfterDoc=DocMerge[:,:150]
#AfterDoc=[" ".join([str(c) for c in lst]) for lst in AfterDoc]
#Dsplit=[" ".join(c) for c in Dsplit]
#
##converting to dataframe
df=pd.DataFrame(AfterDoc)
#print(df)
y=pd.DataFrame(AfterLabel)
#print(y.shape)
df=[" ".join(c) for c in df.values]
df=pd.DataFrame(df)
#print(df.shape)
#df=pd.DataFrame(df)
#y=[" ".join(c) for c in y.values]
#print(a.shape)
#merge=np.append(x1,a)
#print(merge[:-1])
#print(merge.shape)
#BookMerge=x1+x2+x3
#BookMerge=np.asarray(BookMerge)
#print(BookMerge.shape)
#LabelMerge=a+b+c
#LabelMerge=np.asarray(LabelMerge)
#Merge=np.append(BookMerge,LabelMerge)
#print(Merge[:, -1:])
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
#count_vect = CountVectorizer()
#X_train_counts = (count_vect.fit_transform(df[0]))
#X=X_train_counts.toarray()
#print(X)
#print(X_train)
#X=X_train_counts.toarray()
#print(X_train_counts)
##X=X.reshape(1,-1)
##y_train=y_train.reshape(-1,1)
#Bagofwords
test_bag=['Benjamin',
'Dracula']
np_bag=np.array(test_bag)
np_bag1=pd.DataFrame(np_bag)
count_vect = CountVectorizer()
X_train_counts = (count_vect.fit_transform(df[0]))
bagtest=(count_vect.transform(np_bag1[0]))
print(bagtest)
X_train, X_test, y_train, y_test = train_test_split(X_train_counts,y,test_size=.10 )
clf = MultinomialNB().fit(X_train, y_train)
predicted=clf.predict(bagtest)
print(predicted)
#print(accuracy_score(y_test,predicted))
#
##k neighbour BOW
#from sklearn.neighbors import KNeighborsClassifier
#classifier = KNeighborsClassifier(n_neighbors=5)
#cl=classifier.fit(X_train, y_train)
#predict=cl.predict(X_test)
#print(accuracy_score(y_test,predict))
#TFIDF
vectorizer=TfidfVectorizer()
X=vectorizer.fit_transform(df[0])
test_arr=[' ',
'Dracula']
np_tarr=np.array(test_arr)
#print(type(np_tarr))
#print(np_tarr.shape)
np_tarr1=pd.DataFrame(np_tarr)
x1=vectorizer.transform(np_tarr1[0])
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X)
x2 = tfidf_transformer.fit_transform(x1)
#print(x2)
#X_test=x21
X_train, X_test, y_train, y_test = train_test_split(X_train_tfidf,y,test_size=.20 )
clf = MultinomialNB().fit(X_train, y_train)
predicted=clf.predict(X_test)
#print(predicted)
#print(accuracy_score(y_test,predicted))
#print(predicted)
##TFIDF neighbour
#from sklearn.neighbors import KNeighborsClassifier
#classifier = KNeighborsClassifier(n_neighbors=5)
#c1=classifier.fit(X_train, y_train)
#predic=c1.predict(X_test)
#print(accuracy_score(y_test,predic))
##model=svm.SVC(kernel='rbf',C=1,gamma=0.1) model.fit(train_X, train_Y) prediction1=model.predict(test_X) print('Accuracy for rbf SVM is ', metrics.accuracy_score(prediction1,test_Y))
#
##print(type(LabelMerge))
##print(LabelMerge)
## print(len(words))
##words=sent_tokenize(words)
#print(words)
#detoken=MosesDetokenizer('en')
#words1=detoken(words)
#print(words)
#print(type(words1))
#import re
#print(type(words2))
#x1=x1.append(a)
#print(x1)
#clf = MultinomialNB().fit(X_train_tfidf, y_train)
# for i in x1:
# print(i[:151])
#x1=[]
#for i in range (0,200*150,150):
# x1.append(words[i:i+150])
## x1=' '.join(word[i] for word in x1)
##print(type(x1))
##for i in range (0,200*150,150):
#for i in range(0,200*150,150):
# x1[i]=' '.join(word[i:i+150] for word in x1)
#
#print(x1)
#from numpy import array
#a = array(words2)
#print(a)
#print(type(a))
#print(words2)
#str=' '.join(words2)
#print()
#print(type(str))
#print(words1)
#print(type(words1))
#i=0
#while i<10:
# sentence1=' '.join(words2)
#
# i=i+1
#
#print(sentence1)
#i=0
#while j<150:
# r=words1[:]
#while i<200:
# j=150
# r=words1[:j]
# r1=detokenizer.detokenize(r)
# print(r1)
# i=i+1
# j=j+150
#print(type(r1)