You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2014/02/28 15:35:28 UTC

svn commit: r1572959 - in /opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools: cmdline/parser/ParserEvaluatorTool.java parser/ParserEvaluationMonitor.java parser/ParserEvaluator.java

Author: joern
Date: Fri Feb 28 14:35:28 2014
New Revision: 1572959

URL: http://svn.apache.org/r1572959
Log:
OPENNLP-31 First draft of parser eval

Added:
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/parser/ParserEvaluatorTool.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluationMonitor.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java

Added: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/parser/ParserEvaluatorTool.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/parser/ParserEvaluatorTool.java?rev=1572959&view=auto
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/parser/ParserEvaluatorTool.java (added)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/parser/ParserEvaluatorTool.java Fri Feb 28 14:35:28 2014
@@ -0,0 +1,68 @@
+/*
+ * 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.cmdline.parser;
+
+import java.io.IOException;
+
+import opennlp.tools.cmdline.AbstractEvaluatorTool;
+import opennlp.tools.cmdline.TerminateToolException;
+import opennlp.tools.cmdline.params.EvaluatorParams;
+import opennlp.tools.parser.Parse;
+import opennlp.tools.parser.Parser;
+import opennlp.tools.parser.ParserEvaluator;
+import opennlp.tools.parser.ParserFactory;
+import opennlp.tools.parser.ParserModel;
+
+public class ParserEvaluatorTool extends AbstractEvaluatorTool<Parse, EvaluatorParams> {
+
+  public ParserEvaluatorTool() {
+    super(Parse.class, EvaluatorParams.class);
+  }
+  
+  @Override
+  public void run(String format, String[] args) {
+    
+    super.run(format, args);
+    
+    ParserModel model = new ParserModelLoader().load(params.getModel());
+    
+    Parser parser = ParserFactory.create(model);
+    
+    ParserEvaluator evaluator = new ParserEvaluator(parser);
+    
+    System.out.print("Evaluating ... ");
+    try {
+      evaluator.evaluate(sampleStream);
+    }
+    catch (IOException e) {
+      System.err.println("failed");
+      throw new TerminateToolException(-1, "IO error while reading test data: " + e.getMessage(), e);
+    } finally {
+      try {
+        sampleStream.close();
+      } catch (IOException e) {
+        // sorry that this can fail
+      }
+    }
+    System.out.println("done");
+    
+    System.out.println();
+
+    System.out.println(evaluator.getFMeasure());
+  }
+}

Added: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluationMonitor.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluationMonitor.java?rev=1572959&view=auto
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluationMonitor.java (added)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluationMonitor.java Fri Feb 28 14:35:28 2014
@@ -0,0 +1,23 @@
+/*
+ * 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 opennlp.tools.util.eval.EvaluationMonitor;
+
+public interface ParserEvaluationMonitor extends EvaluationMonitor<Parse> {
+}

Added: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java?rev=1572959&view=auto
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java (added)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/parser/ParserEvaluator.java Fri Feb 28 14:35:28 2014
@@ -0,0 +1,108 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+
+import opennlp.tools.cmdline.parser.ParserTool;
+import opennlp.tools.util.Span;
+import opennlp.tools.util.eval.Evaluator;
+import opennlp.tools.util.eval.FMeasure;
+
+public class ParserEvaluator extends Evaluator<Parse> {
+
+  private FMeasure fmeasure = new FMeasure();
+  
+  private final Parser parser;
+
+  public ParserEvaluator(Parser parser, ParserEvaluationMonitor... monitors) {
+    super(monitors);
+    this.parser = parser;
+  }
+  
+  private static Span[] getConstituencySpans(Parse parse) {
+    
+    Stack<Parse> stack = new Stack<Parse>();
+    
+    if (parse.getChildCount() > 0) {
+      stack.add(parse.getChildren()[0]);
+    }
+    
+    List<Span> consts = new ArrayList<Span>();
+    
+    while (!stack.isEmpty()) {
+      
+      Parse constSpan = stack.pop();
+      
+      if (!constSpan.isPosTag()) {
+        Span span = constSpan.getSpan();
+        consts.add(new Span(span.getStart(), span.getEnd(), constSpan.getType()));
+        
+        for (Parse child : constSpan.getChildren()) {
+          stack.push(child);
+        }
+      }
+    }
+    
+    return consts.toArray(new Span[consts.size()]);
+  }
+  
+  @Override
+  protected Parse processSample(Parse reference) {
+    
+    String sentenceText = reference.getText();
+    
+    Parse predictions[] = ParserTool.parseLine(sentenceText, parser, 1);
+    
+    Parse prediction = null;
+    if (predictions.length > 0) {
+      prediction = predictions[0];
+    }
+    
+    fmeasure.updateScores(getConstituencySpans(reference), getConstituencySpans(prediction));
+    
+    return prediction;
+  }
+  
+  public FMeasure getFMeasure() {
+    return fmeasure;
+  }
+  
+  public static void main(String[] args) {
+    
+    // TODO: Move this to a test case!
+    
+    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)) (. .) ))";
+    Span goldConsts[] = getConstituencySpans(Parse.parseParse(goldParseString));
+    
+    String testParseString = "(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) (NN yesterday))) ))  (. .) ))";
+    Span testConsts[] = getConstituencySpans(Parse.parseParse(testParseString));
+    
+    FMeasure measure = new FMeasure();
+    measure.updateScores(goldConsts, testConsts);
+    
+    // Expected output:
+    // Precision: 0.42857142857142855
+    // Recall: 0.375
+    // F-Measure: 0.39999999999999997
+    
+    System.out.println(measure.toString());
+  }
+}