forked from dtuggener/CharSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_ngram_probs.py
39 lines (32 loc) · 1.4 KB
/
merge_ngram_probs.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
import sys
import pickle
with open(sys.argv[1], 'rb') as f:
data1 = pickle.load(f)
with open(sys.argv[2], 'rb') as f:
data2 = pickle.load(f)
data_merged = {'prefix': {}, 'infix': {}, 'suffix': {}}
for _fix in ['prefix', 'infix', 'suffix']:
changes, amount, maxdiff, maxdiff_ngram = 0, 0.0, 0.0, ''
for ngram, prob in data1[_fix].items():
data_merged[_fix][ngram] = prob
for ngram, prob in data2[_fix].items():
# data_merged[_fix][ngram] = (data_merged[_fix].get(ngram, prob) + prob) / 2
if ngram not in data1[_fix] or prob == data1[_fix]:
data_merged[_fix][ngram] = prob
else: # if ngram in data1[_fix] and prob != data1[_fix][ngram]:
data_merged[_fix][ngram] = (data_merged[_fix][ngram] + prob) / 2
# gather statistics of what changed
changes += 1
diff = abs(data1[_fix][ngram] - prob)
amount += diff
if diff > maxdiff:
maxdiff = diff
maxdiff_ngram = ngram + ' (' + str(data1[_fix][ngram]) + ' -> ' + str(prob) + ')'
print('changes:', changes)
print('amount:', amount)
print('avg change:', amount / changes)
print('maxdiff:', maxdiff)
print('maxdiff_ngram:', maxdiff_ngram)
outfile = sys.argv[3] if len(sys.argv) > 3 else 'ngram_probs_merged.pickle'
with open(outfile, 'wb') as f:
pickle.dump(data_merged, f, pickle.HIGHEST_PROTOCOL)