Author: tommaso
Date: Mon Mar 14 07:51:01 2016
New Revision: 1734886

URL: http://svn.apache.org/viewvc?rev=1734886&view=rev
Log:
OPENNLP-837 - applied patch from Jeff Zemerick to throw an exception when train 
data is not sufficient

Added:
    
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
   (with props)
Modified:
    
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
    
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java

Modified: 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java?rev=1734886&r1=1734885&r2=1734886&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
 Mon Mar 14 07:51:01 2016
@@ -24,6 +24,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import opennlp.tools.util.InsufficientTrainingDataException;
+
 
 /**
  * Abstract class for collecting event and context counts used in training.
@@ -78,15 +80,16 @@ public abstract class AbstractDataIndexe
    *
    * @param eventsToCompare a <code>ComparableEvent[]</code> value
    * @return The number of unique events in the specified list.
+   * @throws InsufficientTrainingDataException if not enough events are 
provided
    * @since maxent 1.2.6
    */
-  protected int sortAndMerge(List<ComparableEvent> eventsToCompare, boolean 
sort) {
+  protected int sortAndMerge(List<ComparableEvent> eventsToCompare, boolean 
sort) throws InsufficientTrainingDataException {
     int numUniqueEvents = 1;
     numEvents = eventsToCompare.size();
     if (sort && eventsToCompare.size() > 0) {
 
       Collections.sort(eventsToCompare);
-
+      
       ComparableEvent ce = eventsToCompare.get(0);
       for (int i = 1; i < numEvents; i++) {
         ComparableEvent ce2 = eventsToCompare.get(i);
@@ -100,10 +103,16 @@ public abstract class AbstractDataIndexe
           numUniqueEvents++; // increment the # of unique events
         }
       }
+
     }
     else {
       numUniqueEvents = eventsToCompare.size();
     }
+    
+    if(numUniqueEvents == 0) {
+      throw new InsufficientTrainingDataException("Insufficient training data 
to create model.");
+    }
+    
     if (sort) System.out.println("done. Reduced " + numEvents + " events to " 
+ numUniqueEvents + ".");
 
     contexts = new int[numUniqueEvents][];

Modified: 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java?rev=1734886&r1=1734885&r2=1734886&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
 Mon Mar 14 07:51:01 2016
@@ -27,6 +27,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
+import opennlp.tools.util.InsufficientTrainingDataException;
 import opennlp.tools.util.ObjectStream;
 
 /**
@@ -57,7 +58,7 @@ public class OnePassRealValueDataIndexer
     return values;
   }
 
-  protected int sortAndMerge(List<ComparableEvent> eventsToCompare,boolean 
sort) {
+  protected int sortAndMerge(List<ComparableEvent> eventsToCompare,boolean 
sort) throws InsufficientTrainingDataException {
     int numUniqueEvents = super.sortAndMerge(eventsToCompare,sort);
     values = new float[numUniqueEvents][];
     int numEvents = eventsToCompare.size();
@@ -71,7 +72,7 @@ public class OnePassRealValueDataIndexer
     return numUniqueEvents;
   }
 
-  protected List index(LinkedList<Event> events, Map<String,Integer> 
predicateIndex) {
+  protected List<ComparableEvent> index(LinkedList<Event> events, 
Map<String,Integer> predicateIndex) {
     Map<String,Integer> omap = new HashMap<String,Integer>();
 
     int numEvents = events.size();

Added: 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java?rev=1734886&view=auto
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
 (added)
+++ 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
 Mon Mar 14 07:51:01 2016
@@ -0,0 +1,47 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+
+/**
+ * This exception indicates that the provided training data is
+ * insufficient to train the desired model.
+ */
+public class InsufficientTrainingDataException extends IOException {
+
+  private static final long serialVersionUID = 0;
+
+  public InsufficientTrainingDataException() {
+  }
+
+  public InsufficientTrainingDataException(String message) {
+    super(message);
+  }
+
+  public InsufficientTrainingDataException(Throwable t) {
+    super();
+    initCause(t);
+  }
+
+  public InsufficientTrainingDataException(String message, Throwable t) {
+    super(message);
+    initCause(t);
+  }
+}

Propchange: 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to