Spaces:
Running
Running
File size: 894 Bytes
1d4f575 |
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 |
import sys
import markov
import random
import pickle
"make the markov model"
file_name = sys.argv[1]
# n-gram length for markov model
n = 1
# build model
model = {}
lines = []
for line in open(file_name, 'r'):
line = line.strip()
words = line.split(' ')
upper_words = []
for word in words:
upper_word = word.upper()
# filter out non alpha but leave apostrophes
for char in upper_word:
if not char.isalpha() and char is not "'":
upper_word = upper_word.replace(char, "")
upper_words.append(upper_word)
lines.append(upper_words)
model = markov.generate_model_from_token_lists(lines, n)
# save pickle
with open('abc_markov.pickle', 'wb') as handle:
pickle.dump(model, handle)
print(random.choice(list(model.keys())))
# print model
print(markov.generate(model, n, max_iterations=3))
def nextword(word):
return markov.generate(model, n, seed=word, max_iterations=1) |