nfc.bend source
nfc.bend on the hub · documented module
# Unicode NFC, as UAX #15 defines it: full canonical decomposition, canonical ordering (a stable sort of each run of# combining marks by combining class), then canonical composition (each character joins the last starter unless a# mark in between blocks it). Hangul syllables are decomposed and composed by arithmetic; everything else comes from# the tables, passed in (tok.bend gives tables.bend's), which hold exactly what Hugging Face's NFC knows.import Baseimport ./tree.bend as Ttype NT is Data: NT{ccc: T.RT, dec: T.KT, comp: T.PT}def nt_ccc(nt: NT) -> T.RT: match nt: case NT{ccc, dec, comp}: cccdef nt_dec(nt: NT) -> T.KT: match nt: case NT{ccc, dec, comp}: decdef nt_comp(nt: NT) -> T.PT: match nt: case NT{ccc, dec, comp}: compdef ccc(+nt: NT, +c: U32) -> U32: T.rt_find(nt_ccc(nt), c)# ---- Hangul: syllables are L V (T) jamo sequences, by arithmetic ----# SBase 0xAC00, LBase 0x1100, VBase 0x1161, TBase 0x11A7; 19 L, 21 V, 28 T, 588 = 21 * 28, 11172 syllablesdef is_syllable(+c: U32) -> Bool: Bool.and(U32.is_ge(c, 44032), U32.is_lt(c, 55204))def hangul_t(+s: U32) -> List<&2, U32>: Bool.pick(List<&2, U32>, U32.is_eq(U32.mod(s, 28), 0), [], [(4519 + U32.mod(s, 28) : U32)])def hangul(+s: U32) -> List<&2, U32>: (4352 + (s / 588 : U32) : U32) <> ((4449 + (U32.mod(s, 588) / 28 : U32) : U32) <> hangul_t(s))# ---- decomposition ----def decompose1(+nt: NT, +c: U32) -> List<&2, U32>: Bool.pick(List<&2, U32>, is_syllable(c), hangul((c - 44032 : U32)), Maybe.default(&2, List<&2, U32>, T.kt_find(nt_dec(nt), c), [c]))def decompose(+nt: NT, cs: List<&2, U32>) -> List<&2, U32>: match cs: case []: [] case c <> t: List.append(&2, U32, decompose1(nt, c), decompose(nt, t))# ---- canonical ordering ----type CK is Data: CK{k: U32, c: U32}def ck_k(x: CK) -> U32: match x: case CK{k, c}: kdef ck_c(x: CK) -> U32: match x: case CK{k, c}: cdef ins_here(after: Bool, +x: CK, +h: CK, t: List<&2, CK>, rest: Unit -> List<&2, CK>) -> List<&2, CK>: match after: case True{}: h <> rest(Unit{}) case False{}: x <> (h <> t)# x into a run sorted by class, after every element of its class or lower (so equal classes keep their order)def ins(run: List<&2, CK>, +x: CK) -> List<&2, CK>: match run: case []: [x] case +h <> +t: ins_here(U32.is_le(ck_k(h), ck_k(x)), x, h, t, u => ins(t, x))# a run, onto out (last first)def flush(run: List<&2, CK>, out: List<&2, U32>) -> List<&2, U32>: match run: case []: out case x <> t: flush(t, ck_c(x) <> out)type RO is Data: RO{run: List<&2, CK>, out: List<&2, U32>}def ro_step(z: Bool, +c: U32, +k: U32, run: List<&2, CK>, out: List<&2, U32>) -> RO: match z: case True{}: RO{[], c <> flush(run, out)} case False{}: RO{ins(run, CK{k, c}), out}def ro_add(+nt: NT, st: RO, +c: U32) -> RO: match st: case RO{run, out}: ro_step(U32.is_eq(ccc(nt, c), 0), c, ccc(nt, c), run, out)def reorder_go(+nt: NT, cs: List<&2, U32>, st: RO) -> RO: match cs: case []: st case c <> t: reorder_go(nt, t, ro_add(nt, st, c))def ro_done(st: RO) -> List<&2, U32>: match st: case RO{run, out}: List.reverse(&2, U32, flush(run, out))def reorder(+nt: NT, cs: List<&2, U32>) -> List<&2, U32>: ro_done(reorder_go(nt, cs, RO{[], []}))# ---- canonical composition ----def in_lv(+a: U32, +b: U32) -> Bool: Bool.and(Bool.and(U32.is_ge(a, 4352), U32.is_lt(a, 4371)), Bool.and(U32.is_ge(b, 4449), U32.is_lt(b, 4470)))def in_lvt(+a: U32, +b: U32) -> Bool: Bool.and(Bool.and(is_syllable(a), U32.is_eq(U32.mod((a - 44032 : U32), 28), 0)), Bool.and(U32.is_gt(b, 4519), U32.is_lt(b, 4547)))# the primary composite of a b, if anydef pair(+nt: NT, +a: U32, +b: U32) -> Maybe<&2, U32>: Bool.pick(Maybe<&2, U32>, in_lv(a, b), Some{(44032 + ((((a - 4352 : U32) * 21 : U32) + (b - 4449 : U32) : U32) * 28 : U32) : U32)}, Bool.pick(Maybe<&2, U32>, in_lvt(a, b), Some{(a + (b - 4519 : U32) : U32)}, T.pt_find(nt_comp(nt), a, b)))# done: finished code points (last first); st: the last starter, if any; between: the marks after it (last first);# prev: the class of the last of them (0 for none)type CS is Data: CS{done: List<&2, U32>, st: Maybe<&2, U32>, between: List<&2, U32>, prev: U32}def mlist(m: Maybe<&2, U32>) -> List<&2, U32>: match m: case None{}: [] case Some{x}: [x]def is_nil(xs: List<&2, U32>) -> Bool: match xs: case []: True{} case x <> t: False{}# c starts a new run (a starter) or joins the marks after the last starterdef cs_new(z: Bool, +c: U32, +k: U32, done: List<&2, U32>, st: Maybe<&2, U32>, between: List<&2, U32>, prev: U32) -> CS: match z: case True{}: CS{List.append(&2, U32, between, List.append(&2, U32, mlist(st), done)), Some{c}, [], 0} case False{}: CS{done, st, c <> between, k}def cs_comp(m: Maybe<&2, U32>, +s: U32, +c: U32, +k: U32, done: List<&2, U32>, between: List<&2, U32>, prev: U32) -> CS: match m: case Some{p}: CS{done, Some{p}, between, prev} case None{}: cs_new(U32.is_eq(k, 0), c, k, done, Some{s}, between, prev)def cs_st(st: Maybe<&2, U32>, +nt: NT, +c: U32, +k: U32, done: List<&2, U32>, +between: List<&2, U32>, +prev: U32) -> CS: match st: case None{}: cs_new(U32.is_eq(k, 0), c, k, done, None{}, between, prev) case Some{+s}: cs_comp(Bool.pick(Maybe<&2, U32>, Bool.and(Bool.not(is_nil(between)), Bool.or(U32.is_eq(prev, 0), U32.is_ge(prev, k))), None{}, pair(nt, s, c)), s, c, k, done, between, prev)def cs_add(+nt: NT, s: CS, +c: U32) -> CS: match s: case CS{done, st, between, prev}: cs_st(st, nt, c, ccc(nt, c), done, between, prev)def compose_go(+nt: NT, cs: List<&2, U32>, s: CS) -> CS: match cs: case []: s case c <> t: compose_go(nt, t, cs_add(nt, s, c))def cs_done(s: CS) -> List<&2, U32>: match s: case CS{done, st, between, prev}: List.reverse(&2, U32, List.append(&2, U32, between, List.append(&2, U32, mlist(st), done)))def compose(+nt: NT, cs: List<&2, U32>) -> List<&2, U32>: cs_done(compose_go(nt, cs, CS{[], None{}, [], 0}))def nfc(+nt: NT, cs: List<&2, U32>) -> List<&2, U32>: compose(nt, reorder(nt, decompose(nt, cs)))