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