File size: 1,102 Bytes
1dfa183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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