|
| 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 | +import sys |
| 15 | +import string |
| 16 | +import random |
| 17 | + |
| 18 | +class Markov: |
| 19 | + def __init__(self): |
| 20 | + self.suffix_map = {} # map from prefixes to a list of suffixes |
| 21 | + self.prefix = () # current tuple of words |
| 22 | + |
| 23 | + def process_file(self, filename, order=2): |
| 24 | + """Reads a file and performs Markov analysis. |
| 25 | +
|
| 26 | + filename: string |
| 27 | + order: integer number of words in the prefix |
| 28 | +
|
| 29 | + returns: map from prefix to list of possible suffixes. |
| 30 | + """ |
| 31 | + fp = open(filename) |
| 32 | + self.skip_gutenberg_header(fp) |
| 33 | + |
| 34 | + for line in fp: |
| 35 | + if line.startswith('*** END OF THIS'): |
| 36 | + break |
| 37 | + |
| 38 | + for word in line.rstrip().split(): |
| 39 | + self.process_word(word, order) |
| 40 | + |
| 41 | + def skip_gutenberg_header(self, fp): |
| 42 | + """Reads from fp until it finds the line that ends the header. |
| 43 | +
|
| 44 | + fp: open file object |
| 45 | + """ |
| 46 | + for line in fp: |
| 47 | + if line.startswith('*** START OF THIS'): |
| 48 | + break |
| 49 | + |
| 50 | + |
| 51 | + def process_word(self, word, order=2): |
| 52 | + """Processes each word. |
| 53 | +
|
| 54 | + word: string |
| 55 | + order: integer |
| 56 | +
|
| 57 | + During the first few iterations, all we do is store up the words; |
| 58 | + after that we start adding entries to the dictionary. |
| 59 | + """ |
| 60 | + if len(self.prefix) < order: |
| 61 | + self.prefix += (word,) |
| 62 | + return |
| 63 | + |
| 64 | + try: |
| 65 | + self.suffix_map[self.prefix].append(word) |
| 66 | + except KeyError: |
| 67 | + # if there is no entry for this prefix, make one |
| 68 | + self.suffix_map[self.prefix] = [word] |
| 69 | + |
| 70 | + self.prefix = self.shift(self.prefix, word) |
| 71 | + |
| 72 | + def shift(self, t, word): |
| 73 | + """Forms a new tuple by removing the head and adding word to the tail. |
| 74 | +
|
| 75 | + t: tuple of strings |
| 76 | + word: string |
| 77 | +
|
| 78 | + Returns: tuple of strings |
| 79 | + """ |
| 80 | + return t[1:] + (word,) |
| 81 | + |
| 82 | + def random_text(self, n=100): |
| 83 | + """Generates random wordsfrom the analyzed text. |
| 84 | +
|
| 85 | + Starts with a random prefix from the dictionary. |
| 86 | +
|
| 87 | + n: number of words to generate |
| 88 | + """ |
| 89 | + # choose a random prefix (not weighted by frequency) |
| 90 | + start = random.choice(list(self.suffix_map.keys())) |
| 91 | + |
| 92 | + for i in range(n): |
| 93 | + suffixes = self.suffix_map.get(start, None) |
| 94 | + if suffixes == None: |
| 95 | + # if the start isn't in map, we got to the end of the |
| 96 | + # original text, so we have to start again. |
| 97 | + self.random_text(n-i) |
| 98 | + return |
| 99 | + |
| 100 | + # choose a random suffix |
| 101 | + word = random.choice(suffixes) |
| 102 | + print(word, end=' ') |
| 103 | + start = self.shift(start, word) |
| 104 | + |
| 105 | + |
| 106 | +def main(script, filename='158-0.txt', n=100, order=2): |
| 107 | + try: |
| 108 | + n = int(n) |
| 109 | + order = int(order) |
| 110 | + except ValueError: |
| 111 | + print('Usage: %d filename [# of words] [prefix length]' % script) |
| 112 | + else: |
| 113 | + m = Markov() |
| 114 | + m.process_file(filename, order) |
| 115 | + m.random_text(n) |
| 116 | + print() |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + main(*sys.argv) |
0 commit comments