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

The Archive Base Latest Questions

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

I am getting an NGramModel error… Please tell me where I might be going

  • 0

I am getting an NGramModel error… Please tell me where I might be going wrong.

Traceback (most recent call last):
  File "ai7.py", line 87, in <module>
    tt=NgramModel(1, tText, estimator)
NameError: name 'NgramModel' is not defined

My goal is to calculate the perplexity of the text.

f_in = open("science.txt", 'r');
ln = f_in.read()    

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

s=""
tText = Text(words)
tText1 = Text(my_bigrams)
tText2 = Text(my_trigrams)
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)


tt=NgramModel(1, tText, estimator)
tt1=NgramModel(2, tText1, estimator)
tt2=NgramModel(3, tText2, estimator)


print tt.perplexity(tText)
print tt1.perplexity(tText1)
print tt2.perplexity(tText2)
  • 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-14T15:13:42+00:00Added an answer on June 14, 2026 at 3:13 pm

    You have forgotten to import NgramModel

    You should do something like:

    from nltk.model.ngram import NgramModel
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

GETTING THIS ERROR WHEN UPLOADING A FILE : LoadError (no such file to load
Getting ERR_CONNECTION_RESET after more or less 2 minutes of uploading a rather big file
Getting 'Dispatcher has no subscribers' error while trying to post message to a channel
Getting this weird LINQ error. title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String Here is the code I have:
Getting a strange error when trying to insert data into an Access database using
Getting this error when I put use Blog; at the top. Warning: The use
Getting a parser error when trying to serialize a ulong array, looks like the
Getting Fatal Error while running bin/hadoop namenode -format Using Windows 7 operating system, Under
getting below error after i configure MQ connection factory. java.lang.ClassCastException: com.ibm.ejs.jms.JMSQueueConnectionFactoryHandle incompatible with com.ibm.mq.jms.MQQueueConnectionFactory
Getting the following error: "Warning: Cannot modify header information - headers already sent by

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.