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

Skip to content

Add NgramCharModel to text.py #413

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 4 commits into from
Mar 25, 2017
Merged
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
18 changes: 16 additions & 2 deletions text.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def samples(self, n):
return ' '.join(self.sample() for i in range(n))



class NgramTextModel(CountingProbDist):

"""This is a discrete probability distribution over n-tuples of words.
Expand All @@ -50,12 +51,16 @@ def add(self, ngram):
self.cond_prob[ngram[:-1]] = CountingProbDist()
self.cond_prob[ngram[:-1]].add(ngram[-1])

def add_empty(self, words, n):
return [''] * (n - 1) + words

def add_sequence(self, words):
"""Add each of the tuple words[i:i+n], using a sliding window.
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 + 1):
words = self.add_empty(words, n)

for i in range(len(words) - n):
self.add(tuple(words[i:i + n]))

def samples(self, nwords):
Expand All @@ -72,6 +77,15 @@ def samples(self, nwords):
nminus1gram = nminus1gram[1:] + (wn,)
return ' '.join(output)


class NgramCharModel(NgramTextModel):
def add_empty(self, words, n):
return ' ' * (n - 1) + words

def add_sequence(self, words):
for word in words:
super().add_sequence(word)

# ______________________________________________________________________________


Expand Down