测试OpenNLP分类器模型

时间:2017-11-22 07:49:16

标签: java classification training-data opennlp

我目前正在训练分类器的模型。昨天我发现如果你也测试创建的分类模型会更准确。我尝试在互联网上搜索如何测试模型: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;
    }

1 个答案:

答案 0 :(得分:1)

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

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

opennlp DoccatEvaluator -model model -data sampleData

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社区可以修复),但是想要运行数据,标记化,通过文档分类程序运行并跟踪结果就是您要评估模型的方式。

希望它有所帮助...

相关问题