-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.py
44 lines (39 loc) · 1.05 KB
/
markov.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
## http://code.activestate.com/recipes/194364/ (r3)
import random;
import sys;
nonword = "\n" # Since we split on whitespace, this can never be a word
sentenceend = (".","?","!")
w1 = nonword
w2 = nonword
# GENERATE TABLE
table = {}
def load(filepath):
w1 = w2 = nonword
for line in open(filepath):
for word in line.split():
table.setdefault( (w1, w2), [] ).append(word)
w1, w2 = w2, word
table.setdefault( (w1, w2), [] ).append(nonword) # Mark the end of the file
return table
def generate(number, table):
# for file in files:
# load(file)
# GENERATE OUTPUT
w1 = nonword
w2 = nonword
sentence = []
sentences = []
#for i in xrange(maxwords):
while len(sentences) < number:
newword = random.choice(table[(w1, w2)])
if newword == nonword:
return []
if newword[-1] in sentenceend:
joined = " ".join(sentence)
if len(joined) > 1:
sentences.append(joined+" "+newword)
sentence = []
else:
sentence.append(newword)
w1, w2 = w2, newword
return sentences