annotate Demo/scripts/markov.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 3130b7e812b0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1421
diff changeset
1 #! /usr/bin/env python
1421
fe7298813f17 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
fe7298813f17 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 class Markov:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
4 def __init__(self, histsize, choice):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
5 self.histsize = histsize
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
6 self.choice = choice
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
7 self.trans = {}
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
8
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
9 def add(self, state, next):
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
10 self.trans.setdefault(state, []).append(next)
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
11
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
12 def put(self, seq):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
13 n = self.histsize
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
14 add = self.add
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
15 add(None, seq[:0])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
16 for i in range(len(seq)):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
17 add(seq[max(0, i-n):i], seq[i:i+1])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
18 add(seq[len(seq)-n:], None)
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
19
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
20 def get(self):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
21 choice = self.choice
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
22 trans = self.trans
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
23 n = self.histsize
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
24 seq = choice(trans[None])
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
25 while True:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
26 subseq = seq[max(0, len(seq)-n):]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
27 options = trans[subseq]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
28 next = choice(options)
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
29 if not next:
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
30 break
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
31 seq += next
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
32 return seq
1421
fe7298813f17 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
34
1421
fe7298813f17 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def test():
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
36 import sys, random, getopt
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
37 args = sys.argv[1:]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
38 try:
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
39 opts, args = getopt.getopt(args, '0123456789cdwq')
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
40 except getopt.error:
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
41 print 'Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0]
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
42 print 'Options:'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
43 print '-#: 1-digit history size (default 2)'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
44 print '-c: characters (default)'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
45 print '-w: words'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
46 print '-d: more debugging output'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
47 print '-q: no debugging output'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
48 print 'Input files (default stdin) are split in paragraphs'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
49 print 'separated blank lines and each paragraph is split'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
50 print 'in words by whitespace, then reconcatenated with'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
51 print 'exactly one space separating words.'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
52 print 'Output consists of paragraphs separated by blank'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
53 print 'lines, where lines are no longer than 72 characters.'
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
54 sys.exit(2)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
55 histsize = 2
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
56 do_words = False
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
57 debug = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
58 for o, a in opts:
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
59 if '-0' <= o <= '-9': histsize = int(o[1:])
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
60 if o == '-c': do_words = False
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
61 if o == '-d': debug += 1
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
62 if o == '-q': debug = 0
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
63 if o == '-w': do_words = True
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
64 if not args:
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
65 args = ['-']
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
66
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
67 m = Markov(histsize, random.choice)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
68 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
69 for filename in args:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
70 if filename == '-':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
71 f = sys.stdin
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
72 if f.isatty():
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
73 print 'Sorry, need stdin from file'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
74 continue
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
75 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
76 f = open(filename, 'r')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
77 if debug: print 'processing', filename, '...'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
78 text = f.read()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
79 f.close()
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
80 paralist = text.split('\n\n')
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
81 for para in paralist:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
82 if debug > 1: print 'feeding ...'
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
83 words = para.split()
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
84 if words:
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
85 if do_words:
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
86 data = tuple(words)
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
87 else:
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
88 data = ' '.join(words)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
89 m.put(data)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
90 except KeyboardInterrupt:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
91 print 'Interrupted -- continue with data read so far'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
92 if not m.trans:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
93 print 'No valid input files'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
94 return
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
95 if debug: print 'done.'
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
96
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
97 if debug > 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
98 for key in m.trans.keys():
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
99 if key is None or len(key) < histsize:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
100 print repr(key), m.trans[key]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
101 if histsize == 0: print repr(''), m.trans['']
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
102 print
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
103 while True:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
104 data = m.get()
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
105 if do_words:
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
106 words = data
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
107 else:
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
108 words = data.split()
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
109 n = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
110 limit = 72
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
111 for w in words:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
112 if n + len(w) > limit:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
113 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
114 n = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
115 print w,
56469
3130b7e812b0 Update markov demo.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
116 n += len(w) + 1
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
117 print
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
118 print
1421
fe7298813f17 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
33456
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
120 if __name__ == "__main__":
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
121 test()