-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutility.py
66 lines (55 loc) · 1.62 KB
/
utility.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
import os
def _bucket_count(li):
"""
utility function:
get bucket counts given a list
Return:
{x: count} for each x in the list
"""
bucket = {}
for x in li:
if x not in bucket:
bucket[x] = 0
bucket[x] += 1
return bucket
def _get_output_path(output_dir, output_name, suffix):
"""
utility function:
get output path
"""
if not os.path.exists(output_dir):
os.mkdir(output_dir)
output_path = os.path.join(
output_dir,
"{}_{}".format(output_name, suffix)
)
return output_path
def _txt2dict(path):
import collections.abc
def merge_dict(d1, d2):
for k, v in d2.items():
if isinstance(v, collections.abc.Mapping):
d1[k] = merge_dict(d1.get(k, {}), v)
else:
d1[k] = v
return d1
def create_nested_dict(data):
if len(data) > 1:
head, tail = data[0], data[1:]
return {head: create_nested_dict(tail)}
else:
return {data[0]: {}}
d = {}
with open(path) as f:
for line in f.readlines():
splits = line.strip().split("\t")
splits_d = create_nested_dict(splits)
merge_dict(d, splits_d)
return d
if __name__ == "__main__":
import json
for name in ("HeuristicWordlePlayer", "smallMaxInformationGainWordlePlayer"):
d = _txt2dict("output/traces_{}.txt".format(name))
with open("output/traces_{}.json".format(name), "w") as f:
json.dump(d, f)
print("{} saved.".format(f.name))