You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by ki...@apache.org on 2022/11/30 14:59:14 UTC
[opennlp] branch master updated: OPENNLP-689 ParserEvaluator test case
This is an automated email from the ASF dual-hosted git repository.
kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/opennlp.git
The following commit(s) were added to refs/heads/master by this push:
new 75192b30 OPENNLP-689 ParserEvaluator test case
75192b30 is described below
commit 75192b30db8851409d17804b07f5f54a4999b516
Author: Martin Wiesner <ma...@hs-heilbronn.de>
AuthorDate: Tue Nov 1 18:26:38 2022 +0100
OPENNLP-689 ParserEvaluator test case
- provides `ParserEvaluatorTest` which uses the original example in the main method of `ParserEvaluator` (removed without providing a test case)
- achieves 100% line coverage
- improves JavaDoc in `ParserEvaluator'
---
.../java/opennlp/tools/parser/ParserEvaluator.java | 11 +--
.../opennlp/tools/parser/ParserEvaluatorTest.java | 84 ++++++++++++++++++++++
2 files changed, 90 insertions(+), 5 deletions(-)
diff --git a/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java b/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java
index fddcde71..54a11858 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java
@@ -27,7 +27,7 @@ import opennlp.tools.util.eval.Evaluator;
import opennlp.tools.util.eval.FMeasure;
/**
- * Class for ParserEvaluator.
+ * Class for {@link Evaluator<Parse>}.
* This ParserEvaluator behaves like EVALB with no exceptions, e.g,
* without removing punctuation tags, or equality between ADVP and PRT
* (as in COLLINS convention). To follow parsing evaluation conventions
@@ -38,17 +38,18 @@ import opennlp.tools.util.eval.FMeasure;
public class ParserEvaluator extends Evaluator<Parse> {
/**
- * fmeasure.
+ * Holds the evaluation results for the last run of {@link #processSample}.
*/
- private FMeasure fmeasure = new FMeasure();
+ private final FMeasure fmeasure = new FMeasure();
/**
* The parser to evaluate.
*/
private final Parser parser;
/**
- * Construct a parser with some evaluation monitors.
- * @param aParser
+ * Construct a {@link Parser} with some evaluation monitors.
+ *
+ * @param aParser A valid {@link Parser} instance.
* @param monitors the evaluation monitors
*/
public ParserEvaluator(final Parser aParser, final ParserEvaluationMonitor... monitors) {
diff --git a/opennlp-tools/src/test/java/opennlp/tools/parser/ParserEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/parser/ParserEvaluatorTest.java
new file mode 100644
index 00000000..11918a8c
--- /dev/null
+++ b/opennlp-tools/src/test/java/opennlp/tools/parser/ParserEvaluatorTest.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.parser;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import opennlp.tools.formats.ResourceAsStreamFactory;
+import opennlp.tools.parser.lang.en.HeadRules;
+import opennlp.tools.util.InputStreamFactory;
+import opennlp.tools.util.PlainTextByLineStream;
+import opennlp.tools.util.TrainingParameters;
+import opennlp.tools.util.eval.FMeasure;
+
+/**
+ * Tests {@link ParserEvaluator}. Samples and test assumptions taken from historic "main" method,
+ * that was once available in the class under test.
+ * See: <a href="https://issues.apache.org/jira/projects/OPENNLP/issues/OPENNLP-689">OPENNLP-689</a>.
+ */
+public class ParserEvaluatorTest {
+
+ private Parser parser;
+
+ @BeforeEach
+ public void setup() throws IOException {
+ InputStreamFactory in = new ResourceAsStreamFactory(getClass(),
+ "/opennlp/tools/parser/parser.train");
+ ParseSampleStream samples = new ParseSampleStream(new PlainTextByLineStream(in, StandardCharsets.UTF_8));
+ Assertions.assertNotNull(samples);
+
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ try (InputStream headRulesIn = cl.getResourceAsStream("opennlp/tools/parser/en_head_rules")) {
+ HeadRules headRules = new HeadRules(new BufferedReader(
+ new InputStreamReader(headRulesIn, StandardCharsets.UTF_8)));
+
+ ParserModel model = opennlp.tools.parser.chunking.Parser.train(
+ "en", samples, headRules, TrainingParameters.defaultParams());
+ parser = ParserFactory.create(model);
+ Assertions.assertNotNull(parser);
+ }
+ }
+
+ @Test
+ void testProcessSample() {
+
+ String goldParseString = "(TOP (S (NP (NNS Sales) (NNS executives)) (VP (VBD were) " +
+ "(VP (VBG examing) (NP (DT the) (NNS figures)) (PP (IN with) (NP (JJ great) (NN care))) )) " +
+ "(NP (NN yesterday)) (. .) ))";
+ ParserEvaluator pe = new ParserEvaluator(parser);
+ Parse p = pe.processSample(Parse.parseParse(goldParseString));
+ Assertions.assertNotNull(p);
+
+ FMeasure measure = pe.getFMeasure();
+ Assertions.assertNotNull(measure);
+
+ // Expected output: Precision: 0.42857142857142855, Recall: 0.375, F-Measure: 0.39999999999999997
+ Assertions.assertEquals(measure.getPrecisionScore(), 0.42857142857142855d, 0d);
+ Assertions.assertEquals(measure.getRecallScore(), 0.375d, 0d);
+ Assertions.assertEquals(measure.getFMeasure(), 0.39999999999999997d, 0d);
+ }
+
+}