How to do it...

The necessary steps include the following:

  1. Add the following imports to your project:
import java.io.File;
import java.io.IOException;
import com.aliasi.classify.JointClassification;
import com.aliasi.classify.LMClassifier;
import com.aliasi.util.AbstractExternalizable;
  1. In the main method, add the variables, categories and sampleText, to hold the training categories and the text to be classified. The sample text was copied from the Taoism section of the article at https://en.wikipedia.org/wiki/Religion. We used the defining sentence as defined next:
String[] categories = { "soc.religion.christian", "talk.religion.misc", 
"alt.atheism", "misc.forsale" };

String sampleText = "An ancient tradition of philosophy and " +
"belief rooted in Chinese worldview";
  1. Next, add the following try block to instantiate the model:
try {
LMClassifier lmClassifier = (LMClassifier)
AbstractExternalizable.readObject(
new File("classificationModel.model"));

} catch (IOException | ClassNotFoundException ex) {
// Handle exceptions
}
  1. Add the following code to classify the sample text followed by code to display the best category:
JointClassification jointClassification = 
lmClassifier.classify(sampleText);
String bestCategory = jointClassification.bestCategory();
System.out.println("Best Category: " + bestCategory);
  1. Execute the program. You will get the following output:
Best Category: talk.religion.misc
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.149.94