You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by co...@apache.org on 2012/02/27 17:19:19 UTC

svn commit: r1294201 - in /opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag: POSTaggerCrossValidatorTool.java POSTaggerEvaluatorTool.java

Author: colen
Date: Mon Feb 27 16:19:19 2012
New Revision: 1294201

URL: http://svn.apache.org/viewvc?rev=1294201&view=rev
Log:
OPENNLP-449: Modified POSTagger evaluator tools to use the new report.

Modified:
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerCrossValidatorTool.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerEvaluatorTool.java

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerCrossValidatorTool.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerCrossValidatorTool.java?rev=1294201&r1=1294200&r2=1294201&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerCrossValidatorTool.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerCrossValidatorTool.java Mon Feb 27 16:19:19 2012
@@ -17,12 +17,18 @@
 
 package opennlp.tools.cmdline.postag;
 
+import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import opennlp.tools.cmdline.AbstractCrossValidatorTool;
+import opennlp.tools.cmdline.ArgumentParser.OptionalParameter;
 import opennlp.tools.cmdline.CmdLineUtil;
 import opennlp.tools.cmdline.TerminateToolException;
+import opennlp.tools.cmdline.ArgumentParser.ParameterDescription;
 import opennlp.tools.cmdline.params.CVParams;
 import opennlp.tools.cmdline.postag.POSTaggerCrossValidatorTool.CVToolParams;
 import opennlp.tools.postag.POSDictionary;
@@ -35,6 +41,9 @@ public final class POSTaggerCrossValidat
     extends AbstractCrossValidatorTool<POSSample, CVToolParams> {
   
   interface CVToolParams extends CVParams, TrainingParams {
+    @ParameterDescription(valueName = "outputFile", description = "the path of the fine-grained report file.")
+    @OptionalParameter
+    File getReportOutputFile();
   }
 
   public POSTaggerCrossValidatorTool() {
@@ -58,6 +67,22 @@ public final class POSTaggerCrossValidat
       missclassifiedListener = new POSEvaluationErrorListener();
     }
 
+    POSTaggerFineGrainedReportListener reportListener = null;
+    File reportFile = params.getReportOutputFile();
+    OutputStream reportOutputStream = null;
+    if (reportFile != null) {
+      CmdLineUtil.checkOutputFile("Report Output File", reportFile);
+      try {
+        reportOutputStream = new FileOutputStream(reportFile);
+        reportListener = new POSTaggerFineGrainedReportListener(
+            reportOutputStream);
+      } catch (FileNotFoundException e) {
+        throw new TerminateToolException(-1,
+            "IO error while creating POS Tagger fine-grained report file: "
+                + e.getMessage());
+      }
+    }
+
     POSTaggerCrossValidator validator;
     try {
       // TODO: Move to util method ...
@@ -67,7 +92,8 @@ public final class POSTaggerCrossValidat
       }
 
       validator = new POSTaggerCrossValidator(factory.getLang(), mlParams,
-          tagdict, params.getNgram(), params.getFactory(), missclassifiedListener);
+          tagdict, params.getNgram(), params.getFactory(),
+          missclassifiedListener, reportListener);
       
       validator.evaluate(sampleStream, params.getFolds());
     } catch (IOException e) {
@@ -82,6 +108,19 @@ public final class POSTaggerCrossValidat
 
     System.out.println("done");
 
+    if (reportListener != null) {
+      System.out.println("Writing fine-grained report to "
+          + params.getReportOutputFile().getAbsolutePath());
+      reportListener.writeReport();
+
+      try {
+        // TODO: is it a problem to close the stream now?
+        reportOutputStream.close();
+      } catch (IOException e) {
+        // nothing to do
+      }
+    }
+
     System.out.println();
 
     System.out.println("Accuracy: " + validator.getWordAccuracy());

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerEvaluatorTool.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerEvaluatorTool.java?rev=1294201&r1=1294200&r2=1294201&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerEvaluatorTool.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/postag/POSTaggerEvaluatorTool.java Mon Feb 27 16:19:19 2012
@@ -17,10 +17,17 @@
 
 package opennlp.tools.cmdline.postag;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import opennlp.tools.cmdline.AbstractEvaluatorTool;
+import opennlp.tools.cmdline.CmdLineUtil;
 import opennlp.tools.cmdline.TerminateToolException;
+import opennlp.tools.cmdline.ArgumentParser.OptionalParameter;
+import opennlp.tools.cmdline.ArgumentParser.ParameterDescription;
 import opennlp.tools.cmdline.params.EvaluatorParams;
 import opennlp.tools.cmdline.postag.POSTaggerEvaluatorTool.EvalToolParams;
 import opennlp.tools.postag.POSEvaluator;
@@ -32,6 +39,9 @@ public final class POSTaggerEvaluatorToo
     extends AbstractEvaluatorTool<POSSample, EvalToolParams> {
 
   interface EvalToolParams extends EvaluatorParams {
+    @ParameterDescription(valueName = "outputFile", description = "the path of the fine-grained report file.")
+    @OptionalParameter
+    File getReportOutputFile();
   }
 
   public POSTaggerEvaluatorTool() {
@@ -52,8 +62,25 @@ public final class POSTaggerEvaluatorToo
       missclassifiedListener = new POSEvaluationErrorListener();
     }
 
+    POSTaggerFineGrainedReportListener reportListener = null;
+    File reportFile = params.getReportOutputFile();
+    OutputStream reportOutputStream = null;
+    if (reportFile != null) {
+      CmdLineUtil.checkOutputFile("Report Output File", reportFile);
+      try {
+        reportOutputStream = new FileOutputStream(reportFile);
+        reportListener = new POSTaggerFineGrainedReportListener(
+            reportOutputStream);
+      } catch (FileNotFoundException e) {
+        throw new TerminateToolException(-1,
+            "IO error while creating POS Tagger fine-grained report file: "
+                + e.getMessage());
+      }
+    }
+
     POSEvaluator evaluator = new POSEvaluator(
-        new opennlp.tools.postag.POSTaggerME(model), missclassifiedListener);
+        new opennlp.tools.postag.POSTaggerME(model), missclassifiedListener,
+        reportListener);
 
     System.out.print("Evaluating ... ");
     try {
@@ -72,6 +99,19 @@ public final class POSTaggerEvaluatorToo
 
     System.out.println("done");
 
+    if (reportListener != null) {
+      System.out.println("Writing fine-grained report to "
+          + params.getReportOutputFile().getAbsolutePath());
+      reportListener.writeReport();
+
+      try {
+        // TODO: is it a problem to close the stream now?
+        reportOutputStream.close();
+      } catch (IOException e) {
+        // nothing to do
+      }
+    }
+
     System.out.println();
 
     System.out.println("Accuracy: " + evaluator.getWordAccuracy());