Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion wordninja.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@

class LanguageModel(object):
def __init__(self, word_file):
self.word_file = word_file
# Build a cost dictionary, assuming Zipf's law and cost = -math.log(probability).
with gzip.open(word_file) as f:
words = f.read().decode().split()
self._wordcost = dict((k, log((i+1)*log(len(words)))) for i,k in enumerate(words))
self._maxword = max(len(x) for x in words)



def add_word(self, w):
with gzip.open(self.word_file, 'a') as f:
f.write(b'%b' % (w.encode('utf-8')))

num_words = len(self._wordcost) + 1
self._wordcost[w] = log((num_words)*log(num_words))
self._maxword = max(self._maxword, len(w))


def split(self, s):
"""Uses dynamic programming to infer the location of spaces in a string without spaces."""
Expand Down Expand Up @@ -83,4 +93,7 @@ def best_match(i):
def split(s):
return DEFAULT_LANGUAGE_MODEL.split(s)

def add_word(w):
return DEFAULT_LANGUAGE_MODEL.add_word(w)