FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add NgramCharModel to text.py by lucasmoura · Pull Request #413 · aimacode/aima-python · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 16 additions & 2 deletions text.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL