-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwrapper.py
More file actions
249 lines (194 loc) · 7.35 KB
/
wrapper.py
File metadata and controls
249 lines (194 loc) · 7.35 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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import struct
import array
from . import units
from .compat import int_from_byte
class Dictionary(object):
"""
Dictionary class for retrieval and binary I/O.
"""
def __init__(self):
self._units = array.array(str("I"))
ROOT = 0
"Root index"
def has_value(self, index):
#Checks if a given index is related to the end of a key.
return units.has_leaf(self._units[index])
def value(self, index):
#Gets a value from a given index.
offset = units.offset(self._units[index])
value_index = (index ^ offset) & units.PRECISION_MASK
return units.value(self._units[value_index])
def read(self, fp):
#Reads a dictionary from an input stream.
base_size = struct.unpack(str("=I"), fp.read(4))[0]
self._units.fromfile(fp, base_size)
def contains(self, key):
#Exact matching.
index = self.follow_bytes(key, self.ROOT)
if index is None:
return False
return self.has_value(index)
def find(self, key):
#Exact matching (returns value)
index = self.follow_bytes(key, self.ROOT)
if index is None:
return -1
if not self.has_value(index):
return -1
return self.value(index)
def follow_char(self, label, index):
#Follows a transition
offset = units.offset(self._units[index])
next_index = (index ^ offset ^ label) & units.PRECISION_MASK
if units.label(self._units[next_index]) != label:
return None
return next_index
def follow_bytes(self, s, index):
#Follows transitions.
for ch in s:
index = self.follow_char(int_from_byte(ch), index)
if index is None:
return None
return index
@classmethod
def load(cls, path):
dawg = cls()
with open(path, 'rb') as f:
dawg.read(f)
return dawg
class Guide(object):
ROOT = 0
def __init__(self):
self._units = array.array(str("B"))
def child(self, index):
return self._units[index*2]
def sibling(self, index):
return self._units[index*2 + 1]
def read(self, fp):
base_size = struct.unpack(str("=I"), fp.read(4))[0]
self._units.fromfile(fp, base_size*2)
def size(self):
return len(self._units)
class EdgeFollower(object):
def __init__(self, dic=None, guide=None):
self._dic = dic
self._guide = guide
def value(self):
if self._dic.has_value(self._cur_index):
return self._dic.value(self._cur_index)
return False
def start(self, index, prefix=b""):
"""initial setup for a completer next_edge() action on some prefix. If
there's a child for this prefix, we add that as the one item on the
index_stack. Otherwise, leave the stack empty, so next_edge() fails"""
self.key = bytearray(prefix)
self.base_key_len = len(self.key)
self._parent_index = index
self._sib_index = None
self._cur_index = None
if self._guide.size():
child_label = self._guide.child(index) # UCharType
if child_label:
# Follows a transition to the first child.
next_index = self._dic.follow_char(child_label, index)
if index is not None:
self._sib_index = next_index
self._cur_index = self._sib_index
self.key.append(child_label)
self.decoded_key = self.key.decode('utf-8')
return True
def next(self):
#Gets the next edge (not necessarily a terminal)
if not self._sib_index:
return False
sibling_label = self._guide.sibling(self._sib_index)
self._sib_index = self._dic.follow_char(sibling_label,
self._parent_index)
self._cur_index = self._sib_index
if not self._sib_index:
return False
self.key = self.key[:self.base_key_len]
self.key.append(sibling_label)
try:
self.decoded_key = self.key.decode('utf-8')
except UnicodeDecodeError:
#this sibling is a multibyte char. keep following its children til
#something is decodable
while True:
child_label = self._guide.child(self._sib_index)
self._cur_index = self._dic.follow_char(child_label,
self._cur_index)
if not self._cur_index:
return False
self.key.append(child_label)
try:
self.decoded_key = self.key.decode('utf-8')
break
except UnicodeDecodeError:
pass
return True
def get_cur_edge(self):
return (self.decoded_key, self._dic.has_value(self._cur_index))
class Completer(object):
def __init__(self, dic=None, guide=None):
self._dic = dic
self._guide = guide
def value(self):
return self._dic.value(self._last_index)
def start(self, index, prefix=b""):
#initial setup for a completer next() action on some prefix
self.key = bytearray(prefix)
if self._guide.size():
self._index_stack = [index]
self._last_index = self._dic.ROOT
else:
self._index_stack = []
def next(self):
#Gets the next key
if not self._index_stack:
return False
index = self._index_stack[-1]
if self._last_index != self._dic.ROOT:
child_label = self._guide.child(index) # UCharType
if child_label:
# Follows a transition to the first child.
index = self._follow(child_label, index)
if index is None:
return False
else:
while True:
sibling_label = self._guide.sibling(index)
# Moves to the previous node.
if len(self.key) > 0:
self.key.pop()
#self.key[-1] = 0
self._index_stack.pop()
if not self._index_stack:
return False
index = self._index_stack[-1]
if sibling_label:
# Follows a transition to the next sibling.
index = self._follow(sibling_label, index)
if index is None:
return False
break
return self._find_terminal(index)
def _follow(self, label, index):
next_index = self._dic.follow_char(label, index)
if next_index is None:
return None
self.key.append(label)
self._index_stack.append(next_index)
return next_index
def _find_terminal(self, index):
while not self._dic.has_value(index):
label = self._guide.child(index)
index = self._dic.follow_char(label, index)
if index is None:
return False
self.key.append(label)
self._index_stack.append(index)
self._last_index = index
return True