-def Pwords(words):
- "The Naive Bayes probability of a sequence of words."
- return product(Pw(w) for w in words)
-
-class Pdist(dict):
- "A probability distribution estimated from counts in datafile."
- def __init__(self, data=[], N=None, missingfn=None):
- for key,count in data:
- self[key] = self.get(key, 0) + int(count)
- self.N = float(N or sum(self.itervalues()))
- self.missingfn = missingfn or (lambda k, N: 1./N)
- def __call__(self, key):
- if key in self: return self[key]/self.N
- else: return self.missingfn(key, self.N)
-
-def datafile(name, sep='\t'):
- "Read key,value pairs from file."
- for line in file(name):
- yield line.split(sep)
-
-def avoid_long_words(key, N):
- "Estimate the probability of an unknown word."
- return 10./(N * 10**len(key))
-
-N = 1024908267229 ## Number of tokens
-
-Pw = Pdist(datafile('count_1w.txt'), N, avoid_long_words)