-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.py
More file actions
416 lines (345 loc) · 14.2 KB
/
normalize.py
File metadata and controls
416 lines (345 loc) · 14.2 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
407
408
409
410
411
412
413
414
415
416
"""
Validation and normalization pipeline for KIParla transcription units.
Rules are registered in WARNING_RULES and ERROR_RULES. To add a new rule:
1. Write the check/fix function in this module (or import it).
2. Append a ValidationRule to the appropriate registry.
3. Add it to the default YAML config schema (Step 4).
Warning rule functions must have signature: (str) -> tuple[int, str]
returning (substitution_count, new_annotation).
Error rule functions must have signature: (str) -> bool
returning True if the annotation is valid, False if an error is detected.
"""
from dataclasses import dataclass
from typing import Callable
import regex as re
import num2words as _num2words
@dataclass
class ValidationRule:
name: str
function: Callable
enabled_by_default: bool = True
# Warning rules: applied in order to the annotation string; order is load-bearing.
WARNING_RULES: list[ValidationRule] = []
# Error rules: applied after all warnings; each checks the final annotation.
ERROR_RULES: list[ValidationRule] = []
def _is_enabled(rule: ValidationRule, config: dict[str, bool]) -> bool:
return config.get(rule.name, rule.enabled_by_default)
def validate_and_normalize(
annotation: str,
config: dict[str, bool] | None = None,
) -> tuple[str, dict[str, int], dict[str, bool]]:
"""
Apply all enabled rules to *annotation*.
Args:
annotation: raw transcription unit text.
config: optional mapping of rule name -> enabled. Missing keys
fall back to each rule's ``enabled_by_default``.
Returns:
normalized -- the (possibly modified) annotation string
warnings -- {rule_name: total_substitution_count} for rules that fired
errors -- {rule_name: True} for error rules that detected a problem
"""
if config is None:
config = {}
warnings: dict[str, int] = {}
errors: dict[str, bool] = {}
normalized = annotation
for rule in WARNING_RULES:
if not _is_enabled(rule, config):
continue
count, normalized = rule.function(normalized)
if count > 0:
warnings[rule.name] = warnings.get(rule.name, 0) + count
for rule in ERROR_RULES:
if not _is_enabled(rule, config):
continue
if not rule.function(normalized):
errors[rule.name] = True
return normalized, warnings, errors
# ---------------------------------------------------------------------------
# Warning functions (str) -> tuple[int, str]
# ---------------------------------------------------------------------------
def remove_spaces(annotation: str) -> tuple[int, str]:
"""Collapse tabs, newlines, and repeated spaces into a single space."""
total = 0
for pattern, replacement in [(r"\t+", " "), (r"\n+", " "), (r"\s\s+", " ")]:
annotation, n = re.subn(pattern, replacement, annotation)
total += n
return total, annotation.strip()
def _replace_spaces_in_braces(match: re.Match) -> str:
return "{" + match.group(1).replace(" ", "_") + "}"
def meta_tag(annotation: str) -> tuple[int, str]:
"""Convert Jefferson double-parenthesis notation: (( → {, )) → }, (.) → {P}."""
subs_map = {"((": "{", "))": "}", "(.)": "{P}"}
total = 0
for old, new in subs_map.items():
annotation, n = re.subn(re.escape(old), new, annotation)
total += n
annotation = re.sub(r"\{([\w ]+)\}", _replace_spaces_in_braces, annotation)
return total, annotation
def check_spaces(annotation: str) -> tuple[int, str]:
"""Fix spacing errors around brackets and punctuation."""
total = 0
rules = [
# Space is on the wrong side of an opening bracket: move it outside.
# e.g. "bla[ ciao]" → "bla [ciao]"
(r"([^ \[\(<>°])([\[\(]) ([^ ])", r"\1 \2\3"),
# Space after opening bracket when nothing precedes it (or preceded by space).
# e.g. "[ ciao]" → "[ciao]"
(r"([\[\(]) ([^ ])", r"\1\2"),
# Space is on the wrong side of a closing bracket: move it outside.
# e.g. "[ciao ]bla" → "[ciao] bla"
(r"([^ ]) ([\)\]])([^ ])", r"\1\2 \3"),
# Space before closing bracket when nothing follows it (or followed by space).
# e.g. "[ciao ]" → "[ciao]"
(r"([^ ]) ([\)\]])", r"\1\2"),
# Space before punctuation with a word following: move space to after.
# e.g. "ciao ,bla" → "ciao, bla"
(r"([^ ]) ([.,:?])([^ ])", r"\1\2 \3"),
# Space before punctuation at end (or before another space).
# e.g. "ciao ," → "ciao,"
(r"([^ ]) ([.,:?])", r"\1\2"),
# Missing space before NVB tag.
(r"([^ \[\(<>°])(\{[^}]+\})", r"\1 \2"),
# Missing space after NVB tag.
(r"(\{[^}]+\})([^ \]\)<>°])", r"\1 \2"),
]
for pattern, replacement in rules:
annotation, n = re.subn(pattern, replacement, annotation)
total += n
return total, annotation.strip()
def remove_pauses(annotation: str) -> tuple[int, str]:
"""Strip leading and trailing short-pause markers {P}."""
annotation, n = re.subn(
r"^([\[\]()<>°]?)\s*\{P\}\s*|\s*\{P\}\s*([\[\]()<>°]?)$",
r"\1\2",
annotation,
)
return n, annotation.strip()
def remove_prosodiclinks(annotation: str) -> tuple[int, str]:
"""Strip leading and trailing prosodic-link markers =."""
annotation, n = re.subn(
r"^([\[\]()<>°]?)\s*=\s*|\s*=\s*([\[\]()<>°]?)$",
r"\1\2",
annotation,
)
return n, annotation.strip()
def space_prosodiclink(annotation: str) -> tuple[int, str]:
"""Remove stray spaces immediately before or after = markers."""
annotation, n = re.subn(r" =|= ", "=", annotation)
return n, annotation.strip()
def overlap_prolongations(annotation: str) -> tuple[int, str]:
"""Fix malformed overlap+prolongation sequences: word:[: → [word::."""
annotation, n = re.subn(r"(\w:*)\[:", r"[\1:", annotation)
return n, annotation
def clean_non_jefferson_symbols(annotation: str) -> tuple[int, str]:
"""Remove characters that are not part of the Jefferson transcription system."""
annotation, n = re.subn(
r"[^{}_,\?.:=°><\[\]\(\)\w\s'\-~$#@]",
"",
annotation,
)
return n, annotation.strip()
# Accent substitution tables — defined as module-level constants so they can
# be replaced or extended by a YAML config in a later step.
# Words where -chè should become -ché (the regex allows Jefferson markers
# interspersed between letters, e.g. per[chè → per[ché).
ACCENT_CHE_MAP: dict[str, str] = {
"perchè": "perché",
"benchè": "benché",
"finchè": "finché",
"poichè": "poiché",
"anzichè": "anziché",
"dopodichè":"dopodiché",
"granchè": "granché",
"fuorchè": "fuorché",
"affinchè": "affinché",
"pressochè":"pressoché",
"nè": "né",
}
# Words where a trailing apostrophe-accent should become the proper accent.
ACCENT_PERO_MAP: dict[str, str] = {
"pero'": "però",
"perche'": "perché",
"puo'": "può",
}
def replace_che(annotation: str, accent_map: dict[str, str] | None = None) -> tuple[int, str]:
"""Replace common Italian accent errors of the -chè/-né family."""
if accent_map is None:
accent_map = ACCENT_CHE_MAP
total = 0
for word in accent_map:
# Build a pattern that tolerates Jefferson markers between letters.
pattern = r"\b" + "".join(f"([^ =']*){ch}" for ch in word) + r"\b"
back_refs = "".join(f"\\{i+1}{ch}" for i, ch in enumerate(word))
replacement = back_refs[:-1] + "é" # swap last char with é
annotation, n = re.subn(re.compile(pattern), replacement, annotation)
total += n
return total, annotation
def replace_po(annotation: str) -> tuple[int, str]:
"""Replace pò (and prolonged variants like p:ò) with po'."""
annotation, n = re.subn(r"\bp([^ =\p{L}]*)ò\b", r"p\1o'", annotation)
return n, annotation.strip()
def replace_pero(annotation: str, accent_map: dict[str, str] | None = None) -> tuple[int, str]:
"""Replace apostrophe-accent words (pero', puo', perche') with accented forms."""
if accent_map is None:
accent_map = ACCENT_PERO_MAP
total = 0
for word, substitute in accent_map.items():
pattern = r"\b" + word[0]
back_refs = word[0]
for i, ch in enumerate(word[1:-1]):
pattern += f"([^ =]*){ch}"
back_refs += f"\\{i+1}{ch}"
pattern += f"([^ =]*){word[-1]}"
back_refs = back_refs[:-1] + substitute[-1]
annotation, n = re.subn(re.compile(pattern), back_refs, annotation)
total += n
return total, annotation
def check_numbers(annotation: str) -> tuple[int, str]:
"""Convert digit sequences to Italian words (2 → due)."""
matches = list(re.finditer(r"\b[0-9]+\b", annotation))
if not matches:
return 0, annotation
parts = []
prev = 0
for m in matches:
start, end = m.span()
parts.append(annotation[prev:start])
word = _num2words.num2words(m.group(0), lang="it")
if word.endswith("tre") and len(word) > 3:
word = word[:-1] + "é"
parts.append(word)
prev = end
parts.append(annotation[prev:])
return len(matches), "".join(parts)
def switch_symbols(annotation: str) -> tuple[int, str]:
"""Move intonation markers before prosodic/interruption symbols: .,? must follow :, -, ~."""
annotation, n = re.subn(r"([.,?])([:-~])", r"\2\1", annotation)
return n, annotation.strip()
def switch_NVB(annotation: str) -> tuple[int, str]:
"""Move NVB tags outside bracket spans of any kind.
NVB tags found immediately after an opening bracket, or immediately before
a closing bracket, are relocated to just outside the span.
Exception: {P} immediately after [ or immediately before ] is left in
place — a pause inside an overlap span is transcriptionally valid.
"""
total = 0
# Opening [ : move any NVB except {P} to before the bracket.
annotation, n = re.subn(r"(\[)(\{(?!P\})\w+\})", r"\2 \1", annotation)
total += n
# Opening ( < > ° : move any NVB (including {P}) to before the bracket.
annotation, n = re.subn(r"([(<>°])(\{\w+\})", r"\2 \1", annotation)
total += n
# Closing ] : move any NVB except {P} to after the bracket.
annotation, n = re.subn(r"(\{(?!P\})\w+\})(])", r"\2 \1", annotation)
total += n
# Closing ) > < ° : move any NVB (including {P}) to after the bracket.
annotation, n = re.subn(r"(\{\w+\})([)><°])", r"\2 \1", annotation)
total += n
return total, annotation.strip()
def check_spaces_dots(annotation: str) -> tuple[int, str]:
"""Remove stray spaces immediately inside °...° low-volume markers."""
segments = re.split(r"(°[^°]+°)", annotation)
subs = 0
for i, seg in enumerate(segments):
if not seg.startswith("°"):
continue
if seg[1] == " ":
seg = seg[0] + seg[2:]
subs += 1
if seg[-2] == " ":
seg = seg[:-2] + seg[-1]
subs += 1
segments[i] = seg
return subs, "".join(segments).strip()
def check_spaces_angular(annotation: str) -> tuple[int, str]:
"""Remove stray spaces immediately inside <...> and >...< pace markers."""
segments = _split_angular(annotation)
subs = 0
for i, seg in enumerate(segments):
if not seg or seg[0] not in ("<", ">"):
continue
if len(seg) > 1 and seg[1] == " ":
seg = seg[0] + seg[2:]
subs += 1
if len(seg) > 1 and seg[-2] == " ":
seg = seg[:-2] + seg[-1]
subs += 1
segments[i] = seg
return subs, "".join(segments).strip()
def _split_angular(annotation: str) -> list[str]:
"""Split annotation into segments delimited by angular-bracket pace markers."""
fast = False # >...<
slow = False # <...>
cur: list[str] = []
segs: list[list[str]] = []
for ch in annotation:
if ch == "<":
if fast:
cur.append(ch)
segs.append(cur)
cur = []
fast = False
elif not slow:
segs.append(cur)
cur = [ch]
slow = True
else:
cur.append(ch)
elif ch == ">":
if slow:
cur.append(ch)
segs.append(cur)
cur = []
slow = False
elif not fast:
segs.append(cur)
cur = [ch]
fast = True
else:
cur.append(ch)
else:
cur.append(ch)
if cur:
segs.append(cur)
return ["".join(s) for s in segs if s]
# ---------------------------------------------------------------------------
# Error functions (str) -> bool (True = valid, False = error detected)
# ---------------------------------------------------------------------------
def check_even_dots(annotation: str) -> bool:
"""Return True if the number of ° characters is even (i.e. all pairs are closed)."""
return annotation.count("°") % 2 == 0
def check_normal_parentheses(annotation: str, open_char: str, close_char: str) -> bool:
"""Return True if open_char/close_char pairs are balanced and non-nested."""
is_open = False
for ch in annotation:
if ch == open_char:
if is_open:
return False
is_open = True
elif ch == close_char:
if not is_open:
return False
is_open = False
return not is_open
def check_angular_parentheses(annotation: str) -> bool:
"""Return True if <...> (slow) and >...< (fast) pace markers are balanced."""
fast = False
slow = False
for ch in annotation:
if ch == "<":
if fast:
fast = False
elif not slow:
slow = True
else:
return False # nested slow
elif ch == ">":
if slow:
slow = False
elif not fast:
fast = True
else:
return False # nested fast
return not fast and not slow