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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:52:01+00:00 2026-05-16T14:52:01+00:00

I’m working on a random text generator -without using Markov chains- and currently it

  • 0

I’m working on a random text generator -without using Markov chains- and currently it works without too many problems -actually generates a good amount of random sentences by my criteria but I want to make it even more accurate to prevent as many sentence repeats as possible-. Firstly, here is my code flow:

  1. Enter a sentence as input -this is called trigger string, is assigned to a variable-

  2. Get longest word in trigger string

  3. Search all Project Gutenberg database for sentences that contain this word -regardless of uppercase lowercase-

  4. Return the longest sentence that has the word I spoke about in step 3

  5. Append the sentence in Step 1 and Step4 together

  6. Assign the sentence in Step 4 as the new ‘trigger’ sentence and repeat the process. Note that I have to get the longest word in second sentence and continue like that and so on-

And here is my code:

import nltk

from nltk.corpus import gutenberg

from random import choice

import smtplib #will be for send e-mail option later

triggerSentence = raw_input("Please enter the trigger sentence: ")#get input str

longestLength = 0

longestString = ""

longestLen2 = 0

longestStr2 = ""

listOfSents = gutenberg.sents() #all sentences of gutenberg are assigned -list of list format-

listOfWords = gutenberg.words()# all words in gutenberg books -list format-

while triggerSentence:#run the loop so long as there is a trigger sentence
    sets = []
    sets2 = []
    split_str = triggerSentence.split()#split the sentence into words

    #code to find the longest word in the trigger sentence input
    for piece in split_str:
        if len(piece) > longestLength:
            longestString = piece
            longestLength = len(piece)





    #code to get the sentences containing the longest word, then selecting
    #random one of these sentences that are longer than 40 characters

    for sentence in listOfSents:
        if sentence.count(longestString):
            sents= " ".join(sentence)
            if len(sents) > 40:
                sets.append(" ".join(sentence))


    triggerSentence = choice(sets)
    print triggerSentence #the first sentence that comes up after I enter input-
    split_str = triggerSentence.split()

    for apiece in triggerSentence: #find the longest word in this new sentence
        if len(apiece) > longestLen2:
            longestStr2 = piece
            longestLen2 = len(apiece)
    if longestStr2 == longestString:
        second_longest = sorted(split_str, key=len)[-2]#this should return the second longest word in the sentence in case it's longest word is as same as the longest word of last sentence
    #print second_longest #now get second longest word if first is same
            #as longest word in previous sentence

        for sentence in listOfSents:
            if sentence.count(second_longest):
                sents = " ".join(sentence)
                if len(sents) > 40:
                    sets2.append(" ".join(sentence))
        triggerSentence = choice(sets2)
    else:
        for sentence in listOfSents:
            if sentence.count(longestStr2):
                sents = " ".join(sentence)
                if len(sents) > 40:
                sets.append(" ".join(sentence))
        triggerSentence = choice(sets)


    print triggerSentence

According to my code, once I enter a trigger sentence, I should get another one that contains the longest word of the trigger sentence I entered. Then this new sentence becomes the trigger sentence and it’s longest word is picked. This is where the problem sometimes occurs. I observed that despite the code lines I placed – starting from line 47 to the end- , the algorithm still can pick the same longest word in the sentences that come along, not looking for the second longest word.

For example:

Trigger string = “Scotland is a nice place.”

Sentence 1 = -This is a random sentence with the word Scotland in it-

Now, this is where the problem can occur in my code at times -doesn’t matter whether it comes up in sentence 2 or 942 or zillion or whatever, but I give it in sent.2 for example’s sake-

Sentence 2 = Another sentence that has the word Scotland in it but not the second longest word in sentence 1. According to my code, this sentence should have been some sentence that contained the second longest word in sentence 1, not Scotland !

How can I solve this ? I’m trying to optimize the code as much as possible and any help is welcome.

  • 1 1 Answer
  • 1 View
  • 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-16T14:52:01+00:00Added an answer on May 16, 2026 at 2:52 pm

    There is nothing random about your algorithm at all. It should always be deterministic.

    I’m not quite sure what you want to do here. If it is to generate random words, just use a dictionary and the random module. If you want to grab random sentences from the Gutenberg project, use the random module to pick a work and then a sentence out of that work.

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

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.