测试 OpenNLP 分类器模型

testing OpenNLP classifier model

我目前正在训练分类器模型。昨天我发现如果你也测试创建的分类模型会更准确。我尝试在互联网上搜索如何测试模型:testing openNLP model。但我无法让它工作。我认为原因是因为我使用的是 OpenNLP 版本 1.83 而不是 1.5。谁能解释一下如何在此版本的 OpenNLP 中正确测试我的模型?

提前致谢。

以下是我训练模型的方式:

public static DoccatModel trainClassifier() throws IOException
    {
        // read the training data
        final int iterations = 100;
        InputStreamFactory dataIn = new MarkableFileInputStreamFactory(new File("src/main/resources/trainingSets/trainingssetTest.txt"));
        ObjectStream<String> lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
        ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);

        // define the training parameters
        TrainingParameters params = new TrainingParameters();
        params.put(TrainingParameters.ITERATIONS_PARAM, iterations+"");
        params.put(TrainingParameters.CUTOFF_PARAM, 0+"");
        params.put(AbstractTrainer.ALGORITHM_PARAM, NaiveBayesTrainer.NAIVE_BAYES_VALUE);

        // create a model from traning data
        DoccatModel model = DocumentCategorizerME.train("NL", sampleStream, params, new DoccatFactory());

        return model;
    }

我可以想出两种方法来测试你的模型。无论哪种方式,您都需要有带注释的文档(带注释的我的意思是专家分类)。

第一种方法涉及使用 opennlp DocCatEvaluator。语法类似于

opennlp DoccatEvaluator -model model -data sampleData

您的示例数据格式应为

OUTCOME <document text....>  

文档以换行符分隔。

第二种方法涉及创建 DocumentCategorizer。就像是: (该模型是您问题中的 DocCat 模型)

DocumentCategorizer categorizer = new DocumentCategorizerME(model);

// could also use: Tokenizer tokenizer = new TokenizerME(tokenizerModel)
Tokenizer tokenizer = WhitespaceTokenizer.INSTANCE();

 // linesample is like in your question...
for(String sample=linesample.read(); sample != null; sample=linesample.read()){
    String[] tokens = tokenizer.tokenize(sample);
    double[] outcomeProb = categorizer.categorize(tokens);
    String sampleOutcome = categorizer.getBestCategory(outcomeProb);

  // check if the outcome is right...
  // keep track of # right and wrong...
}
// calculate agreement metric of your choice

由于我在此处键入代码,因此可能存在一两个语法错误(我或 SO 社区都可以修复),但是 运行 通过您的数据、标记化、运行 的想法它通过文档分类器并跟踪结果是您想要评估模型的方式。

希望对您有所帮助...