def test_shift(): assert fn_exists("shift"), """\ Utility function: shift As a quick aside, we need to build a function called 'shift' with certain useful behavior. I promise that you will use this later, but it is non-obvious right now how. Still, this is a good time to build it. The signature of shift looks like this: shift(tuple, anything) => tuple Shift takes a tuple, removes the first element, then adds the second argument to the end, and returns the new tuple. t = (1,2) shift(t, 3) => (2, 3) 'Shifting' values onto the end of a collection while removing them from the front is a commonly used operation. Try experimenting with adding tuples together in the console to get an idea of how to write this function.""" assert markov.shift((1, 2), 3) == (2, 3), """\ Here are more test cases to try out your shift function.""" assert markov.shift(("cat", "in"), "the") == ("in", "the"), """\
def test_shift(): assert fn_exists("shift"), """\ Utility function: shift As a quick aside, we need to build a function called 'shift' with certain useful behavior. I promise that you will use this later, but it is non-obvious right now how. Still, this is a good time to build it. The signature of shift looks like this: shift(tuple, anything) => tuple Shift takes a tuple, removes the first element, then adds the second argument to the end, and returns the new tuple. t = (1,2) shift(t, 3) => (2, 3) 'Shifting' values onto the end of a collection while removing them from the front is a commonly used operation. Try experimenting with adding tuples together in the console to get an idea of how to write this function.""" assert markov.shift((1,2), 3) == (2,3), """\ Here are more test cases to try out your shift function.""" assert markov.shift(("cat", "in"), "the") == ("in","the"), """\
def process_word(self, word, order=2): """Processes each word. word: string order: integer During the first few iterations, all we do is store up the words; after that we start adding entries to the dictionary. """ if len(self.prefix) < order: self.prefix += (word, ) return try: self.suffix_map[self.prefix].append(word) except KeyError: # if there is no entry for this prefix, make one self.suffix_map[self.prefix] = [word] self.prefix = shift(self.prefix, word)
def process_word(self, word, order=2): """Processes each word. word: string order: integer During the first few iterations, all we do is store up the words; after that we start adding entries to the dictionary. """ if len(self.prefix) < order: self.prefix += (word,) return try: self.suffix_map[self.prefix].append(word) except KeyError: # if there is no entry for this prefix, make one self.suffix_map[self.prefix] = [word] self.prefix = shift(self.prefix, word)
def random_text(self, n=100): """Generates random wordsfrom the analyzed text. Starts with a random prefix from the dictionary. n: number of words to generate """ # choose a random prefix (not weighted by frequency) start = random.choice(list(self.suffix_map.keys())) for i in range(n): suffixes = self.suffix_map.get(start, None) if suffixes == None: # if the prefix isn't in map, we got to the end of the # original text, so we have to start again. self.random_text(n - i) return # choose a random suffix word = random.choice(suffixes) print(word, end=' ') start = shift(start, word)
def random_text(self, n=100): """Generates random wordsfrom the analyzed text. Starts with a random prefix from the dictionary. n: number of words to generate """ # choose a random prefix (not weighted by frequency) start = random.choice(list(self.suffix_map.keys())) for i in range(n): suffixes = self.suffix_map.get(start, None) if suffixes == None: # if the prefix isn't in map, we got to the end of the # original text, so we have to start again. self.random_text(n-i) return # choose a random suffix word = random.choice(suffixes) print(word, end=' ') start = shift(start, word)