• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Naive Bayes Classifier in java for predicting news category from a text file

 
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to implement a program in java that classifies text files into various language categories such as english, french, german etc. Naive Bayes Classifier algorithm is used for this problem. I have got this code from Github and it throws Null pointer exception for lines 19 and 40 of NaiveBayesExample.java program .  I need help in debugging this code. I have created and imported all the required custom packages.

 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the complete error messages?
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I corrected the code and the exception is now gone but the program gives the output as null instead of printing the category name. I need the program to output the news category of the input text.
I figured out that there is a problem in the predict method of the NaiveBayes class which should return the category name but instead returns null.

Here is a list of all the files in the various packages:

NaiveBayes.java





Document.java




FeatureStats.java




NaiveBayesKnowledgeBase.java




NaiveBayesExample.java




FeatureExtraction.java




TextTokenizer.java




I need help in correcting the predict method of NaivesBayes.java so that it returns the category name as output instead of returning null.

As of now, I am getting the following output:

File1.txt : null
File2.txt : null
File3.txt : null

 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start by putting System.out.println() into your predict method to see if maxScoreCategory is ever set to something other than null.  Specifically, insert a line between 291 and 292 to see if that if block is ever entered.  If not, test logprob > maxScore to see what's happening.
 
Khusbu Sinha
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing is printed on the console when I do

The predict method prints nothing when I call it by:

Even when I make the return type void, the predict method prints nothing when called by
although the above System.out.println() statements are present in the method.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, depending on where you put the sys.out statements, that will tell you something.  Maybe predict() isn't even called?

But if I'm going to help you more, I'll need to see the predict() method with the sys.out statements in it.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic