-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapril9_test.py
111 lines (83 loc) · 3.13 KB
/
april9_test.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
def solve(new_file):
#from __future__ import print_function
import gensim
from gensim.corpora.dictionary import Dictionary
import os
import codecs
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import re
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils.np_utils import to_categorical
from keras.layers import Dense, Input, Flatten, LSTM
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Model
import sys
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation,Flatten
from keras.layers.convolutional import Convolution2D,MaxPooling2D
from keras.optimizers import SGD,RMSprop
from keras.utils import np_utils
from sklearn.utils import shuffle
import h5py
import pandas as pd
word2vec = gensim.models.Word2Vec
def clean_data(data):
lemmatizer=WordNetLemmatizer()
word_list1=[]
word_list = data.split()
for i in range(len(word_list)):
word_list1.append(word_list[i].strip('<,>,\n'))
filtered_words=[word for word in word_list1 if word not in stopwords.words('english')]
#print("Filtered Words>>>>")
#print(filtered_words)
#filtered_words1=list(''.join(w for w in word_list1 if w not w.isdigit()))
#repeat_regexp = re.compile(r'(\w*)(\w)\2(\w*)')
#repl = r'\1\2\3'
mod_doc = []
mod_doc1=[]
for i in filtered_words:
#i = unicode(i,errors='ignore')
i.lower()
i.strip('#\'"?,.!,')
i.strip('<')
if '@' in i or 'http:' in i:
continue
j = re.sub(r'(.)\1+',r'\1\1',i)
mod_doc.append(lemmatizer.lemmatize(j))
#for k in mod_doc:
# mod_doc1.append(mod_doc[k].strip('\n'))
mod_doc1=' '.join(mod_doc)
return mod_doc1
from keras.models import model_from_json
model = model_from_json(open('model.json').read())
model.load_weights('model.h5')
from keras.layers.core import Activation
#model.summary()
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
import pickle
vectorizer=pickle.load(open(("feature.pkl"),'rb'))
label_encoder=pickle.load(open(("encoder.pkl"),'rb'))
from sklearn.metrics import accuracy_score
# print("Enter File name (.txt)")
# new_file=raw_input()
documents1=[]
with codecs.open(new_file, 'r', 'utf8') as f:
text1=f.read()
documents1.append(text1)
new_doc=[]
new_doc.append(clean_data(text1))
X1 = vectorizer.transform(new_doc).toarray()
#print(X1)
preds=model.predict(X1)
#print(model.predict(X1))
#print(model.predict_classes(X1))
class_ints = preds.argmax(axis=1)
predicted_classes = label_encoder.inverse_transform(class_ints)
#print(predicted_classes)
return predicted_classes
print("File name to classify:>>>>>")
print(solve(raw_input()))