I am using this which is successful in creating a counter for letters relative to the previous pair.
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
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.
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 > target:
break
I am having trouble adapting it to this second configuration, the output isn’t consistent with what i would expect. Thanks!
I think, you forgot that curr is twoletter combination. The last loop should be changed and curr constructed after it:
Also yield should be changed to produce just one character at a time