forked from matteodellamico/montecarlopwd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff.py
266 lines (221 loc) · 9.22 KB
/
backoff.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright 2016 Symantec Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
import bisect
import collections
import itertools
import math
import operator
import os
import random
import shelve
import numpy
import model
import ngram_chain
TmpNode = collections.namedtuple('TmpNode',
'transitions probabilities '
'cumprobs logprobs')
class BackoffModel(ngram_chain.NGramModel):
def __init__(self, words, threshold, start_symbol=True,
with_counts=False, shelfname=None):
if not with_counts:
words = [(1, w) for w in words]
self.start = start = '\0' if start_symbol else ''
self.end = end = '\0'
lendelta = len(start) + len(end)
words = [(len(w) + lendelta, (c, start + w + end))
for c, w in words]
self.nodes = nodes = self.setup_nodes(shelfname)
words.sort(key=operator.itemgetter(0))
if not words:
return
lens, words = zip(*words)
lens = numpy.array(lens)
nwords = len(words)
def zerodict():
return collections.defaultdict(itertools.repeat(0).__next__)
charcounts = zerodict()
for count, word in words:
for c in word[start_symbol:]:
charcounts[c] += count
totchars = sum(charcounts.values())
transitions, counts = zip(*sorted(charcounts.items(),
key=operator.itemgetter(1),
reverse=True))
transitions = ''.join(transitions)
counts = numpy.array(counts)
totchars = counts.sum()
probabilities = counts / totchars
if transitions:
nodes[''] = TmpNode(transitions,
probabilities,
probabilities.cumsum(),
-numpy.log2(probabilities))
leftidx = 0
skipwords = set()
for n in range(2, lens[-threshold] + 1):
leftidx = bisect.bisect_left(lens, n)
ngram_counter = zerodict()
for i in range(leftidx, nwords):
if i in skipwords:
continue
count, word = words[i]
skip = True
for j in range(lens[i] - n + 1):
ngram = word[j: j + n]
if ngram[:-2] in nodes:
ngram_counter[ngram] += count
skip = False
if skip:
skipwords.add(i)
tmp_dict = collections.defaultdict(list)
for ngram, count in ngram_counter.items():
tmp_dict[ngram[:-1]].append((ngram[-1], count))
for state, sscounts in tmp_dict.items():
total = sum(count for _, count in sscounts)
if total < threshold:
continue
trans_probs = {c: count / total
for c, count in sscounts
if count >= threshold}
missing = 1 - sum(trans_probs.values())
if missing == 1:
continue
if missing > 0:
parent_state = self.nodes[state[1:]]
lower_prob = 0
for c, p in zip(parent_state.transitions,
parent_state.probabilities):
if c not in trans_probs:
lower_prob += p
backoff_factor = missing / lower_prob
for c, p in zip(parent_state.transitions,
parent_state.probabilities):
if c not in trans_probs:
trans_probs[c] = p * backoff_factor
trans_probs = sorted(trans_probs.items(),
key=operator.itemgetter(1),
reverse=True)
transitions, probabilities = zip(*trans_probs)
transitions = ''.join(transitions)
probabilities = numpy.array(probabilities)
# probabilities must sum to 1
# assert abs(probabilities.sum() - 1) < 0.001
nodes[state] = TmpNode(transitions, probabilities,
probabilities.cumsum(),
-numpy.log2(probabilities))
Node = ngram_chain.Node
for state, node in self.nodes.items():
nodes[state] = Node(node.transitions, node.cumprobs,
node.logprobs)
def update_state(self, state, transition):
nodes = self.nodes
state += transition
while state not in nodes:
state = state[1:]
return state
class LazyBackoff(model.Model):
def __init__(self, path, threshold, start=True, end=True):
self.threshold = threshold
self.start = '\0' if start else ''
self.end = '\0' if end else ''
self.shelves = {
int(fname): shelve.open(os.path.join(path, fname), 'r')
for fname in os.listdir(path)
}
def hasnode(self, state):
shelves = self.shelves
return len(state) in shelves and state in shelves[len(state)]
def getnode(self, state):
return self.shelves[len(state)][state]
def getcount(self, ngram):
if ngram != '':
return self.shelves[len(ngram) - 1][ngram[:-1]][ngram[-1]]
else:
return sum(self.shelves[0][''].values())
def begin(self):
start = self.start
return start, self.getnode(start)
def update_state(self, state, transition):
state += transition
while not self.hasnode(state):
state = state[1:]
while self.getcount(state) < self.threshold:
state = state[1:]
node = self.getnode(state)
return state, node
def backoff(self, state):
state = state[1:]
node = self.getnode(state)
return state, node
def logprob(self, word, leaveout=False):
res = 0
state, node = self.begin()
for c in word + self.end:
last_node = None
while True: # break when we should stop backing off
count = node.get(c, 0) - leaveout
total = self.getcount(state) - leaveout
if state == self.start == self.end:
total /= 2
if last_node is not None:
lower_sum = sum(cnt for c, cnt in node.items()
if last_node.get(c, 0) >= self.threshold)
res += math.log2(1 - (lower_sum / total))
last_node = node
if count >= self.threshold or state == '':
break
passing = sum(ct for ct in node.values()
if ct >= self.threshold)
if leaveout and count == self.threshold - 1:
passing -= self.threshold
try:
res -= math.log2(1 - (passing / total))
except ValueError:
return float('inf')
state, node = self.backoff(state)
try:
res -= math.log2(count / total)
except ValueError:
return float('inf')
state, node = self.update_state(state, c)
return res
def generate(self, maxlen=100):
logprob = 0
state, node = self.begin()
word = ''
while True: # return when we find self.end
last_node = None
while True: # break when we should stop backing off
total = self.getcount(state)
if state == self.start == self.end:
total /= 2
items = list(node.items())
diff = total - sum([cnt for _, cnt in items])
if last_node is not None:
items = [(c, cnt) for c, cnt in items if last_node.get(
c, -1) < self.threshold]
lower_sum = sum(cnt for c, cnt in node.items()
if last_node.get(c, 0) >= self.threshold)
logprob += math.log2(1 - (lower_sum / total))
items.append(('', diff))
last_node = node
c, count = random.choices(
items, weights=[cnt for _, cnt in items])[0]
if (state == '' or count >= self.threshold) and (c != ''):
break
passing = sum(ct for ct in node.values()
if ct >= self.threshold)
logprob -= math.log2(1 - (passing / total))
state, node = self.backoff(state)
logprob -= math.log2(count / total)
if c == self.end:
return logprob, word
word += c
if len(word) >= maxlen:
return logprob, word
state, node = self.update_state(state, c)