3 from functools
import lru_cache
4 sys
.setrecursionlimit(1000000)
8 """Return a list of words that is the best segmentation of text.
10 if not text
: return []
11 candidates
= ([first
]+segment(rest
) for first
,rest
in splits(text
))
12 return max(candidates
, key
=language_models
.Pwords
)
14 def splits(text
, L
=20):
15 """Return a list of all possible (first, rest) pairs, len(first)<=L.
17 return [(text
[:i
+1], text
[i
+1:])
18 for i
in range(min(len(text
), L
))]