-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
252 lines (199 loc) · 6.43 KB
/
parse.py
File metadata and controls
252 lines (199 loc) · 6.43 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
# -*- coding: utf-8 -*-
import gzip
import json
import os
import re
import sqlite3
import sys
from collections import OrderedDict
from bs4 import BeautifulSoup
INFLECTIONS = {}
ENTRIES = {}
FIX_TEXT_PATTERNS = {
' ': re.compile(r'\s+'),
'(': re.compile(r'\(\s+'),
')': re.compile(r'\s+\)'),
', ': re.compile(r'\s*,\s*'),
' + ': re.compile(r'\s*\+\s*'),
'/': re.compile(r'\s*\/\s*'),
}
def decrypt_blob(blob):
"""Decrypt blob to text"""
# Keys to decrypt
BLOB_KEY_A = 2
BLOB_KEY_B = 7
org_length = len(blob)
length = org_length / BLOB_KEY_A
length = int(length)
tmp = [0] * org_length
for x in range(0, length * 2, 2):
if x > (BLOB_KEY_B * 2) + length:
tmp[x] = blob[x]
tmp[x + 1] = blob[x + 1]
else:
tmp[x] = blob[x + 1]
tmp[x + 1] = blob[x]
if org_length % 2 == 1:
tmp[org_length - 1] = blob[org_length - 1]
output = gzip.decompress(bytearray(tmp))
return str(output, 'utf8')
def fix_text(text):
text = text.strip()
for rpl in FIX_TEXT_PATTERNS:
text = FIX_TEXT_PATTERNS[rpl].sub(rpl, text)
return text
def restore_html(html_doc):
"""
TFlat uses custom tags to shorten HTML content.
This method restores it back.
"""
return (html_doc.replace('<d1', '<div class="')
.replace('<d3>', '</div></div></div>')
.replace('<a1', '<a href="')
.replace('<s1', '<span class="')
.replace('<s2>', '</span></span>'))
def parse_tab_content(content, entry={}):
if not content or len(content) <= 3:
return entry
if not content.startswith('<div'):
content = fix_text(content)
content = '<div><div class="m">{}</div></div>'.format(content)
soup = BeautifulSoup(content, 'html.parser')
root = soup.div
# Unwrap ul, li
for elm in root.find_all(['ul', 'li']):
elm.unwrap()
pronunciation = ''
try:
pronunciation = root.find('div', attrs={'class': 'p5l fl'}).string
except Exception:
w = root.find(attrs={'class': 'w'})
if w:
parent = w.parent
w.decompose()
pronunciation = parent.text
if pronunciation and 'pronunciation' not in entry:
pronunciation = pronunciation.strip()
if pronunciation:
entry['pronunciation'] = pronunciation
try:
body = root.find(attrs={'class': re.compile(r'^[meidub]{1,2}$')}).parent
except Exception:
return entry
# Unwrap e/em
for elm_m in body.find_all(attrs={'class': 'm'}):
tmp = []
for elm in elm_m.find_all(attrs={'class': re.compile(r'^em?$')}):
tmp.append(elm.extract())
tmp.reverse()
for t in tmp:
elm_m.insert_after(t)
if 'parts' in entry:
parts = entry['parts']
else:
parts = OrderedDict()
parts['_'] = {
'meanings': OrderedDict(),
'phrases': OrderedDict(),
}
current_part = '_'
current_meaning = '_'
current_example = ''
current_example_meaning = ''
current_phrase = ''
current_phrase_meaning = ''
for child in body.find_all(attrs={'class': re.compile(r'^[meidub]{1,2}$')}):
if isinstance(child, str):
continue
text = fix_text(child.text)
if 'ub' in child['class'] or 'b' in child['class']:
current_part = text.lower().strip()
if 'current_part' not in parts:
parts[current_part] = {
'meanings': OrderedDict(),
'phrases': OrderedDict(),
}
elif 'm' in child['class']:
current_meaning = text
parts[current_part]['meanings'][current_meaning] = OrderedDict()
elif 'e' in child['class']:
current_example = text
elif 'em' in child['class']:
current_example_meaning = text
if current_meaning not in parts[current_part]['meanings']:
parts[current_part]['meanings'][current_meaning] = OrderedDict()
parts[current_part]['meanings'][current_meaning][current_example] = current_example_meaning
elif 'id' in child['class']:
current_phrase = text
elif 'im' in child['class']:
current_phrase_meaning = text
parts[current_part]['phrases'][current_phrase] = current_phrase_meaning
entry['parts'] = parts
return entry
def parse_word(row):
"""Parse word"""
global INFLECTIONS, ENTRIES
word, blob, mean = row
print(word)
# Word starts with '@' or '(xem)' = infection
if mean.startswith('@') or mean.startswith('(xem)'):
inf = ''
# Remove '@' & '(xem)' prefixes
if mean.startswith('(xem)'):
inf = mean[5:]
else:
inf = mean[1:]
inf = inf.strip().rstrip('#')
if inf:
inf = inf.replace('_', ' ')
if inf not in INFLECTIONS:
INFLECTIONS[inf] = set()
INFLECTIONS[inf].add(word)
return
if len(blob) == 0:
return
# If first 3 chars are the same -> encrypted blob
if len(blob) > 3 and blob[0] == blob[1] == blob[2]:
data = decrypt_blob(blob[3:])
else:
data = str(blob, 'utf8')
data = restore_html(data)
# Main, eng-eng, technical, synonym, grammar
tab_contents = data.split('##')
entry = parse_tab_content(tab_contents[0], {})
if entry and len(tab_contents) > 2:
entry = parse_tab_content(tab_contents[2], entry)
ENTRIES[word] = entry
def parse_file(db_file):
# Connect to DB
db = sqlite3.connect(db_file)
cursor = db.cursor()
# Query all words
query = 'SELECT word, av, mean FROM av'
cursor.execute(query)
rows = cursor.fetchall()
for row in rows:
parse_word(row)
def serialize_sets(obj):
"""Make set JSON serializable"""
if isinstance(obj, set):
return list(obj)
return obj
def main():
args = sys.argv
if len(args) == 1:
print('parser.py <database-file>')
return
db_file = args[1]
if not os.path.isfile(db_file):
print(f'"{db_file}" is not a valid file!')
return
parse_file(db_file)
output = {
'entries': ENTRIES,
'inflections': INFLECTIONS,
}
with open('output.json', 'w') as fp:
json.dump(output, fp, indent=2, default=serialize_sets, ensure_ascii=False)
if __name__ == '__main__':
main()