def __init__(self, n, observation_sequence=[], default=0): # In addition to the dictionary of n-tuples, cond_prob is a # mapping from (w1, ..., wn-1) to P(wn | w1, ... wn-1) CountingProbDist.__init__(self, default=default) self.n = n self.cond_prob = defaultdict() self.add_sequence(observation_sequence)
def __init__(self, n, observation_sequence=[]): ## In addition to the dictionary of n-tuples, cond_prob is a ## mapping from (w1, ..., wn-1) to P(wn | w1, ... wn-1) CountingProbDist.__init__(self) self.n = n self.cond_prob = defaultdict(CountingProbDist()) self.add_sequence(observation_sequence)
def add(self, ngram): """Count 1 for P[(w1, ..., wn)] and for P(wn | (w1, ..., wn-1)""" CountingProbDist.add(self, ngram) self.cond_prob[ngram[:-1]].add(ngram[-1])
def __init__(self, observation_sequence=[], default=0): CountingProbDist.__init__(self, default=default) self.n = 1 self.cond_prob = defaultdict() self.add_sequence(observation_sequence)
def add_cond_prob(self, ngram): """Builds the conditional probabilities P(wn | (w1, ..., wn-1)""" if ngram[:-1] not in self.cond_prob: self.cond_prob[ngram[:-1]] = CountingProbDist() self.cond_prob[ngram[:-1]].add(ngram[-1])
def __init__(self, training_text): training_text = canonicalize(training_text) self.P2 = CountingProbDist(bigrams(training_text), default=1)
def add(self, ngram): """Count 1 for P[(w1, ..., wn)] and for P(wn | (w1, ..., wn-1)""" CountingProbDist.add(self, ngram) if ngram[:-1] not in self.cond_prob: self.cond_prob[ngram[:-1]] = CountingProbDist() self.cond_prob[ngram[:-1]].add(ngram[-1])