-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_database.py
539 lines (434 loc) · 13.7 KB
/
create_database.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import xml.etree.ElementTree as ET
import sqlite3 as DB
import codecs
path = "."
class JKanji:
def __init__(self, value, infs, pris):
self.value = value
self.pris = pris
self.infs = infs
def value_to_str(self):
return self.value if self.value is not None else ""
def inf_to_str(self):
return "|".join(filter(None, self.infs))
def pri_to_str(self):
return "|".join(filter(None, self.pris))
class JReading:
def __init__(self, value, no_kanji, infs, pris, restr):
self.value = value
self.no_kanji = no_kanji
self.infs = infs
self.pris = pris
self.restr = restr
def value_to_str(self):
return self.value if self.value is not None else ""
def inf_to_str(self):
return "|".join(filter(None, self.infs))
def pri_to_str(self):
return "|".join(filter(None, self.pris))
def restr_to_str(self):
return "|".join(filter(None, self.restr))
def no_kanji_to_str(self):
return "T" if self.no_kanji else "F"
class Gloss:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
class Example:
def __init__(self, text, sentences):
self.text = text
self.sentences = sentences
def sentences_to_str(self):
return "/".join(filter(None, self.sentences))
class Sense:
def __init__(self, ants, pos, fields, dials, glosses, exs):
self.ants = ants
self.pos = pos
self.fields = fields
self.dials = dials
self.glosses = glosses
self.exs = exs
def ant_to_str(self):
return "|".join(filter(None, self.ants))
def pos_to_str(self):
return "|".join(filter(None, self.pos))
def fields_to_str(self):
return "|".join(filter(None, self.fields))
def gloss_to_str(self):
return "|".join(filter(None, map(lambda x: x.to_str(), self.glosses)))
def dials_to_str(self):
return "|".join(filter(None, self.dials))
def ex_text_to_str(self):
return "|".join(filter(None, map(lambda x: x.text, self.exs)))
def ex_sent_to_str(self):
return "|".join(filter(None, map(lambda x: x.sentences_to_str(), self.exs)))
class DicReference:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
class QCode:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
class Reading:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
class Radical:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
class Variant:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
class Meaning:
def __init__(self, value, _type):
self.value = value
self._type = _type
def to_str(self):
return "{}-{}".format(self.value, self._type)
def create_tables(cur):
cur.execute(
'''
CREATE TABLE IF NOT EXISTS entry(
id INTEGER NOT NULL PRIMARY KEY,
keb TEXT,
ke_inf TEXT,
ke_pri TEXT,
re TEXT,
re_nokanji TEXT,
re_restr TEXT,
re_inf TEXT,
re_pri TEXT,
ant TEXT,
pos TEXT,
field TEXT,
dial TEXT,
gloss TEXT,
ex_text TEXT,
ex_sent TEXT
)
'''
)
cur.execute(
'''
CREATE TABLE IF NOT EXISTS kanji(
literal TEXT NOT NULL,
radical TEXT,
grade TEXT,
stroke_count INTEGER,
variant TEXT,
freq INTEGER,
rad_name TEXT,
jlpt INTEGER,
dic_number TEXT,
reading TEXT,
meaning TEXT,
nanori TEXT,
q_code TEXT
)
'''
)
cur.execute(
'''
CREATE TABLE IF NOT EXISTS sentence(
japanese TEXT,
english TEXT
)
'''
)
cur.execute(
'''
CREATE TABLE IF NOT EXISTS jlpt(
word TEXT,
level INTEGER
)
'''
)
def parse_jmdict(cur):
tree = ET.parse('{}/jmdict.xml'.format(path))
root = tree.getroot()
entries = []
i = 0
for entry in root.iter('entry'):
i += 1
if i == 10000:
i = 0
cur.execute('BEGIN TRANSACTION')
for (_id, keb, ke_inf, ke_pri, re, re_nokanji, re_restr, re_inf, re_pri, ant, pos, field, dial, gloss, ex_text, ex_sent) in entries:
statement = '''
INSERT INTO entry(id, keb, ke_inf, ke_pri, re, re_nokanji, re_restr, re_inf, re_pri, ant, pos, field, dial, gloss, ex_text, ex_sent)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
cur.execute(statement, [_id, keb, ke_inf, ke_pri, re, re_nokanji, re_restr, re_inf, re_pri, ant, pos, field, dial, gloss, ex_text, ex_sent])
entries = []
cur.execute('COMMIT')
_id = entry.find('ent_seq').text
kanjis = []
readings = []
senses = []
for kanji in entry.iter('k_ele'):
item = JKanji(kanji.find('keb').text,
map(lambda x: x.text, kanji.findall('ke_inf')),
map(lambda x: x.text, kanji.findall('ke_pri'))
)
kanjis.append(item)
print('Kanji {}'.format(item.value))
for reading in entry.iter('r_ele'):
item = JReading(
reading.find('reb').text,
reading.find('re_nokanji') is not None,
map(lambda x: x.text, reading.findall('re_inf')),
map(lambda x: x.text, reading.findall('re_pri')),
map(lambda x: x.text, reading.findall('re_restr'))
)
readings.append(item)
print('Reading {}'.format(item.value))
for sense in entry.iter('sense'):
glosses = []
examples = []
for gloss in sense.findall('gloss'):
glosses.append(Gloss(gloss.text, gloss.attrib.get('g_type')))
for example in sense.findall('example'):
text = example.find('ex_text').text
sentences = map(lambda x: x.text, example.findall('ex_sent'))
examples.append(Example(text, sentences))
item = Sense(
map(lambda x: x.text, sense.findall('ant')),
map(lambda x: x.text, sense.findall('pos')),
map(lambda x: x.text, sense.findall('fields')),
map(lambda x: x.text, sense.findall('dials')),
glosses,
examples
)
senses.append(item)
print(item)
entries.append((
_id,
";".join(map(lambda x: x.value_to_str(), kanjis)),
";".join(map(lambda x: x.inf_to_str(), kanjis)),
";".join(map(lambda x: x.pri_to_str(), kanjis)),
";".join(map(lambda x: x.value_to_str(), readings)),
";".join(map(lambda x: x.no_kanji_to_str(), readings)),
";".join(map(lambda x: x.restr_to_str(), readings)),
";".join(map(lambda x: x.inf_to_str(), readings)),
";".join(map(lambda x: x.pri_to_str(), readings)),
";".join(map(lambda x: x.ant_to_str(), senses)),
";".join(map(lambda x: x.pos_to_str(), senses)),
";".join(map(lambda x: x.fields_to_str(), senses)),
";".join(map(lambda x: x.dials_to_str(), senses)),
";".join(map(lambda x: x.gloss_to_str(), senses)),
";".join(map(lambda x: x.ex_text_to_str(), senses)),
";".join(map(lambda x: x.ex_sent_to_str(), senses))
))
cur.execute('BEGIN TRANSACTION')
for (_id, keb, ke_inf, ke_pri, re, re_nokanji, re_restr, re_inf, re_pri, ant, pos, field, dial, gloss, ex_text, ex_sent) in entries:
statement = '''
INSERT INTO entry(id, keb, ke_inf, ke_pri, re, re_nokanji, re_restr, re_inf, re_pri, ant, pos, field, dial, gloss, ex_text, ex_sent)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
cur.execute(statement, [_id, keb, ke_inf, ke_pri, re, re_nokanji, re_restr, re_inf, re_pri, ant, pos, field, dial, gloss, ex_text, ex_sent])
cur.execute('COMMIT')
def parse_kanjidic(cur):
tree = ET.parse('{}/kanjidic.xml'.format(path))
root = tree.getroot()
entries = []
i = 0
for entry in root.iter('character'):
i += 1
if i == 10000:
cur.execute('BEGIN TRANSACTION')
i = 0
for (literal, radical, grade, stroke_count, variant, freq, rad_name, jlpt, dic_number, reading, meaning, nanori, q_code) in entries:
statement = '''
INSERT INTO kanji(literal, radical, grade, stroke_count, variant, freq, rad_name, jlpt, dic_number, reading, meaning, nanori, q_code)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
cur.execute(statement, [literal, radical, grade, stroke_count, variant, freq, rad_name, jlpt, dic_number, reading, meaning, nanori, q_code])
entries = []
cur.execute('COMMIT')
literal = entry.find('literal').text
jlpt = None
freq = None
grade = None
stroke_count = None
variants = []
rad_names = []
if entry.find('misc') is not None:
if entry.find('misc').find('jlpt') is not None:
jlpt = int(entry.find('misc').find('jlpt').text)
if entry.find('misc').find('freq') is not None:
freq = int(entry.find('misc').find('freq').text)
if entry.find('misc').find('grade') is not None:
grade = entry.find('misc').find('grade').text
if entry.find('misc').find('stroke_count') is not None:
stroke_count = int(entry.find('misc').find('stroke_count').text)
if entry.find('misc').find('variant') is not None:
for variant in entry.find('misc').find('variant').findall('radvalue'):
variants.add(Variant(variant.text, variant.attrib.get('rad_type')))
for rad_name in entry.find('misc').findall('rad_name'):
rad_names.append(rad_name.text)
dic_refs = []
q_codes = []
readings = []
meanings = []
nanoris = []
radicals = []
if entry.find('dic_number') is not None:
for dic in entry.find('dic_number').findall('dic_ref'):
dic_refs.append(DicReference(dic.text, dic.attrib.get('dr_type')))
if entry.find('query_code') is not None:
for qcode in entry.find('query_code').findall('q_code'):
q_codes.append(QCode(qcode.text, qcode.attrib.get('qc_type')))
if entry.find('reading_meaning') is not None:
if entry.find('reading_meaning').find('rmgroup') is not None:
for re in entry.find('reading_meaning').find('rmgroup').findall('reading'):
readings.append(Reading(re.text, re.attrib.get('r_type')))
for meaning in entry.find('reading_meaning').find('rmgroup').findall('meaning'):
meanings.append(Meaning(meaning.text, meaning.attrib.get('m_lang')))
for nanori in entry.find('reading_meaning').findall('nanori'):
nanoris.append(nanori.text)
if entry.find('radical') is not None:
for radical in entry.find('radical').findall('rad_value'):
radicals.append(Radical(radical.text, radical.attrib.get('rad_type')))
print('Kanji {}'.format(literal))
entries.append((
literal,
";".join(map(lambda x: x.to_str(), radicals)),
grade,
stroke_count,
";".join(map(lambda x: x.to_str(), variants)),
freq,
";".join(rad_names),
jlpt,
";".join(map(lambda x: x.to_str(), dic_refs)),
";".join(map(lambda x: x.to_str(), readings)),
";".join(map(lambda x: x.to_str(), meanings)),
";".join(nanoris),
";".join(map(lambda x: x.to_str(), q_codes))
))
cur.execute('BEGIN TRANSACTION')
for (literal, radical, grade, stroke_count, variant, freq, rad_name, jlpt, dic_number, reading, meaning, nanori, q_code) in entries:
statement = '''
INSERT INTO kanji(literal, radical, grade, stroke_count, variant, freq, rad_name, jlpt, dic_number, reading, meaning, nanori, q_code)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
cur.execute(statement, [literal, radical, grade, stroke_count, variant, freq, rad_name, jlpt, dic_number, reading, meaning, nanori, q_code])
cur.execute('COMMIT')
def parse_sentences(cur):
file = open('{}/sentences.tsv'.format(path))
lines = file.readlines()
entries = []
i = 0
for line in lines:
i += 1
if i == 10000:
print('commit')
cur.execute('BEGIN TRANSACTION')
for (en, jp) in entries:
statement = '''
INSERT INTO sentence(japanese, english)
VALUES(?, ?)
'''
cur.execute(statement, [jp, en])
entries = []
cur.execute('COMMIT')
i = 0
(en, jp) = line.split('|')
entries.append((unicode(en, 'utf-8'), unicode(jp, 'utf-8')))
cur.execute('BEGIN TRANSACTION')
for (en, jp) in entries:
statement = '''
INSERT INTO sentence(japanese, english)
VALUES(?, ?)
'''
cur.execute(statement, [jp, en])
cur.execute('COMMIT')
def parse_jlpt(cur, jlpt):
file = open('{}/n{}'.format(path, jlpt))
lines = file.readlines()
entries = []
i = 0
for line in lines:
i += 1
if i == 10000:
print('commit')
cur.execute('BEGIN TRANSACTION')
for entry in entries:
statement = '''
INSERT INTO jlpt(word, level)
VALUES(?, ?)
'''
cur.execute(statement, [entry, jlpt])
entries = []
cur.execute('COMMIT')
i = 0
entries.append(line)
cur.execute('BEGIN TRANSACTION')
for entry in entries:
statement = '''
INSERT INTO jlpt(word, level)
VALUES(?, ?)
'''
cur.execute(statement, [entry, jlpt])
cur.execute('COMMIT')
import csv
def parse_conjo(cur):
cur.execute(
'''
CREATE TABLE IF NOT EXISTS conjugation(
pos INTEGER,
conj INTEGER,
negative TEXT,
formal TEXT,
onum INTEGER,
stem INTEGER,
okurigana TEXT,
euphr TEXT,
euphk TEXT
);
'''
)
cur.execute('BEGIN TRANSACTION')
with open('{}/conjo.csv'.format(path), newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
next(reader)
for row in reader:
print(', '.join(row[0].split('\t')[:-1]))
statement = '''
INSERT INTO conjugation(pos, conj, negative, formal, onum, stem, okurigana, euphr, euphk)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
'''
cur.execute(statement, row[0].split('\t')[:-1])
cur.execute('COMMIT')
conn = DB.connect('{}/jmdict.db'.format(path))
cur = conn.cursor()
# print('create tables')
# create_tables(cur)
# print('parse jmdict')
# parse_jmdict(cur)
# print('parse kanjidic')
# parse_kanjidic(cur)
# print('parse sentences')
# parse_sentences(cur)
# print('parse jlpt')
# parse_jlpt(cur, 2)
# parse_jlpt(cur, 3)
# parse_jlpt(cur, 4)
# parse_jlpt(cur, 5)
print('parse conjugation')
parse_conjo(cur)
conn.close()