-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
406 lines (339 loc) · 10.8 KB
/
main.py
File metadata and controls
406 lines (339 loc) · 10.8 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
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
from __future__ import annotations
import argparse
import csv
import re
from pathlib import Path
import xml.etree.ElementTree as ET
NS = {"tei": "http://www.tei-c.org/ns/1.0"}
SWISS_CANTONS = {
"AG": "Aargau",
"AI": "Appenzell Innerrhoden",
"AR": "Appenzell Ausserrhoden",
"BE": "Bern",
"BL": "Basel-Landschaft",
"BS": "Basel-Stadt",
"FR": "Freiburg",
"GE": "Genf",
"GL": "Glarus",
"GR": "Graubuenden",
"JU": "Jura",
"LU": "Luzern",
"NE": "Neuenburg",
"NW": "Nidwalden",
"OW": "Obwalden",
"SG": "St. Gallen",
"SH": "Schaffhausen",
"SO": "Solothurn",
"SZ": "Schwyz",
"TG": "Thurgau",
"TI": "Tessin",
"UR": "Uri",
"VD": "Waadt",
"VS": "Wallis",
"ZG": "Zug",
"ZH": "Zuerich",
}
NO_SPACE_BEFORE = {
".",
",",
";",
":",
"!",
"?",
")",
"]",
"}",
"»",
"”",
"›",
"%",
"...",
}
NO_SPACE_AFTER = {"(", "[", "{", "«", "\"", "„", "‚", "‹"}
TERMINAL_END = (".", "!", "?", "…")
STARTS_WITH_CLOSER = ("»", "”", "’", ")", "]", "}", ",", ";", ":")
ABBREV_ENDINGS = ("geb.", "gest.", "vgl.", "ca.", "u.a.", "etc.")
def detokenize(tokens: list[str]) -> str:
parts: list[str] = []
for token in tokens:
tok = token.strip()
if not tok:
continue
if not parts:
parts.append(tok)
continue
prev = parts[-1]
if tok in NO_SPACE_BEFORE or tok.startswith("'"):
parts[-1] = f"{prev}{tok}"
elif prev and prev[-1] in NO_SPACE_AFTER:
parts[-1] = f"{prev}{tok}"
else:
parts.append(tok)
return " ".join(parts)
def norm_text(value: str | None) -> str:
if not value:
return ""
return re.sub(r"\s+", " ", value).strip()
def first_text(root: ET.Element, paths: list[str]) -> str:
for path in paths:
node = root.find(path, NS)
if node is not None:
text = norm_text("".join(node.itertext()))
if text:
return text
return ""
def infer_year(root: ET.Element) -> str:
year = first_text(
root,
[
".//tei:date[@type='publication_year']",
".//tei:date[@type='first_published']",
],
)
if year:
m = re.search(r"\b(1[5-9]\d{2}|20\d{2})\b", year)
if m:
return m.group(1)
bibl_text = first_text(root, [".//tei:sourceDesc/tei:bibl"])
m = re.search(r"\b(1[5-9]\d{2}|20\d{2})\b", bibl_text)
return m.group(1) if m else ""
def infer_canton(root: ET.Element) -> tuple[str, str, str]:
canton_name = first_text(
root,
[
".//tei:noteGrp[@type='dialect_canton']/tei:note[@type='name']",
],
)
canton_code = first_text(
root,
[
".//tei:noteGrp[@type='dialect_canton']/tei:note[@type='chmk-id']",
],
)
region_name = first_text(
root,
[
".//tei:noteGrp[@type='dialect_region']/tei:note[@type='name']",
],
)
if not canton_code:
region_code = first_text(
root,
[
".//tei:noteGrp[@type='dialect_region']/tei:note[@type='chmk-id']",
],
)
if region_code and "-" in region_code:
maybe = region_code.split("-", 1)[0]
if maybe in SWISS_CANTONS:
canton_code = maybe
if not canton_name and canton_code in SWISS_CANTONS:
canton_name = SWISS_CANTONS[canton_code]
if not canton_name:
canton_name = "Unknown"
return canton_name, canton_code, region_name
def extract_metadata(root: ET.Element, xml_file: Path) -> dict[str, str]:
source = first_text(
root,
[
".//tei:title[@type='abbreviated']",
".//tei:title[@type='main']",
".//tei:titleStmt/tei:title",
],
)
if not source:
source = xml_file.stem
file_id = first_text(root, [".//tei:msDesc/tei:msIdentifier/tei:idno"]) or xml_file.stem.split("_", 1)[0]
year = infer_year(root)
canton_name, canton_code, region_name = infer_canton(root)
return {
"xml_file": str(xml_file),
"file_id": file_id,
"source": source,
"year": year,
"canton": canton_name,
"canton_code": canton_code,
"region": region_name,
}
def sentence_rows(xml_file: Path) -> list[dict[str, str]]:
tree = ET.parse(xml_file)
root = tree.getroot()
metadata = extract_metadata(root, xml_file)
rows: list[dict[str, str]] = []
s_nodes = root.findall(".//tei:text/tei:body//tei:s", NS)
if s_nodes:
for idx, s in enumerate(s_nodes, start=1):
tokens = [norm_text("".join(w.itertext())) for w in s.findall(".//tei:w", NS)]
tokens = [t for t in tokens if t]
text = detokenize(tokens)
if not text:
text = norm_text("".join(s.itertext()))
if not text:
continue
rows.append(
{
**metadata,
"sentence_id": str(idx),
"lang": s.attrib.get("{http://www.w3.org/XML/1998/namespace}lang", ""),
"sentence": text,
}
)
return rows
paragraphs = root.findall(".//tei:text/tei:body//tei:p", NS)
for idx, p in enumerate(paragraphs, start=1):
tokens = [norm_text("".join(w.itertext())) for w in p.findall(".//tei:w", NS)]
tokens = [t for t in tokens if t]
text = detokenize(tokens)
if not text:
text = norm_text("".join(p.itertext()))
if not text:
continue
rows.append(
{
**metadata,
"sentence_id": f"p{idx}",
"lang": "",
"sentence": text,
}
)
return rows
def should_merge(prev_sentence: str, current_sentence: str) -> bool:
prev = prev_sentence.strip()
cur = current_sentence.strip()
if not prev or not cur:
return False
if cur.startswith(STARTS_WITH_CLOSER):
return True
if prev.endswith((",", ";", ":")):
return True
if prev.endswith(ABBREV_ENDINGS):
return True
if re.match(r"^[0-9][0-9\s,.;:]*$", cur):
return True
if not prev.endswith(TERMINAL_END):
first = cur[0]
if first.islower() or first in ("'", "\"", "«", "„"):
return True
return False
def merge_sentence_rows(rows: list[dict[str, str]]) -> list[dict[str, str]]:
if not rows:
return rows
merged: list[dict[str, str]] = []
current = dict(rows[0])
start_id = current["sentence_id"]
end_id = current["sentence_id"]
for row in rows[1:]:
if should_merge(current["sentence"], row["sentence"]):
current["sentence"] = f"{current['sentence']} {row['sentence']}".strip()
end_id = row["sentence_id"]
continue
if end_id != start_id:
current["sentence_id"] = f"{start_id}-{end_id}"
merged.append(current)
current = dict(row)
start_id = current["sentence_id"]
end_id = current["sentence_id"]
if end_id != start_id:
current["sentence_id"] = f"{start_id}-{end_id}"
merged.append(current)
return merged
def year_sort_key(value: str) -> tuple[int, str]:
if value and value.isdigit():
return (0, f"{int(value):04d}")
return (1, value)
def sentence_id_sort_key(value: str) -> tuple[int, int, str]:
m = re.match(r"^(?:p)?(\d+)", value)
if not m:
return (1, 0, value)
prefix_penalty = 1 if value.startswith("p") else 0
return (0, prefix_penalty, int(m.group(1)))
def write_csv(path: Path, fieldnames: list[str], rows: list[dict[str, str]]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
def build_overview(documents: list[dict[str, str]]) -> list[dict[str, str]]:
return sorted(
documents,
key=lambda d: (
year_sort_key(d["year"]),
d["source"],
d["file_id"],
d["xml_file"],
),
)
def run(input_dir: Path, output_dir: Path) -> tuple[int, int]:
xml_files = sorted(input_dir.glob("*.xml"))
xml_files = [p for p in xml_files if not p.name.endswith("_index.xml")]
all_rows: list[dict[str, str]] = []
documents: list[dict[str, str]] = []
for xml_file in xml_files:
try:
rows = merge_sentence_rows(sentence_rows(xml_file))
all_rows.extend(rows)
if rows:
base = {k: rows[0][k] for k in ["xml_file", "file_id", "source", "year", "canton", "canton_code", "region"]}
else:
root = ET.parse(xml_file).getroot()
base = extract_metadata(root, xml_file)
documents.append({**base, "sentence_count": str(len(rows))})
except ET.ParseError as e:
print(f"[warn] Failed to parse {xml_file}: {e}")
all_rows = sorted(
all_rows,
key=lambda r: (
year_sort_key(r["year"]),
r["source"],
r["file_id"],
sentence_id_sort_key(r["sentence_id"]),
),
)
sentence_csv = output_dir / "chmk_sentences.csv"
overview_csv = output_dir / "chmk_overview.csv"
write_csv(
sentence_csv,
[
"file_id",
"source",
"year",
"canton",
"canton_code",
"region",
"sentence_id",
"lang",
"sentence",
"xml_file",
],
all_rows,
)
overview_rows = build_overview(documents)
write_csv(
overview_csv,
["xml_file", "file_id", "source", "year", "canton", "canton_code", "region", "sentence_count"],
overview_rows,
)
return len(overview_rows), len(all_rows)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Convert CHMK XML files to readable CSV files.")
parser.add_argument(
"--input-dir",
type=Path,
default=Path("data/unpacked/XML-CHMK_v2.2_free_subcorpus"),
help="Directory containing extracted CHMK XML files.",
)
parser.add_argument(
"--output-dir",
type=Path,
default=Path("data/processed"),
help="Directory where CSV files will be written.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
files_count, sentences_count = run(args.input_dir, args.output_dir)
print(f"Wrote {sentences_count} sentence rows across {files_count} sources.")
print(f"- {args.output_dir / 'chmk_sentences.csv'}")
print(f"- {args.output_dir / 'chmk_overview.csv'}")
if __name__ == "__main__":
main()