-
Notifications
You must be signed in to change notification settings - Fork 84
/
wiktionary_extract_wrt.py
executable file
·136 lines (100 loc) · 3.43 KB
/
wiktionary_extract_wrt.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2018 Guenter Bartsch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# extract wkt (word replacement table) entries from german wiktionary
#
import os
import sys
import xml.sax
import re
import string
import codecs
from nltools import misc
PROC_TITLE = 'wiktionary_extract_ipa'
# ARTICLE_LIMIT = 10000
ARTICLE_LIMIT = 0
WRTFN = 'data/src/wrt/wiktionary.csv'
# {{Alte Schreibweise|Kirchturm|Reform 1901}}
WRT_PATTERN = re.compile(r"^\{\{Alte Schreibweise\|([^}|]+)")
article_cnt = 0
wrt_cnt = 0
class ArticleExtractor(xml.sax.ContentHandler):
def __init__(self):
xml.sax.ContentHandler.__init__(self)
def startElement(self, name, attrs):
#print("startElement '" + name + "'")
self.ce = name
if name == 'title':
self.title = ''
if name == 'text':
self.text = ''
def characters(self, content):
if self.ce == 'title':
self.title += content
elif self.ce == 'text':
self.text += content
def endElement(self, name):
global article_cnt, wrt_cnt, dictf
#print("endElement '" + name + "'")
if name == 'page':
article_cnt += 1
title = self.title.lstrip().rstrip().lower()
body = self.text.lstrip().rstrip()
wrt = None
german = False
for line in body.split('\n'):
if not wrt:
m = WRT_PATTERN.match(line)
if m:
wrt = m.group(1).lower()
# print 'matched: %s -> %s' % (line, wrt)
if not german:
if u'{{Sprache|Deutsch}}' in line:
german = True
if not german:
print "%7d %7d %s NOT GERMAN." % (article_cnt, wrt_cnt, title)
return
if not wrt:
print "%7d %7d %s NO WRT ENTRY FOUND." % (article_cnt, wrt_cnt, title)
return
if u' ' in wrt or u' ' in title:
print "%7d %7d %s SPACE." % (article_cnt, wrt_cnt, title)
return
wrt_cnt += 1
print u"%7d %7d %s WRT: %s" % (article_cnt, wrt_cnt, title, wrt)
wrtf.write(u" %-20s: u'%s',\n" % ("u'"+title+"'", wrt))
if ARTICLE_LIMIT and article_cnt >= ARTICLE_LIMIT:
sys.exit(0)
#
# init
#
misc.init_app(PROC_TITLE)
#
# load config, set up global variables
#
config = misc.load_config ('.speechrc')
wikfn = config.get("speech", "wiktionary_de")
#
# main program: SAX parsing of wiktionary dump
#
wrtf = codecs.open(WRTFN, 'w', 'utf8')
source = open(wikfn)
xml.sax.parse(source, ArticleExtractor())
wrtf.close()
print "%s written." % WRTFN