ยปCore Development>Code coverage>Tools/demo/markov.py

Python code coverage for Tools/demo/markov.py

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