Download analysis/silver_overlap_parse.py from Urdatorn/Makron-code: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/Urdatorn/Makron-code/resolve/main/analysis/silver_overlap_parse.py
- Command line
-
hf download hf://Urdatorn/Makron-code/analysis/silver_overlap_parse.py
-
curl -L -o silver_overlap_parse.py https://huggingface.co/Urdatorn/Makron-code/resolve/main/analysis/silver_overlap_parse.py
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 | |