Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix NgramTextModel bug #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ def test_text_models():

assert P3.cond_prob['in', 'order'].dictionary == {'to': 6}

test_string = 'unigram'
wordseq = words(test_string)

P1 = UnigramTextModel(wordseq)

assert P1.dictionary == {('unigram'): 1}

test_string = 'bigram text'
wordseq = words(test_string)

P2 = NgramTextModel(2, wordseq)

assert (P2.dictionary == {('', 'bigram'): 1, ('bigram', 'text'): 1} or
P2.dictionary == {('bigram', 'text'): 1, ('', 'bigram'): 1})


test_string = 'test trigram text'
wordseq = words(test_string)

P3 = NgramTextModel(3, wordseq)

assert ('', '', 'test') in P3.dictionary
assert ('', 'test', 'trigram') in P3.dictionary
assert ('test', 'trigram', 'text') in P3.dictionary
assert len(P3.dictionary) == 3


def test_viterbi_segmentation():
flatland = DataFile("EN-text/flatland.txt").read()
Expand Down
2 changes: 1 addition & 1 deletion text.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def add_sequence(self, words):
Prefix some copies of the empty word, '', to make the start work."""
n = self.n
words = ['', ] * (n - 1) + words
for i in range(len(words) - n):
for i in range(len(words) - n + 1):
self.add(tuple(words[i:i + n]))

def samples(self, nwords):
Expand Down