-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_ahead_V0.py
More file actions
144 lines (117 loc) · 4.83 KB
/
java_ahead_V0.py
File metadata and controls
144 lines (117 loc) · 4.83 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
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
import numpy as np
from soupsieve import select
from tree_sitter import Language, Parser
from java_preprocessing import read_source_files
from utils import remove_comments_and_docstrings, tree_to_token_index, index_to_code_token, tree_to_variable_index, tree_to_token_index_pro
import itertools
import networkx as nx
import datetime
import re
from DFG import DFG_java
dfg_function={
# 'python':DFG_python,
'java':DFG_java,
# 'ruby':DFG_ruby,
# 'go':DFG_go,
# 'php':DFG_php,
# 'javascript':DFG_javascript
}
parsers={}
for lang in dfg_function:
LANGUAGE = Language('build/yk-languages.so', lang)
parser = Parser()
parser.set_language(LANGUAGE)
parser = [parser,dfg_function[lang]]
parsers[lang]= parser
def save_matrix_npy():
dfg_function = {
# 'python': DFG_python,
'java':DFG_java,
# 'ruby':DFG_ruby,
# 'go':DFG_go,
# 'php':DFG_php,
# 'javascript':DFG_javascript
}
parsers = {}
for lang in dfg_function:
LANGUAGE = Language('build/yk-languages.so', lang)
parser = Parser()
parser.set_language(LANGUAGE)
parser = [parser, dfg_function[lang]]
parsers[lang] = parser
print("OK")
print("parser Ready !")
print(datetime.datetime.now())
dataset = read_source_files()
src_train = dataset['src_train']
src_dev = dataset['src_dev']
src_test = dataset['src_test']
tgt_train = dataset['tgt_train']
tgt_dev = dataset['tgt_dev']
tgt_test = dataset['tgt_test']
print(datetime.datetime.now())
# src_raw_train, src_raw_dev, src_raw_test
# src_raw_lsit = []
already = 0
for key in dataset.keys():
print()
print(len(dataset[key]), " {} cases are processing ... ".format(key))
st = datetime.datetime.now()
print(st)
dist_npy_file_dir = './python_0626/distances/'
mltv_npy_file_dir = './python_0626/adjacency/'
subtk_file_dir = './python_0626/'
string, guid = [], []
with open(subtk_file_dir + '{}.token.code'.format(key), 'w', encoding='utf8') as f:
with open(subtk_file_dir + '{}.token.guid'.format(key), 'w', encoding='utf8') as g:
for idx, src_raw in enumerate(dataset[key]):
uid = already + idx
# if idx == 50:
# break
if idx % 5000 == 0:
print(idx, end='| ')
# if idx % 3000 == 0:
# print(idx, end='| ')
# print(idx, end=" |")
code = src_raw.replace(' DCNL DCSP ', '\n\t'). \
replace(' DCNL DCSP ', '\n\t'). \
replace(' DCNL DCSP ', '\n\t'). \
replace(' DCNL ', '\n').replace(' DCSP ', '\t')
adj, mvm, distance_matrix_spllit, tokens_1 = generate_mv_matrix_v1(code, parser, True)
np.savez_compressed(mltv_npy_file_dir + '{}.npy'.format(uid), mvm)
np.savez_compressed(dist_npy_file_dir + '{}.npy'.format(uid), distance_matrix_spllit)
string.append(' '.join(tokens_1) + '\n')
guid.append(str(uid) + '\n')
f.writelines(string)
g.writelines(guid)
# t.append(datetime.datetime.now())
already += len(dataset[key])
# print(st)
# for tt in t:
# print(tt)
print("Done")
if __name__ == '__main__':
code = """
def sample():
a = random()
if a % 2 == 0:
b = a + 1
print(b)
"""
code = '''
def _tosequence(X):
if isinstance(X, Mapping):
return [X]
else:
return tosequence(X)
'''
print("OK")
#
#
code = """def filepath_to_uri(path): DCNL DCSP if (path is None): DCNL DCSP DCSP return path DCNL DCSP return urllib.quote(smart_str(path).replace('\\', '/'), safe="/~!*()'")"""
# code = """def url_filename(url): DCNL DCSP match = upload_title_re.match(url) DCNL DCSP if match: DCNL DCSP DCSP return match.group('filename') DCNL DCSP else: DCNL DCSP DCSP return url"""
# code = """def follow_link(connection, link): DCNL DCSP if link: DCNL DCSP DCSP return connection.follow_link(link) DCNL DCSP else: DCNL DCSP DCSP return None"""
# code = """def escape(s): DCNL DCSP if (s is None): DCNL DCSP DCSP return '' DCNL DCSP assert isinstance(s, basestring), ('expected DCSP %s DCSP but DCSP got DCSP %s; DCSP value=%s' % (basestring, type(s), s)) DCNL DCSP s = s.replace('\\', '\\\\') DCNL DCSP s = s.replace('\n', '\\n') DCNL DCSP s = s.replace(' DCTB ', '\\t') DCNL DCSP s = s.replace(',', ' DCTB ') DCNL DCSP return s"""
adj, mvm, distance_matrix_spllit, tokens_1 = generate_mv_matrix_v1(code, parser, True)
print()
save_matrix_npy()