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 6986303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:47:31+00:00 2026-05-27T18:47:31+00:00

I would like to generate a random text using letter frequencies from a book

  • 0

I would like to generate a random text using letter frequencies from a book in a .txt file, so that each new character (string.lowercase + ' ') depends on the previous one.

How do I use Markov chains to do so? Or is it simpler to use 27 arrays with conditional frequencies for each letter?

  • 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-27T18:47:32+00:00Added an answer on May 27, 2026 at 6:47 pm

    I would like to generate a random text using letter frequencies from a
    book in a txt file

    Consider using collections.Counter to build-up the frequencies when looping over the text file two letters at a time.

    How do I use markov chains to do so? Or is it simpler to use 27 arrays
    with conditional frequencies for each letter?

    The two statements are equivalent. The Markov chain is what you’re doing. The 27 arrays with conditional frequencies is how you’re doing it.

    Here is some dictionary based code to get you started:

    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))
            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 qui_.ck brown fox')
    print ''.join(genrandom(model, 20))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate random text strings of a particular format. Would like some
I would like to generate a WSDL file from a c++ atl webservice without
I am using this code to generate random text : from collections import defaultdict,
Using Groovy, I'd like to generate a random sequence of characters from a given
I am using arc4random to generate a random number. I would like to generate
I would like to generate a random floating point number between 2 values. What
i would like to generate a very simple report with some images and text
I would like to generate random points on a 3D box defined by its
Given a regexp, I would like to generate random data x number of time
I would like to generate a random filename in unix shell (say tcshell). The

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.