Uh oh!
There was an error while loading. Please reload this page.
forked from tech-srl/code2vec
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_predict.py
More file actions
Latest commit
69 lines (60 loc) · 2.8 KB
/
Copy pathinteractive_predict.py
File metadata and controls
69 lines (60 loc) · 2.8 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
importtraceback
fromcommonimportcommon
frompython_extractor.extractorimportExtractor
SHOW_TOP_CONTEXTS=10
MAX_PATH_LENGTH=8
MAX_PATH_WIDTH=2
classInteractivePredictor:
exit_keywords= ['exit', 'quit', 'q']
def__init__(self, config, model):
model.predict([])
self.model=model
self.config=config
self.path_extractor=Extractor(max_path_length=MAX_PATH_LENGTH,
max_path_width=MAX_PATH_WIDTH)
defread_file(self, input_filename):
withopen(input_filename, 'r') asfile:
returnfile.readlines()
defpredict(self):
# input_filename = 'Input.java'
input_filename='input.py'
print('Starting interactive prediction...')
whileTrue:
print(
'Modify the file: "%s" and press any key when ready, or "q" / "quit" / "exit" to exit'%input_filename)
user_input=input()
ifuser_input.lower() inself.exit_keywords:
print('Exiting...')
return
try:
predict_lines=list(path.strip() forpathinself.path_extractor.extract_paths(input_filename))
contexts=predict_lines[0].split()
space_padding=' '* (self.config.MAX_CONTEXTS-len(contexts) +1)
predict_lines[0] =' '.join(contexts) +space_padding
print(predict_lines)
exceptValueErrorase:
print(e)
continue
hash_to_string_dict=UnitDict()
try:
raw_prediction_results=self.model.predict(predict_lines)
exceptExceptionasexc:
print(exc)
continue
method_prediction_results=common.parse_prediction_results(
raw_prediction_results, hash_to_string_dict,
self.model.vocabs.target_vocab.special_words, topk=SHOW_TOP_CONTEXTS)
forraw_prediction, method_predictioninzip(raw_prediction_results, method_prediction_results):
print('Original name:\t'+method_prediction.original_name)
forname_prob_pairinmethod_prediction.predictions:
print('\t(%f) predicted: %s'% (name_prob_pair['probability'], name_prob_pair['name']))
print('Attention:')
forattention_objinmethod_prediction.attention_paths:
print('%f\tcontext: %s,%s,%s'% (
attention_obj['score'], attention_obj['token1'], attention_obj['path'], attention_obj['token2']))
ifself.config.EXPORT_CODE_VECTORS:
print('Code vector:')
print(' '.join(map(str, raw_prediction.code_vector)))
classUnitDict(dict):
def__getitem__(self, key):
returnkey