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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:08:18+00:00 2026-06-14T21:08:18+00:00

Hello: Just a quick question.. I hope. I am trying to use this program

  • 0

Hello: Just a quick question.. I hope.
I am trying to use this program to generate random text from a corpus.. in this case a portion of a book.

I have a text file that is my corpus: (This is the intro, won’t post the whole thing here)

The Project Gutenberg EBook of My Man Jeeves, by P. G. Wodehouse
#27 in our series by P. G. Wodehouse

Copyright laws are changing all over the world. Be sure to check the
copyright laws for your country before downloading or redistributing
this or any other Project Gutenberg eBook.

This header should be the first thing seen when viewing this Project
Gutenberg file.  Please do not remove it.  Do not change or edit the
header without written permission.

Please read the "legal small print," and other information about the
eBook and Project Gutenberg at the bottom of this file.  Included is
important information about your specific rights and restrictions in
how the file may be used.  You can also find out about how to make a
donation to Project Gutenberg, and how to get involved.

etc etc etc

Next I have the class I am trying to use:

import random

class Markov(object):

    def __init__(self, open_file):
        self.cache = {}
        self.open_file = open_file
        self.words = self.file_to_words()
        self.word_size = len(self.words)
        self.database()


def file_to_words(self):
    self.open_file.seek(0)
    data = self.open_file.read()
    words = data.split()
    return words


def triples(self):
    """ Generates triples from the given data string. So if our string were
            "What a lovely day", we'd generate (What, a, lovely) and then
            (a, lovely, day).
    """

    if len(self.words) < 3:
        return

    for i in range(len(self.words) - 2):
        yield (self.words[i], self.words[i+1], self.words[i+2])

def database(self):
    for w1, w2, w3 in self.triples():
        key = (w1, w2)
        if key in self.cache:
            self.cache[key].append(w3)
        else:
            self.cache[key] = [w3]

def generate_markov_text(self, size=25):
    seed = random.randint(0, self.word_size-3)
    seed_word, next_word = self.words[seed], self.words[seed+1]
    w1, w2 = seed_word, next_word
    gen_words = []
    for i in xrange(size):
        gen_words.append(w1)
        w1, w2 = w2, random.choice(self.cache[(w1, w2)])
    gen_words.append(w2)
    return ' '.join(gen_words)

And finally the main that gives the error: “‘Markov’ object has no attribute ‘file_to_words'”

import Class
file_ = open('derp.txt')
markov = Class.Markov(file_)
markov.generate_markov_text()

What is going wrong here? 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-06-14T21:08:19+00:00Added an answer on June 14, 2026 at 9:08 pm

    You need to indent the file_to_words method so that it is part of the Markov class. The way you have it at the moment it is a module level function in the Class function. Move everything in the file_to_words method (including the def line) 4 spaces to the right.

    Update: The same goes for all the other methods too. Python uses whitespace/indentation to denote scope.

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

Sidebar

Related Questions

Just a quick question. I can write char string[] = Hello world; char *p
I have written a small program just like hello the world , everything was
I'm just learning Ruby and making a simple Hello World program, put for some
I just did a syntax like this mysql_real_escape_string($var = 'Hello'); print_r($var); and I got
I just started learning C, and wrote my hello world program: #include <stdio.h> main()
Just a quick question. I have one XML and I was hoping to tranform
Hello i was just wondering is there anyway to reverse this code so that
The following C# program (built with csc hello.cs ) prints just Hello via Console!
Hello i have a quick question. I get 2 flash messages, the devise one
Hello I just watched the first/Day1 screencast on Nettuts CodeIgniter from scracth And 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.