Random text and counter for pairs of letters
<p>I am using this which is successful in creating a counter for letters relative to the previous pair. </p>

<pre><code>def pairwise(iterable):
    it = iter(iterable)
    last = next(it) + next(it)
    for curr in it:
        yield last, curr
        last = last[1]+curr


valid = set('abcdefghijklmnopqrstuvwxyz ')

def valid_pair((last, curr)):
    return last[0] in valid and last[1] 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
</code></pre>

<p>But i would now like to generate random text with each letter depending on the counter for the previous pair. Here is the code used when a letter only depends on the previous letter. </p>

<pre><code>def genrandom(model, n):
    curr = choice(list(model)) 
    for i in xrange(n):
        yield curr
        if curr not in model:   
            curr = choice(list(model))
        d = model[curr]   
        target = randrange(sum(d.values()))
        cumulative = 0
        for curr, cnt in d.items():
            cumulative += cnt
            if cumulative &gt; target:
                break
</code></pre>

<p>I am having trouble adapting it to this second configuration, the output isn't consistent with what i would expect. Thanks!</p>
