@@ -26,6 +26,7 @@ def samples(self, n):
26
26
return ' ' .join (self .sample () for i in range (n ))
27
27
28
28
29
+
29
30
class NgramTextModel (CountingProbDist ):
30
31
31
32
"""This is a discrete probability distribution over n-tuples of words.
@@ -50,12 +51,16 @@ def add(self, ngram):
50
51
self .cond_prob [ngram [:- 1 ]] = CountingProbDist ()
51
52
self .cond_prob [ngram [:- 1 ]].add (ngram [- 1 ])
52
53
54
+ def add_empty (self , words , n ):
55
+ return ['' ] * (n - 1 ) + words
56
+
53
57
def add_sequence (self , words ):
54
58
"""Add each of the tuple words[i:i+n], using a sliding window.
55
59
Prefix some copies of the empty word, '', to make the start work."""
56
60
n = self .n
57
- words = ['' , ] * (n - 1 ) + words
58
- for i in range (len (words ) - n + 1 ):
61
+ words = self .add_empty (words , n )
62
+
63
+ for i in range (len (words ) - n ):
59
64
self .add (tuple (words [i :i + n ]))
60
65
61
66
def samples (self , nwords ):
@@ -72,6 +77,15 @@ def samples(self, nwords):
72
77
nminus1gram = nminus1gram [1 :] + (wn ,)
73
78
return ' ' .join (output )
74
79
80
+
81
+ class NgramCharModel (NgramTextModel ):
82
+ def add_empty (self , words , n ):
83
+ return ' ' * (n - 1 ) + words
84
+
85
+ def add_sequence (self , words ):
86
+ for word in words :
87
+ super ().add_sequence (word )
88
+
75
89
# ______________________________________________________________________________
76
90
77
91
0 commit comments