Java Reference
In-Depth Information
...
98: ... loglikelihood=-1.4117372921765519 1.0
99: ... loglikelihood=-1.4052738190352423 1.0
100: ... loglikelihood=-1.398916120150312 1.0
The model is saved as shown here using the serialize method. The model is saved to
the en-animal.model file as opened in the previous try-with-resources block:
OutputStream modelOut = null;
modelOut = new BufferedOutputStream(dataOut);
model.serialize(modelOut);
Using DocumentCategorizerME to classify text
Once a model has been created, we can use the DocumentCategorizerME class to
classify text. We need to read the model, create an instance of the DocumentCategor-
izerME class, and then invoke the categorize method to return an array of probabil-
ities that will tell us which category the text best fits in.
Since we are reading from a file, exceptions need to be dealt with, as shown here:
try (InputStream modelIn =
new FileInputStream(new File("en-animal.model"));) {
...
} catch (IOException ex) {
// Handle exceptions
}
With the input stream, we create instances of the DoccatModel and DocumentCat-
egorizerME classes as illustrated here:
DoccatModel model = new DoccatModel(modelIn);
DocumentCategorizerME categorizer =
new DocumentCategorizerME(model);
The categorize method is called using a string as an argument. This returns an array
of double values with each element containing the likelihood that the text belongs to a cat-
egory. The DocumentCategorizerME class' getNumberOfCategories method
Search WWH ::




Custom Search