-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess.py
executable file
·127 lines (100 loc) · 3.44 KB
/
postprocess.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
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
#!/usr/bin/env python
import os
import argparse
import errno
import hashlib
import re
from numpy.random import RandomState
RNG = RandomState(214421)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def randint(m):
return RNG.randint(0, m + 1)
def random_combinations(n, k, count):
for _ in range(count):
c = randint(2 ** (k) - 1)
res = []
cap = []
i = 0
cap.append(randint(1) == 1)
for _ in range(n):
res.append(i)
if c % 2 == 1:
cap.append(randint(1) == 1)
i += 1
c = c // 2
yield res, cap
def fixidentifiers(sent, maxids, samples):
ids = set()
for m in re.finditer('i(\d+)', sent):
ids.add(m.group(0))
ids = sorted(ids)
# print(ids)
if len(ids) == 0:
yield sent
return
k = len(ids)
if k > maxids:
k = maxids
ids_map = dict(reversed(t) for t in enumerate(ids))
for comb, cap in random_combinations(len(ids), k, samples):
res = []
i = 0
for m in re.finditer('i(\d+)', sent):
res.append(sent[i:m.span()[0]])
key = m.group(0)
new_id = comb[ids_map[key]]
first = 'I' if cap[new_id] else 'i'
res.append(first + str(new_id))
i = m.span()[1]
res.append(sent[i:])
yield ''.join(res)
if __name__ == '__main__':
argparser = argparse.ArgumentParser()
argparser.add_argument('filename', type=str)
argparser.add_argument('outdir', type=str)
argparser.add_argument('-q', '--squash', type=str)
argparser.add_argument('-m', '--maxids', type=int, default=8)
argparser.add_argument('-s', '--samples', type=int, default=8)
args = argparser.parse_args()
mkdir_p(args.outdir)
seen = set()
file_count = 0
dir_count = 1
for i, chunk in enumerate(open(args.filename, 'rt').read().split('\1')):
chunk = chunk.strip()
if chunk == '':
continue
for chunk in fixidentifiers(chunk, args.maxids, args.samples):
name = hashlib.sha1(chunk.encode('utf-8')).hexdigest()
if name in seen:
continue
seen.add(name)
if (file_count + 1) % 50000 == 0:
olddir = os.path.join(args.outdir, str(dir_count))
if os.path.exists(olddir) and args.squash is not None:
os.system("mksquashfs %s %s -keep-as-directory -no-duplicates" % (olddir, args.squash))
for f in os.listdir(olddir):
os.remove(os.path.join(olddir, f))
os.rmdir(olddir)
dir_count += 1
file_count = 0
outdn = os.path.join(args.outdir, str(dir_count))
if not os.path.exists(outdn):
os.mkdir(outdn)
outfn = os.path.join(outdn, '%s.scala' % name)
with open(outfn, 'wt+') as f:
f.write(chunk)
file_count += 1
olddir = os.path.join(args.outdir, str(dir_count))
if os.path.exists(olddir) and args.squash is not None:
print(os.popen("mksquashfs %s sources -keep-as-directory" % (olddir, args.squash)).read())
for f in os.listdir(olddir):
os.remove(os.path.join(olddir, f))
os.rmdir(olddir)