def peter_norvig_approach(self, word): if word in self.vocab: return [word] p_max = 0 approx = [] candidates = known_generated(word, self.vocab) for c in candidates: p = self.vocab[c] if p > p_max: p_max = p approx = [c] elif p == p_max: approx.append(c) if approx: return approx else: return [word]
def context_approach_trigrams(self, utterance): w1, w2, w3 = word_tokenize(utterance) c_based_on_context = self.tri_grams[w1][w2] if c_based_on_context: vocab = {c for c in c_based_on_context} candidates = known_generated(w3, vocab) if candidates: p_max = 0 approx = [] for c in candidates: p = c_based_on_context[c] if p_max < p: p_max = p approx = [c] elif p_max == p: approx.append(c) return approx return ["xxxxx"]
def context_approach_bigrams(self, utterance): w1, w2 = word_tokenize(utterance) w1 = self.word2idx[w1] c_based_on_context = self.bi_grams[w1] if c_based_on_context: vocab = {self.idx2word[c] for c in c_based_on_context} candidates = known_generated(w2, vocab) if candidates: candidates = tuple([self.word2idx[c] for c in candidates]) p_max = 0 approx = [] for c in candidates: p = c_based_on_context[c] if p_max < p: p_max = p approx = [c] elif p_max == p: approx.append(c) return tuple([self.idx2word[i] for i in approx]) return ["xxxxx"]
def generate_and_scan(self, word): candidates = list(known_generated(word, self.vocab)) if candidates: return candidates return ["xxxx"]