X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=segment.py;h=dd0b2a8347ee800c4addf996f369ea0293b47bb7;hb=3906e8a686e3d1943e22746b65c394a4def34fc0;hp=27bf103c573f0d228a03741ef34dd360138ad072;hpb=6a89ca3a0906f12f0d3457642a235a4f9bc74ebf;p=cipher-tools.git diff --git a/segment.py b/segment.py index 27bf103..dd0b2a8 100644 --- a/segment.py +++ b/segment.py @@ -1,20 +1,12 @@ -# import re, string, random, glob, operator, heapq import string import collections from math import log10 import itertools +import sys +from functools import lru_cache +sys.setrecursionlimit(1000000) -def memo(f): - "Memoize function f." - table = {} - def fmemo(*args): - if args not in table: - table[args] = f(*args) - return table[args] - fmemo.memo = table - return fmemo - -@memo +@lru_cache() def segment(text): """Return a list of words that is the best segmentation of text. """ @@ -31,7 +23,7 @@ def splits(text, L=20): def Pwords(words): """The Naive Bayes log probability of a sequence of words. """ - return sum(Pw[w] for w in words) + return sum(Pw[w.lower()] for w in words) class Pdist(dict): """A probability distribution estimated from counts in datafile.