-
Notifications
You must be signed in to change notification settings - Fork 0
/
idx_reader.py
executable file
·361 lines (279 loc) · 8.43 KB
/
idx_reader.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
#! python3
import io
# import logging
# logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s - %(levelname)s - %(message)s')
class InputError(Exception):
"""docstring for InputError"""
pass
class ByteStream(object):
"""A simple wrapper for bytes"""
def __init__(self, bytes):
super(ByteStream, self).__init__()
self.bytes = bytes
assert type(bytes) == type(b''), "Must be initialized by bytes"
def readall(self):
result = self.bytes[:]
self.bytes = b''
return result
def read(self, n=-1):
result = b''
if n == -1:
result = self.readall()
else:
result = self.bytes[:n]
self.bytes = self.bytes[n:]
return result
class ByteStreamReader(object):
"""docstring for UnicodeReader"""
class EOFError(Exception):
pass
def __init__(self, file_in) :
self.file = file_in
self.EOF = False
def _count_leading_ones(self, byte):
result = 0
number = int.from_bytes(byte, byteorder='big')
for i in range(8):
if (number >> (7 - i)) & 1:
result += 1
else:
break
return result
def read_byte(self):
result = self.file.read(1)
if result == b'':
self.EOF = True
return result
def read_n_bytes(self, n=1):
byte_array = b''
for i in range(n):
byte_array += self.read_byte()
return byte_array
def read_unicode_literal(self, count=[0]):
"""Reads a utf-8 literal from stream.
count variable is set to the number of bytes read
"""
byte = self.read_byte()
byte_array = byte
num_of_ones = self._count_leading_ones(byte)
# This is done due to utf-8 specification.
# A literal can be consisted from up to 4 bytes.
# The number of bytes is encoded in the first byte.
# That is:
# 0xxxxxxx - 1 byte
# 110xxxxx - 2 bytes
# 1110xxxx - 3 bytes
# 11110xxx - 4 bytes
if num_of_ones != 0:
num_of_ones -= 1
assert num_of_ones in [0, 1, 2, 3], "Not a unicode literal?"
for i in range(num_of_ones):
byte = self.read_byte()
byte_array += byte
count[0] = len(byte_array)
return byte_array.decode(encoding="utf-8")
def read_unicode_string(self, delimeter=u'\n', count=[0]):
result = u''
count = [0]
while True:
count_tmp = [0]
literal = self.read_unicode_literal(count=count_tmp)
count[0] += count_tmp[0]
if literal and literal != delimeter:
result += literal
else:
break
return result
def read_int32(self, count=[0]):
# TODO:
# I'm hesitant about how bytes are converted to integer value
# If the first byte in the array begins with 1, does it mean
# that the result is going to be negative?
# So I just added a leading zero byte here. Just in case.
# I should hack this later.
#
# Update: I found signed parameter.
if self.EOF:
return None
byte_array = self.read_n_bytes(n=4)
count[0] = len(byte_array)
return int.from_bytes(b'\0' + byte_array, byteorder="big", signed=False)
def read_int64(self, count=[0]):
# I should fix this
if self.EOF:
return None
byte_array = self.read_n_bytes(n=8)
count[0] = len(byte_array)
return int.from_bytes(b'\0' + byte_array, byteorder="big", signed=False)
class InfoParser(object):
"""Parses .ifo file"""
NUM_OPTS = ["wordcount", "synwordcount", "idxoffsetbits", "idxfilesize"]
TEXT_OPTS = ["bookname", "author", "email", "website",
"description", "date", "sametypesequence", "version"]
OPTIONS = NUM_OPTS + TEXT_OPTS
def __init__(self, filename):
# super(IdxParser, self).__init__()
self.filename = filename
self.version = None
self.bookname = None # required
self.wordcount = None # required
self.synwordcount = None # required if ".syn" file exists.
self.idxfilesize = None # required
self.idxoffsetbits = None # New in 3.0.0
self.author = None
self.email = None
self.website = None
self.description = None # use <br> for new line.
self.date = None
self.sametypesequence = None # very important.
self._parse()
if self._valid() != True:
raise InputError("Wrong data! Required options are missing")
def parse(self):
fin = open(self.filename, "r")
# check the file has a standard header
header = fin.readline().strip()
if header != "StarDict's dict ifo file":
raise InputError("Wrong .ifo file format")
for line in fin:
line = line.strip()
if line == "":
continue
try:
option, option_body = line.split("=", maxsplit=1)
except ValueError(e):
raise InputError("Wrong .ifo file format. Bad option.")
if option not in self.OPTIONS:
raise InputError("No such option: {0}".format(option))
if option in self.NUM_OPTS:
option_body = int(option_body)
setattr(self, option, option_body)
fin.close()
def valid(self):
return (self.bookname != None and
self.wordcount != None and
self.idxfilesize != None)
def get_info(self):
result = {}
for option in self.OPTIONS:
value = getattr(self, option)
if value != None:
result[option] = value
return result
# this is done in case if someone reimplements parse method
_parse = parse
_valid = valid
class IndexParser(object):
"""IdxParser"""
def __init__(self, info, idx_filename):
super(IndexParser, self).__init__()
self.info = info
self.filename = idx_filename
self._fin = open(idx_filename, "rb")
self.byte_reader = ByteStreamReader(self._fin)
self.index = []
self._parse()
def __del__(self):
if not self._fin.closed:
self._fin.close()
def _read_word_string(self):
return self.byte_reader.read_unicode_string(delimeter=u'\0')
def _read_data_offset(self):
result = 0
if "idxoffsetbits" in self.info and self.info["idxoffsetbits"] == 64:
result = self.byte_reader.read_int64()
else:
result = self.byte_reader.read_int32()
return result
def _read_data_size(self):
return self.byte_reader.read_int32()
def parse(self):
while not self.byte_reader.EOF:
word_str = self._read_word_string()
data_offset = self._read_data_offset()
data_size = self._read_data_size()
if data_size != None:
self.index.append( (word_str, data_offset, data_size) )
else:
break
def get_index(self):
return self.index
_parse = parse
class DataParser(object):
def __init__(self, info, index, byte_reader):
super(DataParser, self).__init__()
self.info = info
self.index = index
self.data = []
self.byte_reader = byte_reader
self.sametypesequence = None
if "sametypesequence" in self.info:
self.sametypesequence = self.info["sametypesequence"]
self._parse()
def _read_data_type(self):
return self.byte_reader.read_unicode_literal()
def _retrieve_media(self, data_reader):
pass
def _retrieve_text(self, data_reader):
result = data_reader.read_unicode_string(delimeter='\0')
return result
def _retrieve_data(self, data_reader, data_type):
result = None
if data_type in u"mlgtxykwhr":
result = (data_type, self._retrieve_text(data_reader))
else:
result = (data_type, self._retrieve_media(data_reader))
return result
def _parse_data_chunk(self, data_reader):
result = []
if self.sametypesequence != None:
for data_type in self.sametypesequence:
result.append(self._retrieve_data(data_reader, data_type))
else:
while not data_reader.EOF:
data_type = data_reader.read_unicode_literal()
result.append(self._retrieve_data(data_reader, data_type))
return result
def parse(self):
for idx in self.index:
word_str, data_offset, data_size = idx
data_chunk = self.byte_reader.read_n_bytes(data_size)
data_stream = ByteStream(data_chunk)
data_reader = ByteStreamReader(data_stream)
self.data.append( (word_str, self._parse_data_chunk(data_reader)) )
def get_data(self):
return self.data
_parse = parse
info = InfoParser("./dictionaries/RuKrRu.ifo").get_info()
print(info)
# reader = ByteStreamReader("./hello.txt")
# print(reader.read_unicode_string(delimeter=u'\0'))
# print(reader.read_int32())
# print(reader.read_int32())
# print(reader.read_int32())
# print(reader.read_unicode_string())
index = IndexParser(info, "./dictionaries/RuKrRu.idx").get_index()
count = 0
for i in index:
print("{0:8} {1:8} {2}".format(i[1], i[2], i[0]))
count += 1
if count == 20:
break
# print(info.version)
# print(info.wordcount)
fin = open("./dictionaries/RuKrRu.dict", "rb")
data_parser = DataParser(info, index, ByteStreamReader(fin))
data = data_parser.get_data()
print("number of entries:", len(data))
count = 0
for i in data:
word_str, details = i
print(word_str)
for det in details:
print(" type:", det[0])
print(" data:", det[1])
print()
count += 1
if count == 20:
break