Naive Bayes Text Classification Algorithm

Hye there! I just need the help for implementing Naive Bayes Text Classification Algorithm in Java to just test my Data Set for research purposes. It is compulsory to implement the algorithm in Java; rather using Weka or Rapid Miner tools to get the results!


My Data Set has the following type of Data:

    Doc  Words   Category

Means that I have the Training Words and Categories for each training (String) known in advance. Some of the Data Set is given below:

             Doc      Words                                                              Category        
    Training
               1      Integration Communities Process Oriented Structures...(more string)       A
               2      Integration Communities Process Oriented Structures...(more string)       A
               3      Theory Upper Bound Routing Estimate global routing...(more string)        B
               4      Hardware Design Functional Programming Perfect Match...(more string)      C
               .
               .
               .
    Test
               5      Methodology Toolkit Integrate Technological  Organisational
               6      This test contain string naive bayes test text text test

SO the Data Set comes from a MySQL DataBase and it may contain multiple training strings and test strings as well! The thing is I just need to implement Naive Bayes Text Classification Algorithm in Java.

The algorithm should follow the following example mentioned here Table 13.1

Source: Read here


The thing is that I can implement the algorithm in Java Code myself but i just need to know if it is possible that there exist some kind a Java library with source code documentation available to allow me to just test the results.

The problem is I just need the results for just one time only means its just a test for results.

So, come to the point can somebody tell me about any good java library that helps my code this algorithm in Java and that could made my dataset possible to process the results, or can somebody give me any good ideas how to do it easily...something good that can help me.

I will be thankful for your help. Thanks in advance


As per your requirement, you can use the Machine learning library MLlib from apache. The MLlib is Spark's scalable machine learning library consisting of common learning algorithms and utilities. There is also a java code template to implement the algorithm utilizing the library. So to begin with, you can:

Implement the java skeleton for the Naive Bayes provided on their site as given below.

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.mllib.classification.NaiveBayes;
import org.apache.spark.mllib.classification.NaiveBayesModel;
import org.apache.spark.mllib.regression.LabeledPoint;
import scala.Tuple2;

JavaRDD<LabeledPoint> training = ... // training set
JavaRDD<LabeledPoint> test = ... // test set

final NaiveBayesModel model = NaiveBayes.train(training.rdd(), 1.0);

JavaPairRDD<Double, Double> predictionAndLabel = 
  test.mapToPair(new PairFunction<LabeledPoint, Double, Double>() {
    @Override public Tuple2<Double, Double> call(LabeledPoint p) {
      return new Tuple2<Double, Double>(model.predict(p.features()), p.label());
    }
  });
double accuracy = predictionAndLabel.filter(new Function<Tuple2<Double, Double>, Boolean>() {
    @Override public Boolean call(Tuple2<Double, Double> pl) {
      return pl._1().equals(pl._2());
    }
  }).count() / (double) test.count();

For testing your datasets, there is no best solution here than use the Spark SQL. MLlib fits into Spark's APIs perfectly. To start using it, I would recommend you to go through the MLlib API first, implementing the Algorithm according to your needs. This is pretty easy using the library. For the next step to allow the processing of your datasets possible, just use the Spark SQL. I will recommend you to stick to this. I too have hunted down multiple options before settling for this easy to use library and it's seamless support for inter-operations with some other technologies. I would have posted the complete code here to perfectly fit your answer. But I think you are good to go.


You can use the Weka Java API and include it in your project if you do not want to use the GUI.

Here's a link to the documentation to incorporate a classifier in your code: https://weka.wikispaces.com/Use+WEKA+in+your+Java+code


Please take a look at the Bow toolkit.

It has a Gnu license and source code. Some of its code includes

Setting word vector weights according to Naive Bayes, TFIDF, and several other methods.

Performing test/train splits, and automatic classification tests.

It's not a Java library, but you could compile the C code to ensure that you Java had similar results for a given corpus.

I also spotted a decent Dr. Dobbs article that implements in Perl. Once again, not the desired Java, but will give you the one-time results that you are asking for.

链接地址: http://www.djcxy.com/p/40160.html

上一篇: 随机森林的简单解释

下一篇: 朴素贝叶斯文本分类算法