Language models read text as symbols, and a tokenizer invents them: it starts with single letters and keeps gluing two neighbours into a new symbol, so common chunks of text take fewer symbols. Write merge_once(tokens): add one new symbol, two neighbouring symbols glued together, chosen to make the text as short as possible, and glue it everywhere it fits, left to right. If two pairs would do equally well, take the one that shows up first in the text.
“hug_pug_hugs” is 12 symbols. Glue one kind of neighbour pair into a new symbol, everywhere it fits, and the text gets shorter. Watch two pairs tried, then try your own: which pair leaves the fewest symbols?
merge_once(tokens)| tokens | a list of symbols, like ["h", "u", "g", "_", "p", "u", "g"] glue ONE kind of neighbour pair into a new symbol, everywhere it fits | e.g. ["a"] |
| returns | the new list of symbols | |