X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=segment.py;h=712895b6b0d7f1563ee4149fe7d94445a3931233;hb=e6332a16567643e66c2a491b94994f9482384d34;hp=e4b019f4c8248d8647f938fea8295c132308de0b;hpb=792bef4fa890a8c834ddd83ab9a573d0e2a75dc9;p=cipher-tools.git diff --git a/segment.py b/segment.py index e4b019f..712895b 100644 --- a/segment.py +++ b/segment.py @@ -3,18 +3,11 @@ 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. """ @@ -58,7 +51,5 @@ def avoid_long_words(key, N): """ return -log10((N * 10**(len(key) - 2))) -# N = 1024908267229 ## Number of tokens - Pw = Pdist(datafile('count_1w.txt'), avoid_long_words)