markov.py 3.6 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3

"""
Markov chain simulation of words or characters.
"""
Guido van Rossum's avatar
Guido van Rossum committed
6 7

class Markov:
8 9 10 11
    def __init__(self, histsize, choice):
        self.histsize = histsize
        self.choice = choice
        self.trans = {}
12

13
    def add(self, state, next):
14 15
        self.trans.setdefault(state, []).append(next)

16 17 18 19 20 21 22
    def put(self, seq):
        n = self.histsize
        add = self.add
        add(None, seq[:0])
        for i in range(len(seq)):
            add(seq[max(0, i-n):i], seq[i:i+1])
        add(seq[len(seq)-n:], None)
23

24 25 26 27 28
    def get(self):
        choice = self.choice
        trans = self.trans
        n = self.histsize
        seq = choice(trans[None])
29
        while True:
30 31 32
            subseq = seq[max(0, len(seq)-n):]
            options = trans[subseq]
            next = choice(options)
33 34 35
            if not next:
                break
            seq += next
36
        return seq
Guido van Rossum's avatar
Guido van Rossum committed
37

38

Guido van Rossum's avatar
Guido van Rossum committed
39
def test():
40
    import sys, random, getopt
41 42
    args = sys.argv[1:]
    try:
43
        opts, args = getopt.getopt(args, '0123456789cdwq')
44
    except getopt.error:
45
        print('Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0])
46 47 48 49 50 51 52 53 54 55 56 57
        print('Options:')
        print('-#: 1-digit history size (default 2)')
        print('-c: characters (default)')
        print('-w: words')
        print('-d: more debugging output')
        print('-q: no debugging output')
        print('Input files (default stdin) are split in paragraphs')
        print('separated blank lines and each paragraph is split')
        print('in words by whitespace, then reconcatenated with')
        print('exactly one space separating words.')
        print('Output consists of paragraphs separated by blank')
        print('lines, where lines are no longer than 72 characters.')
58
        sys.exit(2)
59
    histsize = 2
60
    do_words = False
61 62
    debug = 1
    for o, a in opts:
63 64 65
        if '-0' <= o <= '-9': histsize = int(o[1:])
        if o == '-c': do_words = False
        if o == '-d': debug += 1
66
        if o == '-q': debug = 0
67 68 69 70
        if o == '-w': do_words = True
    if not args:
        args = ['-']

71 72 73 74 75 76
    m = Markov(histsize, random.choice)
    try:
        for filename in args:
            if filename == '-':
                f = sys.stdin
                if f.isatty():
77
                    print('Sorry, need stdin from file')
78 79 80
                    continue
            else:
                f = open(filename, 'r')
81
            if debug: print('processing', filename, '...')
82 83
            text = f.read()
            f.close()
84
            paralist = text.split('\n\n')
85
            for para in paralist:
86
                if debug > 1: print('feeding ...')
87
                words = para.split()
88
                if words:
89 90 91 92
                    if do_words:
                        data = tuple(words)
                    else:
                        data = ' '.join(words)
93 94
                    m.put(data)
    except KeyboardInterrupt:
95
        print('Interrupted -- continue with data read so far')
96
    if not m.trans:
97
        print('No valid input files')
98
        return
99
    if debug: print('done.')
100

101
    if debug > 1:
102
        for key in m.trans.keys():
103
            if key is None or len(key) < histsize:
104 105 106
                print(repr(key), m.trans[key])
        if histsize == 0: print(repr(''), m.trans[''])
        print()
107
    while True:
108
        data = m.get()
109 110 111 112
        if do_words:
            words = data
        else:
            words = data.split()
113 114 115 116
        n = 0
        limit = 72
        for w in words:
            if n + len(w) > limit:
117
                print()
118
                n = 0
119
            print(w, end=' ')
120
            n += len(w) + 1
121 122
        print()
        print()
Guido van Rossum's avatar
Guido van Rossum committed
123

124 125
if __name__ == "__main__":
    test()