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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:21:10+00:00 2026-05-27T21:21:10+00:00

I am using this code to generate random text : from collections import defaultdict,

  • 0

I am using this code to generate random text :

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 > target:
                break

model = make_markov(**'the fox jumped over the fence'**)
print ''.join(genrandom(model, 280))

However, I would like to use a txt file as input instead of ‘the fox jumped over the fence’
I have been trying with :

text=open('moby.txt','r+').read()

and then:

model = make_markov(text)

But it seems that I have something wrong, I’m not sure how to handle this.

  • 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-27T21:21:11+00:00Added an answer on May 27, 2026 at 9:21 pm

    Your parameter text is supposed to be the entire contents of Moby Dick , passed in as a string? Try passing the location of the text file and use it as a handle to generate your string from read()

    Def makemarcov(fileloc):
        with open(fileloc, 'r') as f:
            text = f.read()
        #proceed...
    

    Im on my phone, otherwise id also include the open() statement within a try clause.

    EDIT

    The with keyword (if Im not mistaken), uses the yield statement behind the scenes, opening the file using a generator. It uses less resources to read large files.

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

Sidebar

Related Questions

I am using this line of code to generate a random list of integers:
I am using this code to using a Random generator to generate random positions.
I must generate a random number using boost libraries, I use this code: boost::mt19937
I've been using this code to generate a random number with js: var max
This is the code that I'm using to generate a DH keypair: KeyPairGenerator keyGen
When using this code (simplified for asking): var rows1 = (from t1 in db.TABLE1
I'm using this code from Microsoft to play audio notifications for my application. It's
This is the code I am using for the encryption but it generate an
I am trying to generate random text using letter frequencies that I have obtained.
Using PHP, what are some ways to generate a random confirmation code that can

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.