Search code examples
javamachine-learningnlpopennlp

How to deserialize OpenNLP trained model?


I'm using OpenNLP with Java to classify text. I have used DoccatModel object. Then I save it with the serialize method. I would like to know how to create the model from the .bin saved file.

Whitout that, I need to train the model each time that I launch the program.

DoccatModel model = trainedModel(trainingDatasetPath);
serializeModel(model);

The method trainedModel returns a DoccatModel object trained by the file located at trainingDatasetPath.

Does anyone have any idea ?


Solution

  • I have found the answer: ´DoccatModel´ has a constructor for that.

    DoccatModel model;
    if(model_file.exists()) {
        model = new DoccatModel(model_file);
    } else {
        model = trainedModel(trainingDatasetPath);
        serializeModel(model);
    }
    DocumentCategorizer doccat = new DocumentCategorizerME(model);