Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:23:31+00:00 2026-05-28T01:23:31+00:00

I am using this which is successful in creating a counter for letters relative

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:23:31+00:00Added an answer on May 28, 2026 at 1:23 am

    I think, you forgot that curr is twoletter combination. The last loop should be changed and curr constructed after it:

        for newcurr, cnt in d.items():
            cumulative += cnt
            if cumulative > target:
                break
    
        curr = curr[1] + newcurr
    

    Also yield should be changed to produce just one character at a time

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this to determine which view to go to next, from the
So I've been using this code to automatically serialize my User business object which
I have a nested oredered list which i m animating using this code... var
Currently, I am using this query, which does a pretty good job of finding
I am loading feed-items into a ul using this jQuery .ajax() call, which I
I've got this textBox which triggers off an ajax request using jQuery: <asp:TextBox ID=postcodeTextBox
I'm making call to Alfresco Webscripts which return JSON. I do this using GET
A WebDAV library I'm using is issuing this request MKCOL /collection HTTP/1.1 To which
I have this legacy database for which I'm building a custom viewer using Linq
I'm using DbLinq which should be the equivalent of Linq2SQL for this question. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.