forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_file.py
373 lines (324 loc) · 9.4 KB
/
predict_file.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Copyright 2017 Dhvani Patel
import sys
from keras.models import Sequential
from keras.layers import Dense, Dropout, Input, Embedding, LSTM
from keras.models import Model
from keras import optimizers
from keras.callbacks import ModelCheckpoint, CSVLogger, EarlyStopping
from keras.models import model_from_yaml
from Token import Token
from compile_error import CompileError
import tokenize
import token
import StringIO
import keyword
import json
import numpy as np
from numpy import newaxis
global all_tokens
global indexed_tokens
global check_tokens
START_TOKEN = '<s>'
END_TOKEN = '</s>'
BATCH_SIZE = 66
def one_hot(indexed_tokens):
one_hot = []
nb_classes = 88
one_hot_targets = np.eye(nb_classes)[indexed_tokens]
one_hot = one_hot_targets.tolist()
#print "fort"
#bruhTemp = one_hot[:]
for x in range(len(one_hot)):
#one_hot[x].astype(int)
[int(i) for i in one_hot[x]]
#one_hot.astype(int)
#print type(one_hot[0][0])
return one_hot
def set_from_json(all_tokens, flag):
#print "OMG"
with open('vocabulary.json') as data_file:
data = json.load(data_file)
#pprint(data)
#print len(data["indexes"])
#print "DHADHA"
tempT = all_tokens[:]
for token in tempT:
if len(token.value) > 0:
if token.value[0] == '#':
all_tokens.remove(token)
#print "no way"
for token in all_tokens:
if token.value == "\\r\\n":
token.value = "NEWLINE"
toCompare = token.value
#print token.type
#print "Broke..."
#print token.line
global indexed_tokens
indexed_tokens.append(data["indexes"].index(toCompare))
for r in range(9):
indexed_tokens.insert(r, data["indexes"].index(START_TOKEN))
indexed_tokens.append(data["indexes"].index(END_TOKEN))
#print indexed_tokens
return one_hot(indexed_tokens)
def open_closed_tokens(token):
"""
'Flattens' Python into tokens based on whether the token is open or
closed.
"""
# List of token names that whose text should be used verbatim as the type.
VERBATIM_CLASSES = {
"AMPER", "AMPEREQUAL", "ASYNC", "AT", "ATEQUAL", "AWAIT", "CIRCUMFLEX",
"CIRCUMFLEXEQUAL", "COLON", "COMMA", "DOT", "DOUBLESLASH",
"DOUBLESLASHEQUAL", "DOUBLESTAR", "DOUBLESTAREQUAL", "ELLIPSIS",
"EQEQUAL", "EQUAL", "GREATER", "GREATEREQUAL", "LBRACE", "LEFTSHIFT",
"LEFTSHIFTEQUAL", "LESS", "LESSEQUAL", "LPAR", "LSQB", "MINEQUAL",
"MINUS", "NOTEQUAL", "OP", "PERCENT", "PERCENTEQUAL", "PLUS", "PLUSEQUAL",
"RARROW", "RBRACE", "RIGHTSHIFT", "RIGHTSHIFTEQUAL", "RPAR", "RSQB",
"SEMI", "SLASH", "SLASHEQUAL", "STAR", "STAREQUAL", "TILDE", "VBAR",
"VBAREQUAL"
}
OTHER = { "NEWLINE", "INDENT", "DEDENT"}
CHECK = {"None", "True", "False"}
if token.type == 'NAME':
# Special case for NAMES, because they can also be keywords.
if keyword.iskeyword(token.value):
return token.value
elif token.value in CHECK:
return token.value
else:
return '<IDENTIFIER>'
elif token.type in VERBATIM_CLASSES:
# These tokens should be mapped verbatim to their names.
assert ' ' not in token.value
return token.value
elif token.type in {'NUMBER', 'STRING'}:
# These tokens should be abstracted.
# Use the <ANGLE-BRACKET> notation to signify these classes.
return "<" + token.type.upper() + ">"
elif token.type in OTHER:
return token.type
else:
# Use these token's name verbatim.
# assert token.type in {
# 'NEWLINE', 'INDENT', 'DEDENT',
# 'ENDMARKER', 'ENCODING', 'COMMENT', 'NL', 'ERRORTOKEN'
#}
return token.value
def vocabularize_tokens(every_token, flag):
if flag == False:
EXTRANEOUS_TOKENS = {
# Always occurs as the first token: internally indicates the file
# ecoding, but is irrelelvant once the stream is already tokenized
'ENCODING',
# Always occurs as the last token.
'ENDMARKER',
# Insignificant newline; not to be confused with NEWLINE
'NL',
# Discard comments
'COMMENT',
# Represents a tokenization error. This should never appear for
# syntatically correct files.
'ERRORTOKEN',
}
elif flag == True:
EXTRANEOUS_TOKENS = {
# Always occurs as the first token: internally indicates the file
# ecoding, but is irrelelvant once the stream is already tokenized
'ENCODING',
# Always occurs as the last token.
'ENDMARKER',
# Discard comments
'COMMENT',
# Represents a tokenization error. This should never appear for
# syntatically correct files.
'ERRORTOKEN',
}
all_tokens_iter = every_token[:]
for Token in all_tokens_iter:
vocab_entry = open_closed_tokens(Token)
Token.value = vocab_entry
if Token.type in EXTRANEOUS_TOKENS:
every_token.remove(Token)
if flag == True:
if Token.value == "\\n":
every_token.remove(Token)
#if Token.type == "NL":
#print "Gotch u"
#for Token in every_token:
#print Token.value
return set_from_json(every_token, flag)
# Create list of tokens
def handle_token(type, token, (srow, scol), (erow, ecol), line):
if repr(token)[:2] == 'u\'':
val = repr(token)[2:len(repr(token))-1]
else:
val = repr(token)[1:len(repr(token))-1]
send = Token(tokenize.tok_name[type], val, srow, scol, erow, ecol, line)
global all_tokens
all_tokens.append(send)
print "%d,%d-%d,%d:\t%s\t%s" % \
(srow, scol, erow, ecol, tokenize.tok_name[type], repr(token))
def getFileTokens(fileName):
with open(fileName, 'r') as myfile:
data=myfile.read()
print data
global all_tokens
all_tokens = []
global indexed_tokens
indexed_tokens = []
try:
tokenStream = tokenize.tokenize(StringIO.StringIO(data).readline, handle_token)
except tokenize.TokenError:
pass
one_hot_file = vocabularize_tokens(all_tokens, False)
global check_tokens
check_tokens = []
print len(one_hot_file)
print "GOTCH U"
windowInd = 0
loopInd = 0
batchArr = []
while True:
print "here"
loopInd = 0
batchArr = []
while loopInd < (BATCH_SIZE):
print windowInd+1
print "window"
if windowInd <= int((int(len(one_hot_file)) - 10)):
toPass = []
for x in range(10):
y = x + windowInd
#print y
#print len(one_hot_file)
toPass.append(one_hot_file[y])
assert len(toPass) > 0
a = np.array(toPass).astype(int)
#b = a[newaxis, :]
#print b
check_tokens.append(toPass)
#print len(a[1])
assert a.shape == (10,88)
print a
batchArr.append(a)
windowInd += 1
loopInd += 1
else:
toPassEnd = []
for x in range(10):
giveEnd = []
giveEnd = [0] * 88
giveEnd[87] = 1
toPassEnd.append(giveEnd)
#print giveEnd
batchArr.append(np.array(toPassEnd).astype(int))
loopInd += 1
b = np.array(batchArr)
print b.shape
yield b
#print "WINDOW"
#print windowInd
print "done radha"
#print len(one_hot_file)
def predict(fileName):
# load YAML and create model
yaml_file = open('model_l.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights into new model
loaded_model.load_weights("model_l.h5")
print("Loaded model from disk")
# evaluate loaded model on test data
opt = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.5)
loaded_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
'''
gen = getFileTokens(fileName)
arrs = []
for x in gen:
arrs.append(x)
print len(arrs)
print type(radha)
'''
outPredict = loaded_model.predict_generator(getFileTokens(fileName), 4, 1, verbose=1)
print "HERE"
print outPredict
print len(outPredict)
#print type(radha)
inds = []
for x in range(len(list(outPredict))):
inds.append(list(outPredict[x]).index(max(outPredict[x])))
print max(outPredict[0])
print list(outPredict[0]).index(max(outPredict[0]))
print "MAX"
countGood = 0
countIns = 0
countDel = 0
countWhat = 0
iterInd = 0
print inds
print zip(*(iter(inds),) * 10)
global check_tokens
check = check_tokens[:]
print len(check)
#print check[225]
iterInd = 0
with open('vocabulary.json') as data_file:
data = json.load(data_file)
print "-------------------------------"
for window in check:
errType = inds[iterInd]
if errType == 0:
msg = "NO ERROR: "
countGood += 1
elif errType == 2:
msg = "DELETION: "
countDel += 1
elif errType == 3:
msg = "INSERTION: "
countIns += 1
else:
msg = "IDEK: "
countWhat += 1
errLine = ""
for toks in window:
getInd = toks.index(1.0)
actualToken = data["indexes"][getInd]
errLine = errLine + ' ' + actualToken
print msg + errLine
#print c
#print type(radha)
iterInd += 1
print "-------------------------------"
print countGood
print countDel
print countIns
print countWhat
sys.exit()
#print type(radha)
for b in inds:
#if iterInd == 3:
# iterInd = 0
#if iterInd == 0:
if b == 0:
countGood += 1
if b == 1:
countWhat += 1
#if iterInd == 1:
if b == 3:
countIns += 1
#if iterInd == 2:
if b == 2:
countDel += 1
#print b
iterInd += 1
#print b
print len(inds)
print countGood
print countIns
print countDel
print countWhat
if __name__ == '__main__':
fileName = sys.argv[1]
predict(fileName)