Import .txt file and use in markov model
<p>I am using this code to generate random text :</p>

<pre><code>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 &gt; target:
                break

model = make_markov(**'the fox jumped over the fence'**)
print ''.join(genrandom(model, 280))
</code></pre>

<p>However, I would like to use a txt file as input instead of 'the fox jumped over the fence'
I have been trying with :</p>

<pre><code>text=open('moby.txt','r+').read()
</code></pre>

<p>and then:</p>

<pre><code>model = make_markov(text)
</code></pre>

<p>But it seems that I have something wrong, I'm not sure how to handle this.</p>
