This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
kamus.py
253 lines (216 loc) · 7.5 KB
/
kamus.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
"""
External Binary Trie
Author: Peb Ruswono Aryan
Description: read external file as dictionary
License : GPL
"""
import struct
import os
BASE_SCHEMA = 'cIII' #current character (char), left branch (uint32), right branch (uint32), value (uint32), limited to ASCII & 2GB filesize
class KamusEksternal:
CUR_position = 0
ROOT_position = 0
MARKER = 0xFFFFFFFF
def __init__(self, filename, scheme=BASE_SCHEMA):
if os.path.exists(filename):
self.handle = open(filename, "rb+")
#read schema
num = struct.unpack('<B', self.handle.read(1))[0]
self.schema = self.handle.read(num)
self.nodesize = struct.calcsize('<'+self.schema)
#read root
self.ROOT_position = self.handle.tell()
self.first()
else:
self.handle = open(filename, "wb+")
#write schema
self.schema = scheme
self.nodesize = struct.calcsize('<'+self.schema)
self.handle.write(struct.pack('<B', len(self.schema)))
self.handle.write(self.schema)
#write root
self.ROOT_position = self.handle.tell()
self.CUR_position = self.ROOT_position
self.current = ('`',0,0,self.MARKER)
self.save()
def close(self):
self.handle.seek(0, os.SEEK_END)
self.handle.close()
def has_child(self, c):
"""
move to child node containing character c, return to current when failed
"""
self.left()
while self.current is not None and self.current[0] != c:
self.right()
#{ self.current = None | self.current[0] = c }
return self.current
def __getitem__(self,key):
self.first()
child = None
for k in key:
PARENT_position = self.CUR_position
child = self.has_child(k)
if child is None: #not found
self.handle.seek(PARENT_position)
self.next()
break
#{ child = None | child contains last char in key}
if child is not None:
if child[3] == self.MARKER:
#exist but has not assigned
return None
else:
return child[3]
else:
return None
def __setitem__(self, key, value):
#kasus : ketemu/tidak
self.first()
i = 0
child = None
for k in key:
PARENT_position = self.CUR_position
child = self.has_child(k)
if child is None: #not found
self.handle.seek(PARENT_position)
self.next()
break
i += 1
#{ child = None | child contains last char in key}
if child is not None:#ketemu
self.set_value(value)
self.save()
else:
self.insert(key[i:], value)
def findprefix(self, prefix):
"""
return list of all words startswith prefix
"""
self.first()
i = 0
child = self.current #possible if len(prefix)==0
for k in prefix:
PARENT_position = self.CUR_position
child = self.has_child(k)
if child is None: #not found
self.handle.seek(PARENT_position)
self.next()
break
i += 1
#{ child = None | child contains last char in key}
result = []
if self.current[3] != self.MARKER:
result = [prefix]
self.left()
if i==len(prefix):
return result+self.get_child(prefix[:i])
else:
return result
def get_child(self, temp):
stack = []
result = []
stack.append((self.CUR_position, temp))
while len(stack)>0:
offset,pref = stack.pop()
#pref = pref.strip()
self.handle.seek(offset)
if not self.next(): continue
if self.current[2] != 0:
stack.append((self.current[2], pref))
if self.current[3] != self.MARKER:
result.append(pref+self.current[0])
if self.current[1] != 0:
stack.append((self.current[1], pref+self.current[0]))
return result
def insert(self, key, value):
"""
insert from current node as parent
"""
self.handle.seek(0, os.SEEK_END)
NEXT_position = self.handle.tell()
#find insertion point (parent|sibling)
if self.current[1] == 0:
#update parent's left
self.set_left(NEXT_position)
self.save()
else:
#find last sibling
PARENT_position = self.CUR_position
self.left()
while self.current[2] != 0:
self.right()
#{ current is last sibling }
self.set_right(NEXT_position)
self.save()
self.handle.seek(NEXT_position)
self.CUR_position = self.handle.tell()
for ci in xrange(len(key)):
#write current
if ci==len(key)-1:
self.current = (key[ci], 0, 0, value)
else:
self.current = (key[ci], self.CUR_position + self.nodesize, 0, self.MARKER)
self.handle.write(struct.pack('<'+self.schema, *self.current))
self.CUR_position = self.handle.tell()
def save(self):
self.handle.seek(self.CUR_position)
self.handle.write(struct.pack('<'+self.schema, *self.current))
# modify in-memory-node, saving deferred
def set_char(self, value):
self.current = (value, self.current[1], self.current[2], self.current[3])
def set_left(self, value):
self.current = (self.current[0], value, self.current[2], self.current[3])
def set_right(self, value):
self.current = (self.current[0], self.current[1], value, self.current[3])
def set_value(self, value):
self.current = (self.current[0], self.current[1], self.current[2], value)
# tree traversal
def left(self):
"""
(CAR current)
"""
if self.current[1] != 0:
self.handle.seek(self.current[1])
self.next()
else:
self.current = None
return self.current
def right(self):
"""
(CDR current)
"""
if self.current[2] != 0:
self.handle.seek(self.current[2])
self.next()
else:
self.current = None
return self.current
# read schema updates current (in memory) node and store current offset for later saving
def first(self):
self.handle.seek(self.ROOT_position)
self.next()
def next(self):
self.CUR_position = self.handle.tell()
buf = self.handle.read(self.nodesize)
if len(buf)==self.nodesize:
self.current = struct.unpack('<'+self.schema, buf)
return True
else:
return False
if __name__=='__main__':
k = KamusEksternal('tes.dic')
k['acuh'] = 2
k.close()
k = KamusEksternal('tes.dic')
print k['a']
print k['acuh']
k.close()
k = KamusEksternal('tes.dic')
k['a'] = 1
k['abadi'] = 3
k.close()
k = KamusEksternal('tes.dic')
print k.findprefix('a')
k.close()
os.unlink('tes.dic')