Search code examples
javatraining-dataopennlp

Training a categorizer model Opennlp


Im trying to train a model with the code below but i keep getting an error on the DocumentCategorizerME.train() method it tells me to change the factory to doccatfactory. why?

public void trainModel() 
{
    DoccatModel model = null;
    InputStream dataIn = null;

    try
    {
        InputStreamFactory factory = getInputStreamFactory(new File("D:/training.txt"));
        ObjectStream<String> lineStream = new PlainTextByLineStream(factory, Charset.defaultCharset());
        ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);
        TrainingParameters params = new TrainingParameters();
        params.put(TrainingParameters.ITERATIONS_PARAM, "100");
        params.put(TrainingParameters.CUTOFF_PARAM, "0");

        model = DocumentCategorizerME.train("en", sampleStream, params, factory);

    }



}

public static InputStreamFactory getInputStreamFactory(final File file) throws IOException{
    return new InputStreamFactory() {

        @Override
        public InputStream createInputStream() throws IOException {
            return new FileInputStream(file);
        }
    };
}

Solution

  • when you use the DocumentCategorizerME.train(...) method, you need to pass in a DoccatFactory not an InputStreamFactory. Try:

      model = DocumentCategorizerME.train("en", sampleStream, params, new DoccatFactory());
    

    Hope it helps.