def demo(n): print(markov(n, tokenize_text(get_example_text())))
def print_example_model(n): print(repr(NgramModel(n, tokenize_text(get_example_text())).nexttokens))
def test_markov(self): chain = ex2_markov.markov(1, ex2.tokenize_text(ex2.get_example_text())) self.assertEqual(54, len(chain.split(' '))) self.assertTrue(chain.startswith('E m m a '))
import random from ex2 import get_example_text, tokenize_text from ex3_markov import NgramModel from ex3_poem import is_known_word, words_rhyme MODEL = NgramModel(1, tokenize_text(get_example_text())) def random_walk_of_indexes(list_): walk = list(range(len(list_))) random.shuffle(walk) return walk def get_matching_word(words, indexes, predicate=lambda w: True): while indexes: i = indexes.pop() word = words[i] if is_known_word(word) and predicate(word): return i, word raise ValueError("No matching word found") def gen_two_rhyming_lines(): words = [w.lower() for w in MODEL.generate(1000) if w.isalpha()] walk = random_walk_of_indexes(words) i1, last1 = get_matching_word(words, walk) i2, last2 = get_matching_word(words, walk, lambda w: words_rhyme(last1, w)) n1 = random.randrange(3,5+1)
def test_tokenize_text(self): self.assertEqual( ex2.tokenize_text('Note: the "exercise group" of Mondays 12-14'), ['Note', ':', 'the', '"', 'exercise', 'group', '"', 'of', 'Mondays', '12', '-', '14'])