forked from dirtyharrycallahan/pystrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDistanceFromStand.py
More file actions
50 lines (44 loc) · 1.47 KB
/
getDistanceFromStand.py
File metadata and controls
50 lines (44 loc) · 1.47 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
'''
get distance among strings of self
usage: python getDistanceFromStand.py stand_dir unpred_dir c
stand_dir/: the sub folder of the str files (standard)
unpre_dir/: ditto but unpred
c: the cth line of the str file as the reference [0-9)
output e.g. disdir/0_0/test-case.dis (contains str for 10 tests)
'''
import sys
import os
import editdistance
import numpy as np
import pickle
def Start(stand_dir, unpred_dir, c):
for rt, drs, strfs in os.walk(unpred_dir):
for strf_unp in strfs:
unpred_src = os.path.join(rt, strf_unp)
subdir = unpred_src.split('/')[-2] + '/'
# print subdir # 0_0/ stand/
if "stand" in subdir:
continue
if strf_unp[-4:] == ".str":
# print unpred_src # strdir/0_0/xxx_0.str
strf_std = stand_dir + strf_unp[:-6] + ".str"
# print strf_std # strdir/stand/xxx.str
with open(strf_std,'r') as str_fd, open(unpred_src, 'r') as str_unp_fd:
ref = str_fd.readlines()[int(c)]
comp = str_unp_fd.readline()
try:
dis = editdistance.eval(ref, comp)
ratio = 1- dis/float(max(len(ref), len(comp)))
# print str(ratio)
except:
pass
dest_file = "/home/ruimin/pystrace/disdir/" + subdir + strf_unp[:-6] + ".dis"
dest_file_rt = "/home/ruimin/pystrace/ratiodir/" + subdir + strf_unp[:-6] + ".rt"
# print dest_file
with open(dest_file_rt,'ab') as f:
f.write(str(ratio) + '\n')
#
# Entry point to the application
#
if __name__ == '__main__':
Start(sys.argv[1], sys.argv[2], sys.argv[3])