specials.bend source
specials.bend on the hub · documented module
# Added tokens (the file's special and other added tokens) found in the raw text, before normalization, as Hugging# Face's `tokenizers` finds them: at each position the longest added token that starts there, else the next code point# joins the plain text. A match's text is cut from the input itself (take and drop), so (LAWS.bend) the segments always# concatenate back to the text.import Basetype AT is Data: AT{cs: List<&2, U32>, id: U32}type Seg is Data: SText{cs: List<&2, U32>} SAdded{id: U32, cs: List<&2, U32>}def seg_cs(s: Seg) -> List<&2, U32>: match s: case SText{cs}: cs case SAdded{id, cs}: csdef is_prefix(p: List<&2, U32>, xs: List<&2, U32>) -> Bool: match p xs: case Nil{} _: True{} case a <> pt Nil{}: False{} case +a <> pt +b <> xt: Bool.and(U32.is_eq(a, b), is_prefix(pt, xt))# the longest added token starting the text: its length and id (length 0: none)type BA is Data: BA{len: Nat, id: U32}def ba_pick(hit: Bool, +n: Nat, +id: U32, b: BA) -> BA: match b: case BA{+len, bid}: Bool.pick(BA, Bool.and(hit, Nat.is_gt(n, len)), BA{n, id}, BA{len, bid})def at_try(a: AT, +xs: List<&2, U32>, b: BA) -> BA: match a: case AT{+cs, +id}: ba_pick(is_prefix(cs, xs), List.length(&2, U32, cs), id, b)def longest(added: List<&2, AT>, +xs: List<&2, U32>, b: BA) -> BA: match added: case []: b case a <> t: longest(t, xs, at_try(a, xs, b))def ba_len(b: BA) -> Nat: match b: case BA{len, id}: lendef ba_id(b: BA) -> U32: match b: case BA{len, id}: id# the plain text so far (last first), as a segment listdef flush(buf: List<&2, U32>) -> List<&2, Seg>: match buf: case []: [] case c <> t: [SText{List.reverse(&2, U32, c <> t)}]def cut2(none: Bool, +n: Nat, id: U32, +buf: List<&2, U32>, +x: U32, +t: List<&2, U32>, go: List<&2, U32> -> List<&2, U32> -> List<&2, Seg>) -> List<&2, Seg>: match none: case True{}: go(x <> buf, t) case False{}: List.append(&2, Seg, flush(buf), SAdded{id, List.take(&2, U32, x <> t, n)} <> go([], List.drop(&2, U32, x <> t, n)))def cut(b: BA, +buf: List<&2, U32>, +x: U32, +t: List<&2, U32>, go: List<&2, U32> -> List<&2, U32> -> List<&2, Seg>) -> List<&2, Seg>: match b: case BA{+n, id}: cut2(Nat.is_eq(n, 0n), n, id, buf, x, t, go)def has(xs: List<&2, U32>, +c: U32) -> Bool: match xs: case []: False{} case +y <> t: Bool.or(U32.is_eq(y, c), has(t, c))# the first code points of the added tokens, each oncedef first_add2(cs: List<&2, U32>, +acc: List<&2, U32>) -> List<&2, U32>: match cs: case []: acc case +c <> t: Bool.pick(List<&2, U32>, has(acc, c), acc, c <> acc)def first_add(a: AT, +acc: List<&2, U32>) -> List<&2, U32>: match a: case AT{cs, id}: first_add2(cs, acc)def firsts(added: List<&2, AT>, acc: List<&2, U32>) -> List<&2, U32>: match added: case []: acc case a <> t: firsts(t, first_add(a, acc))# the longest added token at x, looked for only when x starts onedef at_pos(here: Bool, +added: List<&2, AT>, +xs: List<&2, U32>) -> BA: match here: case True{}: longest(added, xs, BA{0n, 0}) case False{}: BA{0n, 0}# fuel bounds the steps; once it runs out the rest is plain text (never, from `split_added`)def split_go(fuel: Nat, +fs: List<&2, U32>, +added: List<&2, AT>, buf: List<&2, U32>, xs: List<&2, U32>) -> List<&2, Seg>: match fuel xs: case _ Nil{}: flush(buf) case 0n x <> t: flush(List.append(&2, U32, List.reverse(&2, U32, x <> t), buf)) case 1n+f +x <> +t: cut(at_pos(has(fs, x), added, x <> t), buf, x, t, b => r => split_go(f, fs, added, b, r))# the text as plain segments and added tokensdef split_added(+added: List<&2, AT>, +xs: List<&2, U32>) -> List<&2, Seg>: split_go(List.length(&2, U32, xs), firsts(added, []), added, [], xs)# the text the segments cover, in orderdef text_of(ss: List<&2, Seg>) -> List<&2, U32>: match ss: case []: [] case s <> t: List.append(&2, U32, seg_cs(s), text_of(t))