I am using this code to generate random text :
from collections import defaultdict, Counter
from itertools import ifilter
from random import choice, randrange
def pairwise(iterable):
it = iter(iterable)
last = next(it)
for curr in it:
yield last, curr
last = curr
valid = set('abcdefghijklmnopqrstuvwxyz ')
def valid_pair((last, curr)):
return last in valid and curr in valid
def make_markov(text):
markov = defaultdict(Counter)
lowercased = (c.lower() for c in text)
for p, q in ifilter(valid_pair, pairwise(lowercased)):
markov[p][q] += 1
return markov
def genrandom(model, n):
curr = choice(list(model))
for i in xrange(n):
yield curr
if curr not in model: # handle case where there is no known successor
curr = choice(list(model)) #i.e. letter appears for first time at end of text
d = model[curr]
target = randrange(sum(d.values()))
cumulative = 0
for curr, cnt in d.items():
cumulative += cnt
if cumulative > target:
break
model = make_markov(**'the fox jumped over the fence'**)
print ''.join(genrandom(model, 280))
However, I would like to use a txt file as input instead of ‘the fox jumped over the fence’
I have been trying with :
text=open('moby.txt','r+').read()
and then:
model = make_markov(text)
But it seems that I have something wrong, I’m not sure how to handle this.
Your parameter
textis supposed to be the entire contents of Moby Dick , passed in as a string? Try passing the location of the text file and use it as a handle to generate your string fromread()Im on my phone, otherwise id also include the
open()statement within atryclause.EDIT
The
withkeyword (if Im not mistaken), uses theyieldstatement behind the scenes, opening the file using a generator. It uses less resources to read large files.