You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by au...@apache.org on 2012/07/21 12:18:25 UTC

svn commit: r1364059 - in /opennlp/trunk/opennlp-maxent/src/main/java/opennlp: maxent/GISTrainer.java model/HashSumEventStream.java model/IndexHashTable.java model/RealValueFileEventStream.java model/TrainUtil.java perceptron/PerceptronTrainer.java

Author: autayeu
Date: Sat Jul 21 10:18:24 2012
New Revision: 1364059

URL: http://svn.apache.org/viewvc?rev=1364059&view=rev
Log:
OPENNLP-526 Exception cleanup in opennlp-maxent. Thanks to Daniel Naber for the patch.

Modified:
    opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java
    opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/HashSumEventStream.java
    opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/IndexHashTable.java
    opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/RealValueFileEventStream.java
    opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/TrainUtil.java
    opennlp/trunk/opennlp-maxent/src/main/java/opennlp/perceptron/PerceptronTrainer.java

Modified: opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java?rev=1364059&r1=1364058&r2=1364059&view=diff
==============================================================================
--- opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java (original)
+++ opennlp/trunk/opennlp-maxent/src/main/java/opennlp/maxent/GISTrainer.java Sat Jul 21 10:18:24 2012
@@ -247,8 +247,9 @@ class GISTrainer {
    */
   public GISModel trainModel(int iterations, DataIndexer di, Prior modelPrior, int cutoff, int threads) {
     
-    if (threads <= 0)
-      throw new IllegalArgumentException("threads must be at leat one or greater!");
+    if (threads <= 0) {
+      throw new IllegalArgumentException("threads must be at least one or greater but is " + threads + "!");
+    }
     
     modelExpects = new MutableContext[threads][];
     
@@ -580,7 +581,7 @@ class GISTrainer {
         // Only runtime exception can be thrown during training, if one was thrown
         // it should be re-thrown. That could for example be a NullPointerException
         // which is caused through a bug in our implementation.
-        throw new RuntimeException(e.getCause());
+        throw new RuntimeException("Exception during training: " + e.getMessage(), e);
       }
       
       // When they are done, retrieve the results ...

Modified: opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/HashSumEventStream.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/HashSumEventStream.java?rev=1364059&r1=1364058&r2=1364059&view=diff
==============================================================================
--- opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/HashSumEventStream.java (original)
+++ opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/HashSumEventStream.java Sat Jul 21 10:18:24 2012
@@ -55,7 +55,7 @@ public class HashSumEventStream implemen
       digest.update(event.toString().getBytes("UTF-8"));
     }
     catch (UnsupportedEncodingException e) {
-      throw new IllegalStateException("UTF-8 encoding is not available!");
+      throw new IllegalStateException("UTF-8 encoding is not available!", e);
     }
     
     return event;

Modified: opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/IndexHashTable.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/IndexHashTable.java?rev=1364059&r1=1364058&r2=1364059&view=diff
==============================================================================
--- opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/IndexHashTable.java (original)
+++ opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/IndexHashTable.java Sat Jul 21 10:18:24 2012
@@ -55,9 +55,10 @@ public class IndexHashTable<T> {
    *           if the entries are not unique
    */
   public IndexHashTable(T mapping[], double loadfactor) {
-    if (loadfactor <= 0 || loadfactor > 1)
+    if (loadfactor <= 0 || loadfactor > 1) {
       throw new IllegalArgumentException("loadfactor must be larger than 0 "
-          + "and equal to or smaller than 1!");
+          + "and equal to or smaller than 1 but is " + loadfactor + "!");
+    }
 
     int arraySize = (int) (mapping.length / loadfactor) + 1;
 

Modified: opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/RealValueFileEventStream.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/RealValueFileEventStream.java?rev=1364059&r1=1364058&r2=1364059&view=diff
==============================================================================
--- opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/RealValueFileEventStream.java (original)
+++ opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/RealValueFileEventStream.java Sat Jul 21 10:18:24 2012
@@ -21,7 +21,6 @@ package opennlp.model;
 
 import java.io.File;
 import java.io.IOException;
-
 import opennlp.maxent.GIS;
 import opennlp.maxent.io.SuffixSensitiveGISModelWriter;
 
@@ -30,42 +29,41 @@ public class RealValueFileEventStream ex
   public RealValueFileEventStream(String fileName) throws IOException {
     super(fileName);
   }
-  
+
   public RealValueFileEventStream(File file) throws IOException {
     super(file);
   }
-  
+
   /**
-   * Parses the specified contexts and re-populates context array with features and returns the values
-   * for these features.
-   * If all values are unspecified, then null is returned.
+   * Parses the specified contexts and re-populates context array with features
+   * and returns the values for these features. If all values are unspecified,
+   * then null is returned.
+   * 
    * @param contexts The contexts with real values specified.
    * @return The value for each context or null if all values are unspecified.
    */
   public static float[] parseContexts(String[] contexts) {
     boolean hasRealValue = false;
-    float[] values = new float[contexts.length]; 
+    float[] values = new float[contexts.length];
     for (int ci = 0; ci < contexts.length; ci++) {
       int ei = contexts[ci].lastIndexOf("=");
-      if (ei > 0 && ei+1 < contexts[ci].length()) {
+      if (ei > 0 && ei + 1 < contexts[ci].length()) {
         boolean gotReal = true;
         try {
-          values[ci] = Float.parseFloat(contexts[ci].substring(ei+1));
-        }
-        catch (NumberFormatException e) {
+          values[ci] = Float.parseFloat(contexts[ci].substring(ei + 1));
+        } catch (NumberFormatException e) {
           gotReal = false;
-          System.err.println("Unable to determine value in context:"+contexts[ci]);
+          System.err.println("Unable to determine value in context:" + contexts[ci]);
           values[ci] = 1;
         }
         if (gotReal) {
           if (values[ci] < 0) {
-            throw new RuntimeException("Negitive values are not allowed: "+contexts[ci]);
+            throw new RuntimeException("Negative values are not allowed: " + contexts[ci]);
           }
-          contexts[ci] = contexts[ci].substring(0,ei);
+          contexts[ci] = contexts[ci].substring(0, ei);
           hasRealValue = true;
         }
-      }
-      else {
+      } else {
         values[ci] = 1;
       }
     }
@@ -74,18 +72,19 @@ public class RealValueFileEventStream ex
     }
     return values;
   }
-    
+
   public Event next() {
     int si = line.indexOf(' ');
-    String outcome = line.substring(0,si);
-    String[] contexts = line.substring(si+1).split(" ");
+    String outcome = line.substring(0, si);
+    String[] contexts = line.substring(si + 1).split(" ");
     float[] values = parseContexts(contexts);
     return (new Event(outcome, contexts, values));
-  }  
-  
+  }
+
   /**
    * Trains and writes a model based on the events in the specified event file.
    * the name of the model created is based on the event file name.
+   * 
    * @param args eventfile [iterations cuttoff]
    * @throws IOException when the eventfile can not be read or the model file can not be written.
    */
@@ -94,7 +93,7 @@ public class RealValueFileEventStream ex
       System.err.println("Usage: RealValueFileEventStream eventfile [iterations cutoff]");
       System.exit(1);
     }
-    int ai=0;
+    int ai = 0;
     String eventFile = args[ai++];
     EventStream es = new RealValueFileEventStream(eventFile);
     int iterations = 100;
@@ -103,7 +102,7 @@ public class RealValueFileEventStream ex
       iterations = Integer.parseInt(args[ai++]);
       cutoff = Integer.parseInt(args[ai++]);
     }
-    AbstractModel model = GIS.trainModel(iterations,new OnePassRealValueDataIndexer(es,cutoff));
-    new SuffixSensitiveGISModelWriter(model, new File(eventFile+".bin.gz")).persist();
+    AbstractModel model = GIS.trainModel(iterations, new OnePassRealValueDataIndexer(es, cutoff));
+    new SuffixSensitiveGISModelWriter(model, new File(eventFile + ".bin.gz")).persist();
   }
 }

Modified: opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/TrainUtil.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/TrainUtil.java?rev=1364059&r1=1364058&r2=1364059&view=diff
==============================================================================
--- opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/TrainUtil.java (original)
+++ opennlp/trunk/opennlp-maxent/src/main/java/opennlp/model/TrainUtil.java Sat Jul 21 10:18:24 2012
@@ -155,7 +155,7 @@ public class TrainUtil {
     else if (PERCEPTRON_VALUE.equals(algorithmName))
       sortAndMerge = false;
     else
-      throw new IllegalStateException("Unexpected algorihtm name: " + algorithmName);
+      throw new IllegalStateException("Unexpected algorithm name: " + algorithmName);
 
     HashSumEventStream hses = new HashSumEventStream(events);
     

Modified: opennlp/trunk/opennlp-maxent/src/main/java/opennlp/perceptron/PerceptronTrainer.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-maxent/src/main/java/opennlp/perceptron/PerceptronTrainer.java?rev=1364059&r1=1364058&r2=1364059&view=diff
==============================================================================
--- opennlp/trunk/opennlp-maxent/src/main/java/opennlp/perceptron/PerceptronTrainer.java (original)
+++ opennlp/trunk/opennlp-maxent/src/main/java/opennlp/perceptron/PerceptronTrainer.java Sat Jul 21 10:18:24 2012
@@ -85,8 +85,10 @@ public class PerceptronTrainer {
    */
   public void setTolerance(double tolerance) {
     
-    if (tolerance < 0)
-      throw new IllegalArgumentException("tolerance must be a positive number!");
+    if (tolerance < 0) {
+      throw new
+          IllegalArgumentException("tolerance must be a positive number but is " + tolerance + "!");
+    }
     
     this.tolerance = tolerance;
   }
@@ -99,8 +101,10 @@ public class PerceptronTrainer {
    */
   public void setStepSizeDecrease(double decrease) {
     
-    if (decrease < 0 || decrease > 100)
-      throw new IllegalArgumentException("decrease must be between 0 and 100");
+    if (decrease < 0 || decrease > 100) {
+      throw new
+          IllegalArgumentException("decrease must be between 0 and 100 but is " + decrease + "!");
+    }
     
     stepSizeDecrease = decrease;
   }