-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_de_ipa.py
302 lines (243 loc) · 11.6 KB
/
extract_de_ipa.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
import html
import regex
# modes:
check = False # or True
test = False # or True
#
patternRef = "<ref.*?(?:>.*?</ref|/)>" # <ref>
patternWords = "\w+(?:\s+\w+)*"
patternWordsComma = "\w+(?:[,/\s]+\w+)*"
def getPatternIpa(name: str) -> str:
return ("\{\{Lautschrift\??\s?\|"
+ "(?:spr=[^|}]+\|)?" # |spr=xx|(IPA)
+ "(?P<" + name + ">[^|}]*?)" # (IPA)
+ "(?:\|spr=[^}]+)?" # (IPA)|spr=xx
+ "\}\}"
)
patternStr = ("^"
+ "(?:(?:(?:\[.+?\])|die|der|das|ein |A\)|bei Plural \d:)\s*)?" # references [1] etc, occasional article
+ "(?:<!--.*?-->\s*)?" # HTML comment
+ "(?P<comment0>" # various descriptions before IPA
+ "(?:\*?''.+?'')?\s*"
+ "(?:" + patternRef + ")?\s*"
+ "(?:<!-- Spezialfall, NICHT löschen -->)?(\{\{(?:[^}]+?)\}\})?"
+ "(?:(?:(?:" + patternWords + ")|"
+ "\{\{nordd\.\}\} ''und \{\{md\.\}\}:''|"
+ "''standardsprachlich \(Schweiz\)''):?)?\s*"
+ "(?:\{\{(?:nordd|südd)\.(?:\|\:)?\}\}\s*)?"
+ ")"
# {{Lautschrift|}} template variants
+ getPatternIpa("ipa0") # + "\{\{Lautschrift\??\s?\|(?P<ipa0>[^}]*?)\}\}"
+ "(?:\s*(~|oder|''oder''|<small>auch</small>|(?:''(?:regional|, auch|auch|, beziehungsweise):?''))\s*"
+ getPatternIpa("ipa1") # + "\{\{Lautschrift\??\s?\|(?P<ipa1>[^}]*?)\}\}"
+ ")?\s*"
+ "(?:[;,]?" + patternRef + ")*\s*"
# ignore (rest of line)
+ "(?:<!--.*?-->\s*)?" # HTML comment
+ "(?:<small>\(Beˈtonung fehlt\)</small>\s*)?"
+ "(?:\((?:zu weiteren Aussprachevarianten siehe|vergleiche) .*?\)\s*)?"
+ "(?:\(stimmloses s im Anlaut, da nur noch in Österreich üblich\))?"
+ "(?:\(\[\[.*?\]\]\)\s*)?"
+ "(?:[\(,;]?\s*\{\{(?:Pl|Gen|Sg|Part|attr|Prät)\..+)?"
+ "(?:,\s*(?:"
+ "(?:''Varianten siehe.+?'')|"
+ "(?:weitere Aussprache(?:variante)?n siehe Haupteintrag)|"
+ "(?:sonst siehe \[\[.*?\]\])|"
+ "(?:(?:das|ein|die|der) " + getPatternIpa("dummy") # \{\{Lautschrift\|.+?\}\}
+ ".*)|" # ignore "japanisch, das japanische" etc for languages, "der achte, ein achter"
+ "(?:''schweizerisch:'' '''\?''')|"
+ "(?:\{\{(?:kSt\.|kPl\.)\}\})|"
+ "(?:<!-- Spezialfall, NICHT löschen -->\s*\{\{.*)"
+ "))?\s*"
# capture comment after IPA
+ "(?P<commentAfter0>(?:"
+ "(?:[,]?\s*"
+ "(?:" + patternWords + ")|"
+ "(?:\((?:" + patternWords + "|DIN-Aussprache)\))|"
+ "(?:''.+?'':?)?\s*"
+ ")?)"
+ ")"
+ "(?P<repeat>[,/;\|]\s*" # repeat IPA clauses
+ "(?:" + patternRef + ")*\s*"
+ "(?:\w\))?"
+ "(?:\[.*?\])?"
+ "(?P<comment>" # various descriptions before subsequent IPAs
+ "(?:''.+?'':?)?\s*"
+ "(?:<!-- Spezialfall, NICHT löschen -->)?\s*"
+ "(?:(?:"
+ "(?:" + patternWordsComma + ")|"
+ "(?:''.+?'')|"
+ "(\(''" + patternWords+ ":?'')|"
+ "(?:\[\[" + patternWords + "\]\])|"
+ "(?:\{\{(?:österr|reg|südd|nordd|ugs|schweiz)\.(?:\|\:)?\}\}(?: auch)?)|"
+ "(?:\{\{K\|[^}]*\}\})|"
+ "''ursprünglich'' und ''regional:''|(\{\{reg.\}\} ''auch:'')|<small>auch</small>|"
+ "<small>veraltet oder gehoben veraltend:</small>|"
+ "''Ost- und Westmitteldeutsch:'' und \{\{ugs\.\}\}|"
+ "\{\{schweiz\.\}\} ''auch:''"
+ "):?)?\s*"
+ "(?:,?" + patternRef + ")*\s*" # <ref>s
#+ "(?P<special>(?:\{\{[^}]+?\}\}(?:\s+auch))?)\s*"
+ ")"
+ getPatternIpa("ipa") # + "\{\{Lautschrift\??\s?\|(?P<ipa>[^}]*?)\}\}"
+ "(?:,?" + patternRef + ")*" # <ref>s
+ "\)?"
+ "\s*"
+ "(?:\((?:zu weiteren Aussprachevarianten siehe|vergleiche) .*?\)\s*)?"
+ "(?:\(sogenanntes.+?\))?"
+ "(?:\(\[\[.*?\]\]\)\s*)?"
+ "(?:[\(,;]?\s*\{\{(?:Pl|Gen|Sg|Part|attr|Prät)\..+)?"
+ "(?::\{\{Hörbeispiele\}\}\+\{\{Audio\|\}\})?"
+ "(?:; Plural .+?)?"
+ "(?:''\(?mit Plural \d\)?'')?"
+ "(?P<commentAfter>"
+ "(?:"
+ "(?:''\(englisch\)'')|"
+ "(?:\((?:" + patternWords + ")\))|"
+ "(?:(?:,\s)?immer unbetont)"
+ ")?"
+ ")"
+ ")*"
+ "(?:&nbsp;)?"
+ "[;,snəɪ\)]*" # typos at end of entry
+ "$")
if check or test:
print(patternStr)
rexEntry = regex.compile(patternStr)
rexFallback = regex.compile("^"
+ "(?:(?:(?:Hochdeutsch|original|bundesdeutsches Hochdeutsch|Positiv \d):?)\s*)?"
+ getPatternIpa("ipa") # + "\{\{Lautschrift\??\s?\|(?P<ipa>[^}]*?)\}\}"
)
rexIpa = regex.compile("^:\{\{IPA(?:\|[^}]*)?\}\}\s*")
pageTitle = ""
langFound = False
pronFound = False
commentsTable = str.maketrans("", "", "'()[]{}:")
def formatColumn(s: str) -> str:
s = regex.sub("(\u2060|\u200b)", "", html.unescape(s))
return "\"" + s + "\"" if "," in s else s
def formatComments(c: str, ca: str) -> str:
c = c.strip().translate(commentsTable)
ca = ca.strip().translate(commentsTable)
if c == "":
return ca
if ca == "":
return c
return c + " " + ca
ipaChars = set()
searchIpa = ""
def printIpa(pageTitle: str, ipa: str, comments: str = ""):
global searchIpa
plainIpa = html.unescape(html.unescape(ipa)) # MediaWiki markup inside XML text
plainIpa = regex.sub("(\u2060|\u200b)", "", plainIpa)
#if '1' in plainIpa:
# searchIpa = pageTitle
for ch in plainIpa:
ipaChars.add(ch)
print(formatColumn(pageTitle) + "," + formatColumn("/" + plainIpa + "/") + (("," + formatColumn(comments)) if comments != "" else ""))
def isValidIpa(s: str) -> bool:
return s != None and s.strip() != "" and not s.startswith("-") and not "…" in s
with open('../wikidata/dewiktionary-20221120-pages-meta-current.xml', newline='') as f:
while True:
line = f.readline()
if not line:
break
if line.find("</page>") > -1:
pageTitle = ""
pronFound = False
continue
if line.find("<title>") > -1:
pt = line.replace("<title>", "").replace("</title>", "").strip()
if pt.find(":") > -1: # skip pages in namespaces
continue
if pt.startswith("-"): # don't include suffixes
continue
pageTitle = pt
continue
if pageTitle == "":
continue
if line.startswith("==") and line.find("|Deutsch}}") > -1:
langFound = True
continue
elif line.startswith("=="):
langFound = False
continue
if langFound and line.startswith("{{Aussprache}}"):
pronFound = True
inIpa = False
continue
elif line.startswith("{{"):
pronFound = False
inIpa = False
continue
if not pronFound:
continue
if rexIpa.match(line): # line.startswith(":{{IPA}}"):
inIpa = True
elif inIpa and not line.startswith("::"):
inIpa = False
if not inIpa:
continue
if inIpa:
# line.removeprefix(":{{IPA}}")
line = regex.sub(rexIpa, "", line).strip().removeprefix("::").strip().removeprefix(":").strip().removeprefix("{{IPA}}").strip()
# clean-up :{{IPA}} and :: and various typos
if len(line) == 0: # empty :{{IPA}} heading
continue
if line.find("{{Lautschrift") == -1:
continue
match = rexEntry.match(line)
matchFallback = rexFallback.match(line) if not match else None
if check:
if not match:
print(pageTitle + " - no regex match: " + line)
if not matchFallback:
#print(pageTitle + " - no regex match: " + line)
print(pageTitle + " - no fallback regex match, giving up")
continue
print("fallback ipa '{ipa}'".format(ipa = matchFallback["ipa"]))
continue
if test:
print(pageTitle + " - " + line)
if match:
print("ipa0 '{ipa0}' ipa1 '{ipa1}' comment '{comment}' commentAfter '{commentAfter}'".format(
ipa0 = match["ipa0"], ipa1 = match["ipa1"], comment = match["comment0"], commentAfter = match["commentAfter0"]
))
l = len(match.captures("repeat"))
for i in range(l):
print("ipa '{ipa}' comment '{comment}' commentAfter '{commentAfter}'".format(
ipa = match.captures("ipa")[i], comment = match.captures("comment")[i],
commentAfter = match.captures("commentAfter")[i]
))
continue
if matchFallback:
print("fallback ipa '{ipa}'".format(ipa = matchFallback["ipa"]))
continue
print(pageTitle + " - no fallback regex match, giving up")
continue
# prod: not check and not test
if match:
if not isValidIpa(match["ipa0"]):
continue
desc = formatComments(match["comment0"], match["commentAfter0"])
printIpa(pageTitle, match["ipa0"], desc)
if isValidIpa(match["ipa1"]):
printIpa(pageTitle, match["ipa1"], desc)
l = len(match.captures("repeat"))
for i in range(l):
prevDesc = desc
desc = formatComments(match.captures("comment")[i], match.captures("commentAfter")[i])
if desc == "":
desc = prevDesc
ipa = match.captures("ipa")[i]
if isValidIpa(ipa):
printIpa(pageTitle, ipa, desc)
if matchFallback:
if not isValidIpa(matchFallback["ipa"]):
continue
printIpa(pageTitle, matchFallback["ipa"])
f.close()
# used to debug IPA
#print("characters in IPA")
#print(ipaChars)
#print(searchIpa)