Give every piece a probability, find the most probable split of a word by dynamic programming over a lattice, and train by pruning the pieces the text misses least.
This interactive visualization needs JavaScript.