-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiktionary-extract.py
executable file
·568 lines (425 loc) · 15.3 KB
/
wiktionary-extract.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/usr/bin/env python3
import os
import json
import re
import html
import argparse
from wikimunge import WikiMunge, WikiNode, NodeKind
json_args = dict(ensure_ascii = False, indent = 2, separators = (',', ': '))
chars = '0123456789aáâåäbcdeéfghijklmnoóöõpqrsšștuüvwxyzž '
configs = {
'fi': {
'lang': 'Suomi',
'lang_code': 'fi',
'upload_url': 'https://upload.wikimedia.org/wiktionary/fi',
'keywords': {
'Wiktionary': 'Wikisanakirja',
'Template': 'Malline',
'Module': 'Moduuli',
'Image': 'Kuva',
'File': 'Tiedosto',
'Appendix': 'Liite',
'Category': 'Luokka',
'thumbnail': 'pienoiskuva',
},
'pos': {
'substantiivi': 'noun',
'verbi': 'verb',
'adjektiivi': 'adjective',
'lyhenne': 'abbrev',
'adverbi': 'adverb',
'suffiksi': 'suffix',
'prefiksi': 'prefix',
'interjektio': 'interjection',
'pronomini': 'pronoun',
'numeraali': 'number',
'konjunktio': 'conjunction',
'postpositio': 'postposition',
'partikkeli': 'particle',
'supistuma': 'contraction',
'prepositio': 'preposition',
'infiksi': 'infix',
# 'erisnimi': 'name',
# 'aakkonen': 'letter',
# 'fraasi': 'phrase',
# 'symboli': 'symbol',
},
'section': {
'etymologia': 'etymology',
'ääntäminen': 'sounds',
'käännökset': 'translations',
'liittyvät sanat': 'related',
},
'related': {
'yläkäsitteet': 'hypernyms',
'vieruskäsitteet': 'cohyponyms',
'yhdyssanat': 'compounds',
'synonyymit': 'synonyms',
'johdokset': 'derived',
'alakäsitteet': 'hyponyms',
'vastakohdat': 'antonyms',
'tavutus': 'hyphenations',
'rinnakkaismuodot': 'alt_of',
'yhdyssanat ja sanaliitot': 'compounds',
'osakäsitteet': 'meronym',
'vastakohta': 'antonyms',
'lähikäsitteet': 'related',
'lyhenteet': 'abreviations',
}
},
'en': {
'lang': 'Finnish',
'lang_code': 'en',
'keywords': {},
'pos': {
'noun': 'noun',
'verb': 'verb',
'adjective': 'adjective',
'abbrev': 'abbrev',
'adverb': 'adverb',
'suffix': 'suffix',
'prefix': 'prefix',
'interjetion': 'interjection',
'pronoun': 'pronoun',
'number': 'number',
'conjunction': 'conjunction',
'postposition': 'postposition',
'particle': 'particle',
'contraction': 'contraction',
'postposition': 'preposition',
'infix': 'infix',
},
'section': {
'etymology': 'etymology',
'pronunciation': 'sounds',
'translations': 'translations',
},
'related': {
'hypernyms': 'hypernyms',
'cohyponyms': 'cohyponyms',
'compounds': 'compounds',
'synonyms': 'synonyms',
'derived': 'derived',
'hyponyms': 'hyponyms',
'antonyms': 'antonyms',
'hyphenations': 'hyphenations',
'alt_of': 'alt_of',
'compounds': 'compounds',
'meronym': 'meronym',
'related': 'related',
'abreviations': 'abreviations',
}
}
}
all_words = set()
def title_to_filename(title):
title = ''.join([c for c in title.strip() if c in chars])
return title.replace(' ', '_')
def match_node(node, kind, arg0 = None):
if not isinstance(kind, (list, tuple)): kind = [kind]
return (isinstance(node, WikiNode) and node.kind in kind and
(arg0 is None or node.args[0][0] == arg0))
def filter_node(node):
if isinstance(node, list):
results = []
for item in node:
result = filter_node(item)
if isinstance(result, list): results += result
else: results.append(result)
return results
if match_node(node, NodeKind.HLINE): return ''
if match_node(node, NodeKind.HTML) and node.args in ('span', 'nowiki'):
return filter_node(node.children)
if match_node(node, NodeKind.LINK):
for i in range(len(node.args)):
node.args[i] = [WikiNode.to_text(filter_node(node.args[i]))]
node.args[0][0] = re.sub(r'#.*$', '', node.args[0][0])
# Filter out Wiki links
if ':' in node.args[0][0]:
if len(node.args) == 1: return ''
else: return node.args[-1]
elif node.args[0][0] in all_words: return node
else: return node.args[-1][0]
if match_node(node, NodeKind.HTML):
node.attrs = {k: v for k, v in node.attrs.items()
if k not in ('class', 'lang')}
if isinstance(node, WikiNode):
node.children = filter_node(node.children)
return node
return node
def node_to_text(node):
text = WikiNode.to_text(filter_node(node))
return html.unescape(text).strip()
def extract_example(node, word):
children = []
trans = []
for child in node.children:
if match_node(child, NodeKind.LIST):
for item in child.children:
t = node_to_text(item.children)
if t: trans.append(t)
else: children.append(child)
text = node_to_text(children).replace('~', word)
if text:
d = dict(text = text)
if len(trans):
trans = node_to_text(trans)
if trans: d['trans'] = trans
return d
def extract_examples(node, word):
exs = []
for child in node.children:
if match_node(child, NodeKind.LIST_ITEM):
ex = extract_example(child, word)
if ex: exs.append(ex)
return exs if exs else None
def extract_sense(node, word):
# Extract examples
exs = None
for i in range(len(node.children)):
child = node.children[i]
if match_node(child, NodeKind.LIST):
exs = extract_examples(child, word)
del node.children[i]
break
# Extract sense
text = node_to_text(node.children)
if text or exs:
sense = dict()
if text: sense['glosses'] = [text]
if exs: sense['examples'] = exs
return sense
def extract_sound(node):
text = node_to_text(node)
if not text.startswith('Rhymes:'):
return text
def extract_sounds(node):
sounds = []
for child in node.children:
if match_node(child, NodeKind.LIST):
sounds += extract_sounds(child)
elif match_node(child, NodeKind.LIST_ITEM):
sounds.append(extract_sound(child.children))
elif isinstance(child, WikiNode):
sounds.append(extract_sound(child))
return [sound for sound in sounds if sound]
def extract_etymology(node):
return node_to_text(node.children)
def extract_related_list(node):
items = []
for child in node.children:
if match_node(child, NodeKind.LIST_ITEM):
text = node_to_text(child.children)
items += [x.strip() for x in text.split(',') if x.strip()]
return items
def extract_related_nav_frame(node):
items = []
for child in node.children:
if (match_node(child, NodeKind.HTML) and
'NavContent' in child.attrs.get('class', '')):
for e in child.children:
if match_node(e, NodeKind.LIST):
items += extract_related_list(e)
return items
def extract_to_related(node, related, category):
items = related.get(category, [])
for child in node.children:
if (match_node(child, NodeKind.HTML) and
child.attrs.get('class') == 'NavFrame'):
items += extract_related_nav_frame(child)
elif match_node(child, NodeKind.LIST):
items += extract_related_list(child)
elif isinstance(child, str):
text = node_to_text(child)
items += [x.strip() for x in text.split(',') if x.strip()]
if len(items): related[category] = sorted(set(items))
def extract_related(node):
related = {}
for child in node.children:
if match_node(child, NodeKind.LIST):
if not 'related' in related: related['related'] = []
related['related'] += extract_related_list(child)
if match_node(child, NodeKind.LEVEL5):
title = child.args[0][0].lower()
if title in config['related']:
category = config['related'][title]
extract_to_related(child, related, category)
return related
def extract_translations(node):
if not isinstance(node, WikiNode): return []
t = []
if 1 < len(node.children):
first = node.children[0]
if isinstance(first, str) and first == 'englanti: ':
t = [n.strip() for n in node_to_text(node.children[1:]).split(',')]
if not t:
for child in node.children: t += extract_translations(child)
return sorted(list(set(t)))
def extract_section(node, section):
if section == 'sounds': return extract_sounds(node)
if section == 'etymology': return extract_etymology(node)
if section == 'translations': return extract_translations(node)
if section == 'related': return extract_related(node)
def extract_senses(node, word):
senses = []
for child in node.children:
if match_node(child, NodeKind.LIST):
for item in child.children:
if match_node(item, NodeKind.LIST_ITEM):
sense = extract_sense(item, word)
if sense: senses.append(sense)
return senses
_levels = (NodeKind.LEVEL2, NodeKind.LEVEL3, NodeKind.LEVEL4, NodeKind.LEVEL5,
NodeKind.LEVEL6)
def extract_levels(node):
for child in node.children:
if match_node(child, _levels):
yield child
yield from extract_levels(child)
def extract_defs(node, word, lang):
defs = []
first_pos = True
d = dict(word = word, lang = lang)
#print('%r' % node)
for child in extract_levels(node):
if child.kind != NodeKind.LEVEL2:
title = ''.join(child.args[0]).split()[0].lower()
# POS
if title in config['pos']:
pos = config['pos'][title]
senses = extract_senses(child, word)
if first_pos:
first_pos = False
d |= dict(pos = pos, senses = senses)
else: d = dict(word = title, lang = lang, pos = pos, senses = senses)
defs.append(d)
# Sections
if title in config['section']:
section = config['section'][title]
d[section] = extract_section(child, section)
# Related
if title in config['related']:
category = config['related'][title]
if not 'related' in d: d['related'] = {}
extract_to_related(child, d['related'], category)
return dict(word = word, defs = defs)
def sanitize_text(text):
text = text.replace('\r', '')
text = re.sub(r'(?s)<strong class="error">.*</strong>', '', text)
text = re.sub(r'\[\[\s*Category:[^\]<>]*\]\]', '', text)
text = re.sub(r'data-[\w-]+="[^"]*"', '', text) # Parsing problems
text = re.sub(r'&#(\d+);', lambda m: chr(int(m.group(1))), text) # char refs
return text
class WiktionaryExtractor:
def __init__(self, config, outdir, threads, log = None, expand_only = False,
match_title = None, max_pages = None):
self.config = config
self.outdir = outdir
self.expand_only = expand_only
self.match_title = re.compile(match_title) if match_title else None
self.max_pages = max_pages
for name in ('dict', 'expanded'):
path = '%s/%s' % (outdir, name)
if not os.path.exists(path): os.makedirs(path)
self.load_titles()
tfilt = lambda title: not re.match(r'^fi-((decl)|(conj))', title)
self.munge = WikiMunge(
config['lang_code'], outdir, template_filter = tfilt, log = log,
threads = threads)
def load_titles(self):
path = self.outdir + '/titles.txt'
if not os.path.exists(path): self.titles = []
else:
with open(path, 'r') as f:
self.titles = [line.strip() for line in f]
def save_titles(self):
path = self.outdir + '/titles.txt'
if len(self.titles):
with open(path, 'w') as f:
for title in self.titles:
f.write(title + '\n')
def get_ns(self, ns): return self.config['namespaces'].get(ns, ns)
def save_dict_entry(self, path, data):
if os.path.exists(path):
with open(path, 'r') as f:
d = json.load(f)
d['defs'] += data['defs']
data = d
with open(path, 'w') as f:
json.dump(data, f, **json_args)
def extract_page(self, title, text):
if self.match_title and not self.match_title.match(title): return
filename = title.replace(' ', '_').replace('/', '_')
exp_path = self.outdir + '/expanded/%s.txt' % filename
if os.path.exists(exp_path) and not self.expand_only:
with open(exp_path, 'r') as f: text = f.read()
else:
text = self.munge.expand(title, text)
with open(exp_path, 'w') as f: f.write(text)
if self.expand_only: return
text = sanitize_text(text)
tree = self.munge.parse(title, text)
data = extract_defs(tree, title, config['lang'])
if data: return title, data
def page_handler(self, model, ns, title, text):
if model == 'redirect' or (
ns and ns != 'Talk' and not ns.endswith(' talk')):
self.munge.add_page(model, title, ''.join(text))
elif model == 'wikitext':
tag = '==%s==' % self.config['lang']
text = ''.join(text)
i = text.find(tag)
if i != -1 and (i == 0 or text[i - 1] in '\r\n'):
text = text[i:]
i = 0
while i != -1:
i = text.find('==', i + 1)
if i != -1 and text[i - 1] in '\r\n' and text[i + 2] != '=':
text = text[0 : i]
i = -1
self.titles.append(title)
self.munge.add_page(model, title, text)
def run(self, path):
global all_words
# Load pages
if not self.munge.cache.offset:
self.titles = []
self.munge.process(path, self.page_handler)
self.save_titles()
# Disable known bad templates
for name in ('slim-wikipedia', 'wikipedia', 'pedia', 'number box',
'specieslite'):
self.munge.cache.set_template('Template:' + name, '')
# Map titles
all_words = set(self.titles)
# Extract entries
titles = self.titles[0 : self.max_pages]
results = self.munge.reprocess(titles, self.extract_page)
# Save extracted data
for title, data in results:
filename = title_to_filename(title)
path = self.outdir + '/dict/%s.json' % filename
self.save_dict_entry(path, data)
parser = argparse.ArgumentParser(
prog = 'wiktionary-extract',
description = 'Extract language data from wiktionary dump files.')
parser.add_argument('filename', help = 'Wiktionary XML input file')
parser.add_argument('-l', '--lang', help = 'Input file language',
required = True, choices = configs.keys())
parser.add_argument('--expand-only', help = 'Only expand entries',
action = 'store_true')
parser.add_argument('-m', '--match', metavar = 'REGEX',
help = 'Only process entries with matching titles.')
parser.add_argument('--threads', type = int,
help = 'Number of processor threads to use.')
parser.add_argument('-n', '--max-pages', type = int,
help = 'Maximum number of pages to expand.')
args = parser.parse_args()
config = configs[args.lang]
outdir = 'output/' + args.lang
log = outdir + '/log.txt'
we = WiktionaryExtractor(
config, outdir, threads = args.threads, log = log,
match_title = args.match, expand_only = args.expand_only,
max_pages = args.max_pages)
we.run(args.filename)