Skip to content
Split run-together words

Split run-together words with a dictionary

Read a string with no spaces over the words of a dictionary, every way it tiles into them, and let a grammar choose which tilings to keep (segmentation). Without a dictionary, segment by the text’s own statistics.

$ cat words.txt
the
cat
sat
thecat
on
mat

Count the readings

thecatsat tiles two ways, the cat sat and thecat sat; a grammar asking for three words keeps one:

$ trex grammar --grammar-text 's := <s> ident | ident' --segment thecatsat --dict words.txt --count
2

$ trex grammar --grammar-text 's := ident ident ident' --segment thecatsat --dict words.txt --count
1

List the readings

With @p weights on the rules, each tiling the grammar accepts has a probability, its most probable parse’s, and --list lists the tilings most probable first. A grammar that weighs each word at a half prefers the reading with fewer words:

$ trex grammar --grammar-text 's := <s> ident @0.5 | ident @0.5' --segment thecatsat --dict words.txt --list
thecat sat	0.25	1
the cat sat	0.125	1

$ trex grammar --grammar-text 's := <s> ident @0.5 | ident @0.5' --segment thecatsat --dict words.txt --best
0.25

--best gives the first tiling’s probability alone. Each line of --list is a tiling’s words, its probability and its parse count, separated by tabs.