X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=segment.py;fp=segment.py;h=0000000000000000000000000000000000000000;hb=92ae3192f1ac20bfefacbefa7c2d68f843553e80;hp=a64ea5d4eb4248edaaff12c791df088f8109e21c;hpb=20aff345391c6ae2d00a55c586fda31f6f4315d5;p=cipher-training.git diff --git a/segment.py b/segment.py deleted file mode 100644 index a64ea5d..0000000 --- a/segment.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Segment a collection of letters into words""" - -import language_models -import sys -from functools import lru_cache -sys.setrecursionlimit(1000000) - -@lru_cache() -def segment(text): - """Return a list of words that is the best segmentation of text. - """ - if not text: return [] - candidates = ([first]+segment(rest) for first, rest in splits(text)) - return max(candidates, key=language_models.Pwords) - -@lru_cache() -def segment_wrong(text): - """Return a list of words that is the best segmentation of text. - """ - if not text: return [] - candidates = ([first]+segment(rest) for first, rest in splits(text)) - return max(candidates, key=language_models.Pwords_wrong) - - -def splits(text, L=20): - """Return a list of all possible (first, rest) pairs, len(first)<=L. - """ - return [(text[:i+1], text[i+1:]) - for i in range(min(len(text), L))] -