Skip to content
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