Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 6930b3d | 1993-12-14 10:08:02 +0000 | [diff] [blame] | 2 | |
| 3 | class Markov: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 4 | def __init__(self, histsize, choice): |
| 5 | self.histsize = histsize |
| 6 | self.choice = choice |
| 7 | self.trans = {} |
| 8 | def add(self, state, next): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 9 | if state not in self.trans: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 10 | self.trans[state] = [next] |
| 11 | else: |
| 12 | self.trans[state].append(next) |
| 13 | def put(self, seq): |
| 14 | n = self.histsize |
| 15 | add = self.add |
| 16 | add(None, seq[:0]) |
| 17 | for i in range(len(seq)): |
| 18 | add(seq[max(0, i-n):i], seq[i:i+1]) |
| 19 | add(seq[len(seq)-n:], None) |
| 20 | def get(self): |
| 21 | choice = self.choice |
| 22 | trans = self.trans |
| 23 | n = self.histsize |
| 24 | seq = choice(trans[None]) |
| 25 | while 1: |
| 26 | subseq = seq[max(0, len(seq)-n):] |
| 27 | options = trans[subseq] |
| 28 | next = choice(options) |
| 29 | if not next: break |
| 30 | seq = seq + next |
| 31 | return seq |
Guido van Rossum | 6930b3d | 1993-12-14 10:08:02 +0000 | [diff] [blame] | 32 | |
| 33 | def test(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 34 | import sys, string, random, getopt |
| 35 | args = sys.argv[1:] |
| 36 | try: |
| 37 | opts, args = getopt.getopt(args, '0123456789cdw') |
| 38 | except getopt.error: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 39 | print('Usage: markov [-#] [-cddqw] [file] ...') |
| 40 | print('Options:') |
| 41 | print('-#: 1-digit history size (default 2)') |
| 42 | print('-c: characters (default)') |
| 43 | print('-w: words') |
| 44 | print('-d: more debugging output') |
| 45 | print('-q: no debugging output') |
| 46 | print('Input files (default stdin) are split in paragraphs') |
| 47 | print('separated blank lines and each paragraph is split') |
| 48 | print('in words by whitespace, then reconcatenated with') |
| 49 | print('exactly one space separating words.') |
| 50 | print('Output consists of paragraphs separated by blank') |
| 51 | print('lines, where lines are no longer than 72 characters.') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 52 | histsize = 2 |
| 53 | do_words = 0 |
| 54 | debug = 1 |
| 55 | for o, a in opts: |
| 56 | if '-0' <= o <= '-9': histsize = eval(o[1:]) |
| 57 | if o == '-c': do_words = 0 |
| 58 | if o == '-d': debug = debug + 1 |
| 59 | if o == '-q': debug = 0 |
| 60 | if o == '-w': do_words = 1 |
| 61 | if not args: args = ['-'] |
| 62 | m = Markov(histsize, random.choice) |
| 63 | try: |
| 64 | for filename in args: |
| 65 | if filename == '-': |
| 66 | f = sys.stdin |
| 67 | if f.isatty(): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 68 | print('Sorry, need stdin from file') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 69 | continue |
| 70 | else: |
| 71 | f = open(filename, 'r') |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 72 | if debug: print('processing', filename, '...') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 73 | text = f.read() |
| 74 | f.close() |
| 75 | paralist = string.splitfields(text, '\n\n') |
| 76 | for para in paralist: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 77 | if debug > 1: print('feeding ...') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 78 | words = string.split(para) |
| 79 | if words: |
| 80 | if do_words: data = tuple(words) |
| 81 | else: data = string.joinfields(words, ' ') |
| 82 | m.put(data) |
| 83 | except KeyboardInterrupt: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 84 | print('Interrupted -- continue with data read so far') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 85 | if not m.trans: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 86 | print('No valid input files') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 87 | return |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 88 | if debug: print('done.') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 89 | if debug > 1: |
Skip Montanaro | 1e8ce58 | 2007-08-06 21:07:53 +0000 | [diff] [blame^] | 90 | for key in m.trans.keys(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 91 | if key is None or len(key) < histsize: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 92 | print(repr(key), m.trans[key]) |
| 93 | if histsize == 0: print(repr(''), m.trans['']) |
| 94 | print() |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 95 | while 1: |
| 96 | data = m.get() |
| 97 | if do_words: words = data |
| 98 | else: words = string.split(data) |
| 99 | n = 0 |
| 100 | limit = 72 |
| 101 | for w in words: |
| 102 | if n + len(w) > limit: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 103 | print() |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 104 | n = 0 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 105 | print(w, end=' ') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 106 | n = n + len(w) + 1 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 107 | print() |
| 108 | print() |
Guido van Rossum | 6930b3d | 1993-12-14 10:08:02 +0000 | [diff] [blame] | 109 | |
| 110 | def tuple(list): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 111 | if len(list) == 0: return () |
| 112 | if len(list) == 1: return (list[0],) |
| 113 | i = len(list)/2 |
| 114 | return tuple(list[:i]) + tuple(list[i:]) |
Guido van Rossum | 6930b3d | 1993-12-14 10:08:02 +0000 | [diff] [blame] | 115 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 116 | if __name__ == "__main__": |
| 117 | test() |