Spaces:
Running
Running
File size: 2,743 Bytes
c8085d2 | 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 | """Browser glue for tigmorph (runs in Pyodide). Every function returns JSON for the page."""
import json
import re
import unicodedata
from collections import Counter
from functools import lru_cache
import tigmorph
from tigmorph import Analyzer, Normalizer
from tigmorph.fidel import decompose
A = Analyzer()
_NORM = {}
NOMINAL_TAMS = {'PartAct', 'PartPass', 'Agent', 'Inf', 'Instr'}
PUNCT = re.compile(r'^[\W_።፣፤፥፦፧፨፡]+|[\W_።፣፤፥፦፧፨፡]+$')
VERSION = tigmorph.__version__
def _class(a):
if 'POS' in a.tags:
return a.tags['POS']
return 'V>N' if a.tags.get('TAM') in NOMINAL_TAMS else 'V'
def _features(a):
tags = dict(a.tags)
tags.pop('POS', None)
if tags.get('TAM') in NOMINAL_TAMS and 'Subj' in tags:
tags['Num'] = tags.pop('Subj')[1:]
if a.guess:
tags['Guess'] = 'Yes'
return [[k, str(v)] for k, v in tags.items()]
def _reading(a):
return {'root': a.root, 'cls': _class(a), 'feats': _features(a), 'raz': a.raz, 'gloss': a.gloss,
'score': round(a.weight, 2), 'guess': a.guess}
def analyze(text, n, guess, latin, compound):
text = unicodedata.normalize('NFC', text or '').strip()
if not text:
return json.dumps([])
g = True if guess else 'auto'
if compound:
return json.dumps([{'word': text, 'readings': [_reading(a) for a in A.analyze(text, guess=g)[:n]]}])
out = []
for t in re.split(r'[\s፡]+', text)[:200]:
t = PUNCT.sub('', t)
if not t:
continue
res = A.analyze_latin(t, guess=g) if latin else A.analyze(t, guess=g)
out.append({'word': t, 'readings': [_reading(a) for a in res[:n]]})
return json.dumps(out)
def root_to_latin(root):
root = unicodedata.normalize('NFC', root.strip())
if any('ሀ' <= c <= '' or 'ⶀ' <= c <= '' for c in root):
return '-'.join(decompose(c)[0] for c in root if c not in '- ')
if '-' in root:
return root.replace(' ', '')
return '-'.join(re.findall(r'.[̀-ͯ]*', root.replace(' ', '')))
@lru_cache(maxsize=20000)
def _gen(root, key):
try:
return tuple(A.generate(root, dict(key)))
except KeyError:
return ()
def generate(root, tags_json):
r = root_to_latin(root)
tags = {k: v for k, v in json.loads(tags_json).items() if v}
forms = _gen(r, tuple(sorted(tags.items())))
return json.dumps({'root': r, 'forms': [[raz, gz] for raz, gz in forms]})
def normalize(text, level):
if level not in _NORM:
_NORM[level] = Normalizer(level=level, analyzer=A)
log = Counter()
out = _NORM[level].normalize(text or '', log=log)
return json.dumps({'text': out, 'changes': [[a, b, c] for (a, b), c in log.most_common()]})
|