Skip to content

Apache Lucene open-source search software

LUCENE-6433 - using generics in Classifier#getClasses

Tommaso Teofilidbf063d3d1ea
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java
index 3cf500bdb79..481392af979 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/BooleanPerceptronClassifier.java
@@ -173,21 +173,24 @@ public class BooleanPerceptronClassifier implements Classifier<Boolean> {
Integer.MAX_VALUE).scoreDocs) {
StoredDocument doc = indexSearcher.doc(scoreDoc.doc);
- // assign class to the doc
- ClassificationResult<Boolean> classificationResult = assignClass(doc
- .getField(textFieldName).stringValue());
- Boolean assignedClass = classificationResult.getAssignedClass();
+ StorableField textField = doc.getField(textFieldName);
// get the expected result
- StorableField field = doc.getField(classFieldName);
-
- Boolean correctClass = Boolean.valueOf(field.stringValue());
- long modifier = correctClass.compareTo(assignedClass);
- if (modifier != 0) {
- updateWeights(leafReader, scoreDoc.doc, assignedClass,
- weights, modifier, batchCount % batchSize == 0);
+ StorableField classField = doc.getField(classFieldName);
+
+ if (textField != null && classField != null) {
+ // assign class to the doc
+ ClassificationResult<Boolean> classificationResult = assignClass(textField.stringValue());
+ Boolean assignedClass = classificationResult.getAssignedClass();
+
+ Boolean correctClass = Boolean.valueOf(classField.stringValue());
+ long modifier = correctClass.compareTo(assignedClass);
+ if (modifier != 0) {
+ updateWeights(leafReader, scoreDoc.doc, assignedClass,
+ weights, modifier, batchCount % batchSize == 0);
+ }
+ batchCount++;
}
- batchCount++;
}
weights.clear(); // free memory while waiting for GC
}
@@ -246,18 +249,18 @@ public class BooleanPerceptronClassifier implements Classifier<Boolean> {
* {@inheritDoc}
*/
@Override
- public List<ClassificationResult<BytesRef>> getClasses(String text)
+ public List<ClassificationResult<Boolean>> getClasses(String text)
throws IOException {
- return null;
+ throw new RuntimeException("not implemented");
}
/**
* {@inheritDoc}
*/
@Override
- public List<ClassificationResult<BytesRef>> getClasses(String text, int max)
+ public List<ClassificationResult<Boolean>> getClasses(String text, int max)
throws IOException {
- return null;
+ throw new RuntimeException("not implemented");
}
}
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java b/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java
index 7591747e6ec..4166199dd9a 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java
@@ -48,7 +48,7 @@ public interface Classifier<T> {
* @return the whole list of {@link ClassificationResult}, the classes and scores. Returns <code>null</code> if the classifier can't make lists.
* @throws IOException If there is a low-level I/O error.
*/
- public List<ClassificationResult<BytesRef>> getClasses(String text) throws IOException;
+ public List<ClassificationResult<T>> getClasses(String text) throws IOException;
/**
* Get the first <code>max</code> classes (sorted by score, descending) assigned to the given text String.
@@ -58,7 +58,7 @@ public interface Classifier<T> {
* @return the whole list of {@link ClassificationResult}, the classes and scores. Cut for "max" number of elements. Returns <code>null</code> if the classifier can't make lists.
* @throws IOException If there is a low-level I/O error.
*/
- public List<ClassificationResult<BytesRef>> getClasses(String text, int max) throws IOException;
+ public List<ClassificationResult<T>> getClasses(String text, int max) throws IOException;
/**
* Train the classifier using the underlying Lucene index