【问题标题】:testing OpenNLP classifier model测试 OpenNLP 分类器模型
【发布时间】:2018-05-05 20:59:56
【问题描述】:

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

【问题讨论】:

    标签: java classification training-data opennlp


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

    希望对你有帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 2013-11-07
      • 2018-04-07
      • 1970-01-01
      • 2020-08-18
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多