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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:46:13+00:00 2026-06-14T19:46:13+00:00

I have to generate random sentences using nltk. However, it seems like text.generate() only

  • 0

I have to generate random sentences using nltk. However, it seems like text.generate() only gives us sentences with trigrams. Is there any way I can expand this to include unigrams as well as bigrams?

My current code is:

exclude = set(string.punctuation)
ln = ''.join(ch for ch in ln if ch not in exclude)


words = nltk.word_tokenize(ln)
my_bigrams = nltk.bigrams(words)
my_trigrams = nltk.trigrams(words)


tText = Text(words)
tText1 = Text(my_bigrams)
tText2 = Text(my_trigrams)
print tText.generate()
print tText1.generate()
print tText2.generate()

Change in generate() function:

def generate(self, length=100, c=3):
    """
    Print random text, generated using a trigram language model.

    :param length: The length of text to generate (default=100)
    :type length: int
    :seealso: NgramModel
    """
    if '_trigram_model' not in self.__dict__:
        print "Building ngram index..."
        estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
        self._trigram_model = NgramModel(c, self, estimator=estimator)
    text = self._trigram_model.generate(length)
    print tokenwrap(text)
  • 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-14T19:46:14+00:00Added an answer on June 14, 2026 at 7:46 pm

    After reading your code, here’s some considerations:

    • nltk.Text takes as an argument words only, not tuples (bigrams, trigrams).

    • nltk.Text.generate() generates text using trigrams exclusively, as the documentation will tell you

    You will need to write the generation function yourself if you want to use unigrams and bigrams. It should be straightforward, taking as a starting point generate()’s source.

    You could even attach it dynamically to the nltk.Text class so it becomes available to the objects: nltk.Text.generate_with_ngrams= my_generation_function (don’t forget to include “self” as first argument)

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

Sidebar

Related Questions

I have to generate five distinct random numbers from 0 to 50 using only
I am trying to generate random text using letter frequencies that I have obtained.
Like my question, i need to generate random numbers that have identical pairs between
Does Java have any functionality to generate random characters or strings? Or must one
I want to generate random datasets with regex. Therefore I have an expression like
I have been studying this code to generate random text: from collections import defaultdict,
I have seen this question: How to generate random variable names in C++ using
I want to generate random colours which can be attractive in pie charts.I have
I have the following code to generate two random numbers var attackRoll = Math.floor((Math.random()*6)+1);
I have a project which uses php's mt_rand() to generate different random integers but

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.