-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_test.py
58 lines (42 loc) · 1.42 KB
/
run_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
import os
import argparse
import json
from time import time
from tqdm import tqdm
from glob import glob
import code_graph as cg
def load_examples(files):
for n, file in enumerate(files):
name = os.path.basename(file)
desc = "File %d / %d: %s" % (n+1, len(files), name)
total = sum(1 for _ in open(file, "r"))
with open(file, "r") as lines:
for line in tqdm(lines, total = total, desc = desc):
yield json.loads(line)
def tokens_to_text(tokens):
method_text = " ".join(tokens)
return "public class Test{\n%s\n}" % method_text
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input_dir")
parser.add_argument("result_file")
args = parser.parse_args()
if os.path.isfile(args.input_dir):
files = [args.input_dir]
else:
files = glob(os.path.join(args.input_dir, "*.jsonl"))
run_times = open(args.result_file, "w")
for example in load_examples(files):
tokens = example["tokens"]
length = len(tokens)
code = tokens_to_text(tokens)
start_time = time()
try:
graph = cg.codegraph(code, lang = "java", syntax_error = "raise")
except Exception as e:
print(e)
continue
end_time = time() - start_time
run_times.write(json.dumps([length, len(graph), end_time]) + "\n")
if __name__ == '__main__':
main()