More on keyword breaking
[cipher-training.git] / segment.py
1 import language_models
2 import sys
3 from functools import lru_cache
4 sys.setrecursionlimit(1000000)
5
6 @lru_cache()
7 def segment(text):
8 """Return a list of words that is the best segmentation of text.
9 """
10 if not text: return []
11 candidates = ([first]+segment(rest) for first,rest in splits(text))
12 return max(candidates, key=language_models.Pwords)
13
14 def splits(text, L=20):
15 """Return a list of all possible (first, rest) pairs, len(first)<=L.
16 """
17 return [(text[:i+1], text[i+1:])
18 for i in range(min(len(text), L))]
19