1af1b62fc8eb3270c35c4bb39a773804faf8da47
[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 @lru_cache()
15 def segment_wrong(text):
16 """Return a list of words that is the best segmentation of text.
17 """
18 if not text: return []
19 candidates = ([first]+segment(rest) for first,rest in splits(text))
20 return max(candidates, key=language_models.Pwords_wrong)
21
22
23 def splits(text, L=20):
24 """Return a list of all possible (first, rest) pairs, len(first)<=L.
25 """
26 return [(text[:i+1], text[i+1:])
27 for i in range(min(len(text), L))]
28