Makron-code / analysis /silver_overlap_parse.py
Urdatorn's picture
Norma accuracy by silver form and lemma coverage (reviewer: unseen forms/lemmas); hand lemmas for forms odyCy cannot lemmatise
1dfa183 verified
Raw History Blame Contribute Delete
1.1 kB
"""parse() from silver_overlap.py, importable."""
import json, sys, unicodedata, collections, glob, os
APOS = {"'": "’", "ʼ": "’", "᾽": "’", "‘": "’", "᾿": "’"}
MARK = "^_"
GRAVE2ACUTE = {"̀": "́"}
def nfc(s):
return unicodedata.normalize("NFC", s)
def parse(tok):
"""marked token -> (plain chars with edge punctuation stripped, {pos: '^'|'_'})"""
chars, labels = [], {}
for ch in nfc(tok):
if ch in MARK:
if chars:
labels[len(chars) - 1] = ch
continue
chars.append(APOS.get(ch, ch))
# strip leading/trailing non-letters (keep final elision apostrophe)
def ok(c):
return unicodedata.category(c)[0] in "LM"
s = 0
while s < len(chars) and not ok(chars[s]):
s += 1
e = len(chars)
while e > s and not ok(chars[e - 1]) and not (chars[e - 1] == "’" and e - 1 > s):
e -= 1
# drop trailing punctuation after an apostrophe (e.g. "δ’,")
chars = chars[s:e]
labels = {p - s: l for p, l in labels.items() if s <= p < e}
return "".join(chars), labels