|
| 1 | +"""This module contains a code example related to |
| 2 | +
|
| 3 | +Think Python, 2nd Edition |
| 4 | +by Allen Downey |
| 5 | +http://thinkpython2.com |
| 6 | +
|
| 7 | +Copyright 2015 Allen Downey |
| 8 | +
|
| 9 | +License: http://creativecommons.org/licenses/by/4.0/ |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import print_function, division |
| 13 | + |
| 14 | + |
| 15 | +import sys |
| 16 | +import random |
| 17 | + |
| 18 | +from markov import skip_gutenberg_header, shift |
| 19 | + |
| 20 | + |
| 21 | +class Markov(object): |
| 22 | + """Encapsulates the statistical summary of a text.""" |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + self.suffix_map = {} # map from prefixes to a list of suffixes |
| 26 | + self.prefix = () # current tuple of words |
| 27 | + |
| 28 | + def process_file(self, filename, order=2): |
| 29 | + """Reads a file and performs Markov analysis. |
| 30 | +
|
| 31 | + filename: string |
| 32 | + order: integer number of words in the prefix |
| 33 | +
|
| 34 | + Returns: map from prefix to list of possible suffixes. |
| 35 | + """ |
| 36 | + fp = open(filename) |
| 37 | + skip_gutenberg_header(fp) |
| 38 | + |
| 39 | + for line in fp: |
| 40 | + for word in line.rstrip().split(): |
| 41 | + self.process_word(word, order) |
| 42 | + |
| 43 | + def process_word(self, word, order=2): |
| 44 | + """Processes each word. |
| 45 | +
|
| 46 | + word: string |
| 47 | + order: integer |
| 48 | +
|
| 49 | + During the first few iterations, all we do is store up the words; |
| 50 | + after that we start adding entries to the dictionary. |
| 51 | + """ |
| 52 | + if len(self.prefix) < order: |
| 53 | + self.prefix += (word,) |
| 54 | + return |
| 55 | + |
| 56 | + try: |
| 57 | + self.suffix_map[self.prefix].append(word) |
| 58 | + except KeyError: |
| 59 | + # if there is no entry for this prefix, make one |
| 60 | + self.suffix_map[self.prefix] = [word] |
| 61 | + |
| 62 | + self.prefix = shift(self.prefix, word) |
| 63 | + |
| 64 | + def random_text(self, n=100): |
| 65 | + """Generates random wordsfrom the analyzed text. |
| 66 | +
|
| 67 | + Starts with a random prefix from the dictionary. |
| 68 | +
|
| 69 | + n: number of words to generate |
| 70 | + """ |
| 71 | + # choose a random prefix (not weighted by frequency) |
| 72 | + start = random.choice(self.suffix_map.keys()) |
| 73 | + |
| 74 | + for i in range(n): |
| 75 | + suffixes = self.suffix_map.get(start, None) |
| 76 | + if suffixes == None: |
| 77 | + # if the prefix isn't in map, we got to the end of the |
| 78 | + # original text, so we have to start again. |
| 79 | + self.random_text(n-i) |
| 80 | + return |
| 81 | + |
| 82 | + # choose a random suffix |
| 83 | + word = random.choice(suffixes) |
| 84 | + print(word, end=' ') |
| 85 | + start = shift(start, word) |
| 86 | + |
| 87 | + |
| 88 | +def main(script, filename='emma.txt', n=100, order=2): |
| 89 | + try: |
| 90 | + n = int(n) |
| 91 | + order = int(order) |
| 92 | + except ValueError: |
| 93 | + print('Usage: %d filename [# of words] [prefix length]' % script) |
| 94 | + else: |
| 95 | + markov = Markov() |
| 96 | + markov.process_file(filename, order) |
| 97 | + markov.random_text(n) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main(*sys.argv) |
| 102 | + |
0 commit comments