OPENNLP-837 - applied patch from Jeff Zemerick to throw an exception when train data is not sufficient

git-svn-id: https://svn.apache.org/repos/asf/opennlp/trunk@1734886 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
index c1726f6..8abcecc 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
@@ -24,6 +24,8 @@
 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 @@
    *
    * @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 @@
           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][];
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
index b127c15..438b67a 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
@@ -27,6 +27,7 @@
 import java.util.List;
 import java.util.Map;
 
+import opennlp.tools.util.InsufficientTrainingDataException;
 import opennlp.tools.util.ObjectStream;
 
 /**
@@ -57,7 +58,7 @@
     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 @@
     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();
diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java b/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
new file mode 100644
index 0000000..f4b7080
--- /dev/null
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/InsufficientTrainingDataException.java
@@ -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);
+  }
+}