forked from barrucadu/markov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.py
182 lines (145 loc) · 6.13 KB
/
repl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import cmd
import shlex
import docopt
import os
import glob
import markovstate
import fileinput
import functools
def decorator_with_arguments(wrapper):
return lambda *args, **kwargs: lambda func: wrapper(func, *args, **kwargs)
@decorator_with_arguments
def arg_wrapper(f, cmd, argstr="", types={}):
@functools.wraps(f)
def wrapper(self, line):
try:
args = docopt.docopt("usage: {} {}".format(cmd, argstr),
argv=shlex.split(line),
help=False)
for k, v in types.items():
try:
if k in args:
args[k] = v[1] if args[k] == [] else v[0](args[k])
except:
args[k] = v[1]
return f(self, args)
except docopt.DocoptExit:
print(cmd + " " + argstr)
return wrapper
class Repl(cmd.Cmd):
"""REPL for Markov interaction. This is way overkill, yay!
"""
def __init__(self):
"""Initialise a new REPL.
"""
super().__init__()
self.markov = markovstate.MarkovState()
def help_generators(self):
print("""Generate a sequence of output:
generator <len> [--seed=<seed>] [--prob=<prob>] [--offset=<offset>] [--cln=<cln>] [--] [<prefix>...]
<len> is the length of the sequence; <seed> is the optional random
seed. If no seed is given, the current system time is used; and <prob>
is the probability of random token choice. The default value for <prob>
is 0. If an offset is give, drop that many tokens from the start of the
output. <cln> is the <n> value to use after a clause ends, the default
is <n>. The optional prefix is used to see the generator with tokens. A
prefix of length longer than the generator's n will be truncated. """)
@arg_wrapper("tokens",
"<len> [--seed=<seed>] [--prob=<prob>] [--offset=<offset>] [--cln=<cln>] [--] [<prefix>...]",
{"<len>": (int,),
"--seed": (int, None),
"--prob": (float, 0),
"--offset": (int, 0),
"--cln": (int, None),
"<prefix>": (tuple, ())})
def do_tokens(self, args):
"""Generate tokens of output. See 'help generators'."""
try:
print(self.markov.generate(args["<len>"], args["--seed"],
args["--prob"], args["--offset"],
args["--cln"],
prefix=args["<prefix>"]))
except markovstate.MarkovStateError as e:
print(e.value)
@arg_wrapper("paragraphs",
"<len> [--seed=<seed>] [--prob=<prob>] [--offset=<offset>] [--cln=<cln>] [--] [<prefix>...]",
{"<len>": (int,),
"--seed": (int, None),
"--prob": (float, 0),
"--offset": (int, 0),
"--cln": (int, None),
"<prefix>": (tuple, ('\n\n',))})
def do_paragraphs(self, args):
"""Generate paragraphs of output. See 'help generators'."""
try:
print(self.markov.generate(args["<len>"], args["--seed"],
args["--prob"], args["--offset"],
endchunkf=lambda t: t == '\n\n',
kill=1, prefix=args["<prefix>"]))
except markovstate.MarkovStateError as e:
print(e.value)
@arg_wrapper("sentences",
"<len> [--seed=<seed>] [--prob=<prob>] [--offset=<offset>] [--cln=<cln>] [--] [<prefix>...]",
{"<len>": (int,),
"--seed": (int, None),
"--prob": (float, 0),
"--offset": (int, 0),
"--cln": (int, None),
"<prefix>": (tuple, ())})
def do_sentences(self, args):
"""Generate sentences of output. See 'help generators'."""
sentence_token = lambda t: t[-1] in ".!?"
try:
print(self.markov.generate(args["<len>"], args["--seed"],
args["--prob"], args["--offset"],
startf=sentence_token,
endchunkf=sentence_token,
prefix=args["<prefix>"]))
except markovstate.MarkovStateError as e:
print(e.value)
@arg_wrapper("continue", "[<len>]", {"<len>": (int, 1)})
def do_continue(self, args):
"""Continue generating output.
continue [<len>]"""
try:
print(self.markov.more(args["<len>"]))
except markovstate.MarkovStateError as e:
print(e.value)
# Loading and saving data
@arg_wrapper("train", "<n> [--noparagraphs] <path> ...", {"<n>": (int,)})
def do_train(self, args):
"""Train a generator on a corpus.
train <n> [--noparagraphs] <path> ...
Discard the current generator, and train a new generator on the given paths.
Wildcards are allowed.
<n> is the length of prefix (producing <n+1>-grams). If the 'noparagraphs'
option is given, paragraph breaks are treated as spaces and discarded, rather
than a separate token.
"""
paths = [path
for ps in args["<path>"]
for path in glob.glob(os.path.expanduser(ps))]
def charinput(paths):
with fileinput.input(paths) as fi:
for line in fi:
for char in line:
yield char
self.markov.train(args["<n>"],
charinput(paths),
noparagraphs=args["--noparagraphs"])
@arg_wrapper("load", "<file>")
def do_load(self, args):
"""Load a generator from disk.
load <file>
Discard the current generator, and load the trained generator in the given
file."""
self.markov.load(args["<file>"])
@arg_wrapper("dump", "<file>")
def do_dump(self, args):
"""Save a generator to disk.
dump <file>
Save the trained generator to the given file."""
try:
self.markov.dump(args["<file>"])
except markovstate.MarkovStateError as e:
print(e.value)