培训分类程序模型Opennlp

时间:2017-10-06 07:02:13

标签: java training-data opennlp

我正在尝试使用下面的代码训练模型,但我在DocumentCategorizerME.train()方法上遇到错误,它告诉我将factory更改为doccatfactory。为什么?

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);
        }
    };
}

1 个答案:

答案 0 :(得分:0)

当您使用DocumentCategorizerME.train(...)方法时,您需要传入DoccatFactory而不是InputStreamFactory。尝试:

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

希望它有所帮助。