-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdawgs.py
More file actions
507 lines (385 loc) · 14.8 KB
/
dawgs.py
File metadata and controls
507 lines (385 loc) · 14.8 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import struct
from binascii import a2b_base64
from . import wrapper
from .compat import int_from_byte
class DAWG(object):
"""
Base DAWG wrapper.
"""
def __init__(self):
self.dct = None
def __contains__(self, key):
if not isinstance(key, bytes):
key = key.encode('utf8')
return self.dct.contains(key)
def load(self, path):
"""
Loads DAWG from a file.
"""
self.dct = wrapper.Dictionary.load(path)
return self
def _has_value(self, index):
return self.dct.has_value(index)
def _similar_keys(self, current_prefix, key, index, replace_chars):
res = []
start_pos = len(current_prefix)
end_pos = len(key)
word_pos = start_pos
while word_pos < end_pos:
b_step = key[word_pos].encode('utf8')
if b_step in replace_chars:
for (b_replace_char, u_replace_char) in replace_chars[b_step]:
next_index = index
next_index = self.dct.follow_bytes(b_replace_char, next_index)
if next_index:
prefix = current_prefix + key[start_pos:word_pos] + u_replace_char
extra_keys = self._similar_keys(prefix, key, next_index, replace_chars)
res += extra_keys
index = self.dct.follow_bytes(b_step, index)
if index is None:
break
word_pos += 1
else:
if self._has_value(index):
found_key = current_prefix + key[start_pos:]
res.insert(0, found_key)
return res
def similar_keys(self, key, replaces):
"""
Returns all variants of ``key`` in this DAWG according to
``replaces``.
``replaces`` is an object obtained from
``DAWG.compile_replaces(mapping)`` where mapping is a dict
that maps single-char unicode strings to (one or more) single-char
unicode strings.
This may be useful e.g. for handling single-character umlauts.
"""
return self._similar_keys("", key, self.dct.ROOT, replaces)
@classmethod
def compile_replaces(cls, replaces):
for k,v in replaces.items():
if len(k) != 1:
raise ValueError("Keys must be single-char unicode strings.")
if (isinstance(v, str) and len(v) != 1):
raise ValueError("Values must be single-char unicode strings or non-empty lists of such.")
if isinstance(v, list) and (any(len(v_entry) != 1 for v_entry in v) or len(v) < 1):
raise ValueError("Values must be single-char unicode strings or non-empty lists of such.")
return dict(
(
k.encode('utf8'),
[(v_entry.encode('utf8'), v_entry) for v_entry in v]
)
for k, v in replaces.items()
)
def prefixes(self, key):
'''
Returns a list with keys of this DAWG that are prefixes of the ``key``.
'''
res = []
index = self.dct.ROOT
if not isinstance(key, bytes):
key = key.encode('utf8')
pos = 1
for ch in key:
index = self.dct.follow_char(int_from_byte(ch), index)
if not index:
break
if self._has_value(index):
res.append(key[:pos].decode('utf8'))
pos += 1
return res
class CompletionDAWG(DAWG):
"""
DAWG with key completion support.
"""
def __init__(self):
super(CompletionDAWG, self).__init__()
self.guide = None
def keys(self, prefix=""):
b_prefix = prefix.encode('utf8')
res = []
index = self.dct.follow_bytes(b_prefix, self.dct.ROOT)
if index is None:
return res
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, b_prefix)
while completer.next():
key = completer.key.decode('utf8')
res.append(key)
return res
def iterkeys(self, prefix=""):
b_prefix = prefix.encode('utf8')
index = self.dct.follow_bytes(b_prefix, self.dct.ROOT)
if index is None:
return
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, b_prefix)
while completer.next():
yield completer.key.decode('utf8')
def load(self, path):
"""
Loads DAWG from a file.
"""
self.dct = wrapper.Dictionary()
self.guide = wrapper.Guide()
with open(path, 'rb') as f:
self.dct.read(f)
self.guide.read(f)
return self
PAYLOAD_SEPARATOR = b'\x01'
MAX_VALUE_SIZE = 32768
class BytesDAWG(CompletionDAWG):
"""
DAWG that is able to transparently store extra binary payload in keys;
there may be several payloads for the same key.
In other words, this class implements read-only DAWG-based
{unicode -> list of bytes objects} mapping.
"""
def __init__(self, payload_separator=PAYLOAD_SEPARATOR):
self._payload_separator = payload_separator
def __contains__(self, key):
if not isinstance(key, bytes):
key = key.encode('utf8')
return bool(self._follow_key(key))
# def b_has_key(self, key):
# return bool(self._follow_key(key))
def __getitem__(self, key):
res = self.get(key)
if res is None:
raise KeyError(key)
return res
def get(self, key, default=None):
"""
Returns a list of payloads (as byte objects) for a given key
or ``default`` if the key is not found.
"""
if not isinstance(key, bytes):
key = key.encode('utf8')
return self.b_get_value(key) or default
def _follow_key(self, b_key):
index = self.dct.follow_bytes(b_key, self.dct.ROOT)
if not index:
return False
index = self.dct.follow_bytes(self._payload_separator, index)
if not index:
return False
return index
def _value_for_index(self, index):
res = []
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index)
while completer.next():
# a2b_base64 doesn't support bytearray in python 2.6
# so it is converted (and copied) to bytes
b64_data = bytes(completer.key)
res.append(a2b_base64(b64_data))
return res
def b_get_value(self, b_key):
index = self._follow_key(b_key)
if not index:
return []
return self._value_for_index(index)
def keys(self, prefix=""):
if not isinstance(prefix, bytes):
prefix = prefix.encode('utf8')
res = []
index = self.dct.ROOT
if prefix:
index = self.dct.follow_bytes(prefix, index)
if not index:
return res
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, prefix)
while completer.next():
payload_idx = completer.key.index(self._payload_separator)
u_key = completer.key[:payload_idx].decode('utf8')
res.append(u_key)
return res
def iterkeys(self, prefix=""):
if not isinstance(prefix, bytes):
prefix = prefix.encode('utf8')
index = self.dct.ROOT
if prefix:
index = self.dct.follow_bytes(prefix, index)
if not index:
return
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, prefix)
while completer.next():
payload_idx = completer.key.index(self._payload_separator)
u_key = completer.key[:payload_idx].decode('utf8')
yield u_key
def items(self, prefix=""):
if not isinstance(prefix, bytes):
prefix = prefix.encode('utf8')
res = []
index = self.dct.ROOT
if prefix:
index = self.dct.follow_bytes(prefix, index)
if not index:
return res
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, prefix)
while completer.next():
key, value = completer.key.split(self._payload_separator)
res.append(
(key.decode('utf8'), a2b_base64(bytes(value))) # bytes() cast is a python 2.6 fix
)
return res
def iteritems(self, prefix=""):
if not isinstance(prefix, bytes):
prefix = prefix.encode('utf8')
index = self.dct.ROOT
if prefix:
index = self.dct.follow_bytes(prefix, index)
if not index:
return
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, prefix)
while completer.next():
key, value = completer.key.split(self._payload_separator)
item = (key.decode('utf8'), a2b_base64(bytes(value))) # bytes() cast is a python 2.6 fix
yield item
def _has_value(self, index):
return self.dct.follow_bytes(PAYLOAD_SEPARATOR, index)
def _similar_items(self, current_prefix, key, index, replace_chars):
res = []
start_pos = len(current_prefix)
end_pos = len(key)
word_pos = start_pos
while word_pos < end_pos:
b_step = key[word_pos].encode('utf8')
if b_step in replace_chars:
for (b_replace_char, u_replace_char) in replace_chars[b_step]:
next_index = index
next_index = self.dct.follow_bytes(b_replace_char, next_index)
if next_index:
prefix = current_prefix + key[start_pos:word_pos] + u_replace_char
extra_items = self._similar_items(prefix, key, next_index, replace_chars)
res += extra_items
index = self.dct.follow_bytes(b_step, index)
if not index:
break
word_pos += 1
else:
index = self.dct.follow_bytes(self._payload_separator, index)
if index:
found_key = current_prefix + key[start_pos:]
value = self._value_for_index(index)
res.insert(0, (found_key, value))
return res
def similar_items(self, key, replaces):
"""
Returns a list of (key, value) tuples for all variants of ``key``
in this DAWG according to ``replaces``.
``replaces`` is an object obtained from
``DAWG.compile_replaces(mapping)`` where mapping is a dict
that maps single-char unicode strings to (one or more) single-char
unicode strings.
"""
return self._similar_items("", key, self.dct.ROOT, replaces)
def _similar_item_values(self, start_pos, key, index, replace_chars):
res = []
end_pos = len(key)
word_pos = start_pos
while word_pos < end_pos:
b_step = key[word_pos].encode('utf8')
if b_step in replace_chars:
next_index = index
b_replace_char, u_replace_char = replace_chars[b_step]
next_index = self.dct.follow_bytes(b_replace_char, next_index)
if next_index:
extra_items = self._similar_item_values(word_pos+1, key, next_index, replace_chars)
res += extra_items
index = self.dct.follow_bytes(b_step, index)
if not index:
break
word_pos += 1
else:
index = self.dct.follow_bytes(self._payload_separator, index)
if index:
value = self._value_for_index(index)
res.insert(0, value)
return res
def similar_item_values(self, key, replaces):
"""
Returns a list of values for all variants of the ``key``
in this DAWG according to ``replaces``.
``replaces`` is an object obtained from
``DAWG.compile_replaces(mapping)`` where mapping is a dict
that maps single-char unicode strings to (one or more) single-char
unicode strings.
"""
return self._similar_item_values(0, key, self.dct.ROOT, replaces)
class RecordDAWG(BytesDAWG):
def __init__(self, fmt, payload_separator=PAYLOAD_SEPARATOR):
super(RecordDAWG, self).__init__(payload_separator)
self._struct = struct.Struct(str(fmt))
self.fmt = fmt
def _value_for_index(self, index):
value = super(RecordDAWG, self)._value_for_index(index)
return [self._struct.unpack(val) for val in value]
def items(self, prefix=""):
res = super(RecordDAWG, self).items(prefix)
return [(key, self._struct.unpack(val)) for (key, val) in res]
def iteritems(self, prefix=""):
res = super(RecordDAWG, self).iteritems(prefix)
return ((key, self._struct.unpack(val)) for (key, val) in res)
LOOKUP_ERROR = -1
class IntDAWG(DAWG):
"""
Dict-like class based on DAWG.
It can store integer values for unicode keys.
"""
def __getitem__(self, key):
res = self.get(key, LOOKUP_ERROR)
if res == LOOKUP_ERROR:
raise KeyError(key)
return res
def get(self, key, default=None):
"""
Return value for the given key or ``default`` if the key is not found.
"""
if not isinstance(key, bytes):
key = key.encode('utf8')
res = self.b_get_value(key)
if res == LOOKUP_ERROR:
return default
return res
def b_get_value(self, key):
return self.dct.find(key)
class IntCompletionDAWG(CompletionDAWG, IntDAWG):
"""
Dict-like class based on DAWG.
It can store integer values for unicode keys and support key completion.
"""
def items(self, prefix=""):
if not isinstance(prefix, bytes):
prefix = prefix.encode('utf8')
res = []
index = self.dct.ROOT
if prefix:
index = self.dct.follow_bytes(prefix, index)
if not index:
return res
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, prefix)
while completer.next():
res.append(
(completer.key.decode('utf8'), completer.value())
)
return res
def iteritems(self, prefix=""):
if not isinstance(prefix, bytes):
prefix = prefix.encode('utf8')
index = self.dct.ROOT
if prefix:
index = self.dct.follow_bytes(prefix, index)
if not index:
return
completer = wrapper.Completer(self.dct, self.guide)
completer.start(index, prefix)
while completer.next():
yield completer.key.decode('utf8'), completer.value()