-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse_model.py
207 lines (168 loc) · 5.42 KB
/
sparse_model.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
import nltk
from nltk.stem.lancaster import LancasterStemmer
import os
import json
import datetime
import nltk
from os import walk
import os
import codecs
stemmer = LancasterStemmer()
def bets(add):
with codecs.open(add , 'r' , "utf-8") as f:
pol = f.readlines()
return pol
def trainin_data(texps,add,nam):
training = []
with codecs.open(add , 'r' , "utf-8") as f:
pol = f.readlines()
for i, l in enumerate(texps):
for ki in pol:
if l.split() == ki.split():
#print l
training.append({"class":os.path.splitext(nam)[0] ,"sentence":texps[i]})
return training
bek =[]
path = '/home/rajesh/Desktop/bridges'
for (dirpath, dirnames, filenames) in walk(path):
for name in filenames:
if name.endswith((".txt")):
bek.extend(bets(dirpath+'/'+name))
'''print "filename"+name
for l in bek:
print l.decode("utf-8").encode("ascii").strip()'''
kil = []
for it in bek:
kil.append(it.decode("utf-8").encode("ascii").strip())
hidn =[]
pathh = '/home/rajesh/Desktop/bridgehid'
for (dirpath, dirnames, filenames) in walk(pathh):
for name in filenames:
if name.endswith((".txt")):
hidn.extend(bets(dirpath+'/'+name))
hidnn = []
pathhh = '/home/rajesh/Desktop/bridgehid'
for (dirpath, dirnames, filenames) in walk(pathh):
for name in filenames:
if name.endswith((".txt")):
hidn.extend(bets(dirpath+'/'+name))
wil = []
for it in hidn:
wil.append(it.decode("utf-8").encode("ascii").strip())
qil = []
for it in hidnn:
qil.append(it.decode("utf-8").encode("ascii").strip())
training_data=[]
for (dirpath, dirnames, filenames) in walk(path):
for name in filenames:
if name.endswith((".txt")):
training_data.extend(trainin_data(kil,dirpath+'/'+name,name))
hiden_la=[]
for (dirpath, dirnames, filenames) in walk(pathh):
for name in filenames:
if name.endswith((".txt")):
hiden_la.extend(trainin_data(wil,dirpath+'/'+name,name))
hiden_la2=[]
for (dirpath, dirnames, filenames) in walk(pathhh):
for name in filenames:
if name.endswith((".txt")):
hiden_la2.extend(trainin_data(wil,dirpath+'/'+name,name))
words = []
hidwords = []
hidwords2 = []
classes = []
documents = []
hiddocuments = []
hiddocuments2= []
ignore_words = ['?']
# loop through each sentence in our training data
for pattern in training_data:
# tokenize each word in the sentence
w = nltk.word_tokenize(pattern['sentence'])
# add to our words list
words.extend(w)
# add to documents in our corpus
documents.append((w, pattern['class']))
# add to our classes list
if pattern['class'] not in classes:
classes.append(pattern['class'])
for pattern in hiden_la:
w = nltk.word_tokenize(pattern['sentence'])
hidwords.extend(w)
hiddocuments.append((w, pattern['class']))
if pattern['class'] not in classes:
classes.append(pattern['class'])
for pattern in hiden_la2:
w = nltk.word_tokenize(pattern['sentence'])
hidwords2.extend(w)
hiddocuments2.append((w, pattern['class']))
if pattern['class'] not in classes:
classes.append(pattern['class'])
# stem and lower each word and remove duplicates
words = [stemmer.stem(w.lower()) for w in words if w not in ignore_words]
words = list(set(words))
hidwords = [stemmer.stem(w.lower()) for w in hidwords if w not in ignore_words]
hidwords = list(set(hidwords))
hidwords2 = [stemmer.stem(w.lower()) for w in hidwords2 if w not in ignore_words]
hidwords2 = list(set(hidwords2))
# remove duplicates
classes = list(set(classes))
print (len(documents), "documents")
print (len(classes), "classes", classes)
print (len(words), "unique stemmed words", words)
# create our training data
training = []
hidden1 = []
hidden2 = []
output = []
# create an empty array for our output
output_empty = [0] * len(classes)
# training set, bag of words for each sentence
for doc in documents:
# initialize our bag of words
bag = []
# list of tokenized words for the pattern
pattern_words = doc[0]
# stem each word
pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
# create our bag of words array
for w in words:
bag.append(1) if w in pattern_words else bag.append(0)
training.append(bag)
# output is a '0' for each tag and '1' for current tag
for doc in hiddocuments:
hidbag = []
pattern_words = doc[0]
# stem each word
pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
for w in words:
hidbag.append(1) if w in pattern_words else hidbag.append(0)
hidden1.append(hidbag)
for doc in hiddocuments2:
hidbag2 =[]
pattern_words = doc[0]
pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
for w in words:
hidbag2.append(1) if w in pattern_words else hidbag2.append(0)
hidden2.append(hidbag2)
output_row = list(output_empty)
output_row[classes.index(doc[1])] = 1
output.append(output_row)
# sample training/output
i = 0
w = documents[i][0]
print ([stemmer.stem(word.lower()) for word in w])
print (training[i])
print (output[i])
print (hidden1[0])
print len(hidden1)
print len(hidden2)
print hiddocuments2
'''for doc in hiddocuments2:
hidbag = []
pattern_words = doc[0]
# stem each word
pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
for w in words:
hidbag.append(1) if w in pattern_words else hidbag.append(0)
hidden2.append(hidbag)'''