import re

import hfst as hfst

def one_sym_fst(insym, outsym):
  fst = hfst.HfstBasicTransducer()
  fst.add_state(1)
  fst.set_final_weight(1, 0.0)
  tr = hfst.HfstBasicTransition(1, insym, outsym, 0.0)
  fst.add_transition(0, tr)
  return  hfst.HfstTransducer(fst)

def f(p, tl):
    for t in tl:
        m = re.match(p, t)
        if m:
            a = m.group(0)
            u = m.group(1)
            if not u:
                u = "ANY"
            l = m.group(2)
            if not l:
                l = "ANY"
            elif len(l) == 2 and l[0] == "%":
                l = l[1]

            q = u + ":" + l
            print((f"input str: '{t}', input sym: '{u}',"
                   f" output sym: '{l}', pair: '{q}'"))
            fst = one_sym_fst(u, l)
            print(fst)
        else:
            print(t, "failed")

p = (
    r"(\{(?:[^ {}]+)\}|)"
    ":"
    r"([^\W0-9_]|[']|%[\W_]|)(?![\w%0-9])"
)

tl = [":", "{aä}:a", "{aä}:a,","{aä}:a ",  "{aä}:", ":a",
      "{_}:Ø", "{_}:%_", "{_}:%-", "{_}:%%"]

f(p, tl)

