/
structures.py
389 lines (320 loc) · 11.1 KB
/
structures.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from itertools import chain
from collections import Counter
import numpy as np
class TrieList:
word_end = -1
def __init__(self, lexicon=[]):
"""
Initialize your data structure here.
"""
self.root = {}
self.word_end = -1
self.par_list = []
self.par_dict = dict()
self.relationship = dict()
self.root['skipgram'] = dict()
for i in lexicon:
if i not in self:
self.append(i)
def insert(self, index, word, note=True):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
curNode = self.root
for c in word:
if not c in curNode:
curNode[c] = {}
curNode = curNode[c]
if self.word_end in curNode:
raise Exception(f'already has "{word}"')
else:
curNode[self.word_end] = note
if index == -1:
self.par_list.append(word)
self.par_dict[word] = len(self.par_list) - 1
else:
self.par_list.insert(index, word)
par_dict[word] = index
def append(self, word, note=True):
self.insert(-1, word, note)
def _del(self, word): # delete from the trie and dict
# print(word)
curNode = self.root
def f(word, curNode):
if len(word) == 0:
del curNode[-1]
else:
f(word[1:], curNode[word[0]])
if len(curNode[word[0]]) == 0:
del curNode[word[0]]
f(word, curNode)
del self.par_dict[word]
def _del(self, word): # delete from the trie and dict
# print(word)
curNode = self.root
def f(word, curNode):
if len(word) == 0:
del curNode[-1]
else:
f(word[1:], curNode[word[0]])
if len(curNode[word[0]]) == 0:
del curNode[word[0]]
f(word, curNode)
del self.par_dict[word]
def pop(self, index=-1):
word = self.par_list.pop(index)
self._del(word)
return word
def exchange(self, i, j):
self.par_list[j], self.par_list[i] = self.par_list[i], self.par_list[j]
def move(self, i, step):
w = self.par_list[i]
if step<0:
if i!=0:
self.par_list[i+step+1: i+1] = self.par_list[i+step: i]
self.par_list[i+step] = w
self.par_dict[w] = i + step
elif step>0:
if i+step<len(self.par_list):
self.par_list[i: i+step] = self.par_list[i+1: i+step+1]
self.par_list[i+step] = w
self.par_dict[w] = i + step
else:
self.par_list[i:] = self.par_list[i+1:]
self._del(w)
def group_move(self, words, update_rate, pre_update=True):
if pre_update:
self.update_ind() # make "index_with_prior" works
offset = 0
for w, e in words:
# print(w)
if w in self.par_dict:
raw_ind = self.index_with_prior(w, offset)
if raw_ind is not None and raw_ind > 0:
if e == 1:
offset += 1
step = e * (int((raw_ind) * update_rate) + 1)
self.move(raw_ind, step)
self.update_ind()
def remove(self, word):
ind = self.index_with_prior(word, nothing=-1)
if ind != -1:
self._del(word)
self.par_list[ind: ind + 1] = []
def group_remove(self, words):
if len(words) > 0:
words_ = []
for w in words:
if w in self.par_dict:
self._del(w)
words_.append(w)
# li = np.array(self.par_list,dtype=object)
# self.par_list = li[~np.in1d(li, words_)].tolist()
w_set = set(words_)
self.par_list = [i for i in self.par_list if i not in w_set]
self.update_ind()
# def group_remove(self, words):
# if len(words) > 0:
# words_ = []
# for w in words:
# if w in self.par_dict:
# self.remove(w)
# self.update_ind()
def index(self, word):
index = self.par_list.index(word)
return index
def index_with_prior(self, word, offset=0, nothing=False):
try:
prior_ind = self.par_dict[word]
if offset == 0:
return self.par_list.index(word, prior_ind)
else:
start = prior_ind - offset
if start < 0:
start = 0
return self.par_list.index(word, start)
except:
if nothing==False:
raise
return nothing
def update_ind(self):
self.par_dict = dict(zip(self.par_list, range(len(self.par_list)))) # speed up 1.5x~2x than the next line
# self.par_dict = dict((word,ind) for ind,word in enumerate(self.par_list))
# self.relationship = dict((k,v) for k,v in self.relationship.items() if k in self.par_dict)
def be_a_part(self, string):
if not self.search(string):
return False
curNode = self.root
for c in string:
curNode = curNode[c]
return (len(curNode) > 1)
def startwith(self, string):
if not self.search(string):
return []
chunks = []
curNode = self.root
for c in string:
curNode = curNode[c]
def f(pre, curNode):
if len(curNode) == 1:
chunks.append(pre)
for i in curNode:
if i!=-1:
f(pre+i, curNode[i])
f(string, curNode)
return chunks
def has_a_larger(self, string):
if not self.search(string):
return None
curNode = self.root
for c in string:
curNode = curNode[c]
def f(pre, curNode):
subs = [i for i in curNode if i!=-1]
if len(subs) == 0:
return pre
elif len(subs) == 1:
for i in subs:
return f(pre+i, curNode[i])
else:
return None
if curNode == {-1: True}:
return None
else:
return f(string, curNode)
def search(self, word):
return word in self.par_dict
def skipgram_match(self, chunks):
curNode = self.root['skipgram']
gram = []
for c in chunks:
if c in curNode:
gram.append(c)
if len(gram) > 1:
break
else:
curNode = curNode[c]
elif len(gram) > 0:
gram.append(c)
else:
gram = []
if len(gram) > 2:
return [gram[0],gram[-1]], gram[1:-1], chunks[chunks.index(gram[0])+1:]
else:
return None, None, None
def match(self, string):
curNode = self.root
words = ['','']
for c in string:
if not c in curNode:
if -1 in curNode:
return words[-1]
else:
return words[-2]
if -1 in curNode:
words.append(words[-1]) # we know there was a chunk, now we make a new hypothesis
words[-1] += c
curNode = curNode[c]
if -1 in curNode:
return words[-1]
else:
return words[-2]
def match_two(self, string):
curNode = self.root
words = ['','']
for c in string:
if not c in curNode:
if -1 in curNode:
if len(words) > 1:
return words[-2:]
else:
return ['',words[-1]]
else:
if len(words) > 2:
return words[-3:-1]
else:
return ['','']
if -1 in curNode:
words.append(words[-1]) # we know there was a chunk, now we make a new hypothesis
words[-1] += c
curNode = curNode[c]
if -1 in curNode:
return words[-2:]
elif len(words) > 2:
return words[-3:-1]
else:
return ['','']
def match_2nd(self, string):
curNode = self.root
words = ['','']
for c in string:
if -1 in curNode:
words.append(words[-1]) # we know there was a chunk, now we make a new hypothesis
if not c in curNode:
if -1 in curNode:
return words[-2]
elif len(words) > 3:
return words[-3]
else:
return ''
words[-1] += c
curNode = curNode[c]
if -1 in curNode:
return words[-2]
elif len(words) > 3:
return words[-3]
else:
return ''
def __iter__(self):
return iter(self.par_list)
def __contains__(self, word):
return self.search(word)
def __repr__(self):
return str(self.par_list[:100])+'...'
def __len__(self):
return len(self.par_list)
def __set__(self):
return set(self.par_list)
def __getitem__(self, key):
return self.par_list[key]
def __setitem__(self, key, value):
self._del(self.par_list[key])
curNode = self.root
for c in value:
if not c in curNode:
curNode[c] = {}
curNode = curNode[c]
curNode[self.word_end] = True
self.par_list[key] = value
def __copy__(self):
return self.copy()
def copy(self):
new = TrieList(self.par_list)
new.relationship = new.relationship
return new
def debug(self, mode):
if mode == 'dict not root':
return [i for i in self.par_dict if not self.match(i)==i]
elif mode == 'dict not list':
return [i for i in self.par_dict if i not in self.par_list]
elif mode == 'list not root':
return [i for i in self.par_list if not self.match(i)==i]
elif mode == 'list not dict':
return [i for i in self.par_list if i not in self.par_dict]
else:
chunks = []
curNode = self.root
def f(pre, curNode):
for i in curNode:
if i!=-1:
f(pre+i, curNode[i])
else:
chunks.append(pre)
f('', curNode)
if mode == 'root not list':
li = [i if isinstance(i,str) else ''.join(i) for i in self.par_list]
elif mode == 'root not dict':
li = [i if isinstance(i,str) else ''.join(i) for i in self.par_dict]
return [i for i in chunks if i not in li]