Apache Lucene open-source search software
[LUCENE-4651] - added generics to Classifier and ClassificationResult
Tommaso Teofili325c3678ea1f
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/ClassificationResult.java b/lucene/classification/src/java/org/apache/lucene/classification/ClassificationResult.javaindex 49e1eebd874..9d60e4c3f87 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/ClassificationResult.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/ClassificationResult.java@@ -17,29 +17,29 @@package org.apache.lucene.classification;/**- * The result of a call to {@link Classifier#assignClass(String)} holding an assigned class and a score.+ * The result of a call to {@link Classifier#assignClass(String)} holding an assigned class of type <code>T</code> and a score.* @lucene.experimental*/-public class ClassificationResult {+public class ClassificationResult<T> {- private String assignedClass;+ private T assignedClass;private double score;/*** Constructor- * @param assignedClass the class <code>String</code> assigned by a {@link Classifier}+ * @param assignedClass the class <code>T</code> assigned by a {@link Classifier}* @param score the score for the assignedClass as a <code>double</code>*/- public ClassificationResult(String assignedClass, double score) {+ public ClassificationResult(T assignedClass, double score) {this.assignedClass = assignedClass;this.score = score;}/*** retrieve the result class- * @return a <code>String</code> representing an assigned class+ * @return a <code>T</code> representing an assigned class*/- public String getAssignedClass() {+ public T getAssignedClass() {return assignedClass;}diff --git a/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java b/lucene/classification/src/java/org/apache/lucene/classification/Classifier.javaindex 38d0486cc93..4d0fe2e2b4d 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/Classifier.java@@ -22,18 +22,19 @@ import org.apache.lucene.index.AtomicReader;import java.io.IOException;/**- * A classifier, see <code>http://en.wikipedia.org/wiki/Classifier_(mathematics)</code>+ * A classifier, see <code>http://en.wikipedia.org/wiki/Classifier_(mathematics)</code>, which assign classes of type+ * <code>T</code>* @lucene.experimental*/-public interface Classifier {+public interface Classifier<T> {/*** Assign a class (with score) to the given text String* @param text a String containing text to be classified- * @return a {@link ClassificationResult} holding assigned class and score+ * @return a {@link ClassificationResult} holding assigned class of type <code>T</code> and score* @throws IOException If there is a low-level I/O error.*/- public ClassificationResult assignClass(String text) throws IOException;+ public ClassificationResult<T> assignClass(String text) throws IOException;/*** Train the classifier using the underlying Lucene indexdiff --git a/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.javaindex 2a24322558b..9d3625d5385 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java@@ -23,6 +23,7 @@ import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;+import org.apache.lucene.util.BytesRef;import java.io.IOException;import java.io.StringReader;@@ -35,7 +36,7 @@ import java.util.Map;** @lucene.experimental*/-public class KNearestNeighborClassifier implements Classifier {+public class KNearestNeighborClassifier implements Classifier<BytesRef> {private MoreLikeThis mlt;private String textFieldName;@@ -56,35 +57,37 @@ public class KNearestNeighborClassifier implements Classifier {* {@inheritDoc}*/@Override- public ClassificationResult assignClass(String text) throws IOException {+ public ClassificationResult<BytesRef> assignClass(String text) throws IOException {Query q = mlt.like(new StringReader(text), textFieldName);TopDocs topDocs = indexSearcher.search(q, k);return selectClassFromNeighbors(topDocs);}- private ClassificationResult selectClassFromNeighbors(TopDocs topDocs) throws IOException {+ private ClassificationResult<BytesRef> selectClassFromNeighbors(TopDocs topDocs) throws IOException {// TODO : improve the nearest neighbor selection- Map<String, Integer> classCounts = new HashMap<String, Integer>();+ Map<BytesRef, Integer> classCounts = new HashMap<BytesRef, Integer>();for (ScoreDoc scoreDoc : topDocs.scoreDocs) {- String cl = indexSearcher.doc(scoreDoc.doc).getField(classFieldName).stringValue();- Integer count = classCounts.get(cl);- if (count != null) {- classCounts.put(cl, count + 1);- } else {- classCounts.put(cl, 1);+ BytesRef cl = new BytesRef(indexSearcher.doc(scoreDoc.doc).getField(classFieldName).stringValue());+ if (cl != null) {+ Integer count = classCounts.get(cl);+ if (count != null) {+ classCounts.put(cl, count + 1);+ } else {+ classCounts.put(cl, 1);+ }}}double max = 0;- String assignedClass = null;- for (String cl : classCounts.keySet()) {+ BytesRef assignedClass = new BytesRef();+ for (BytesRef cl : classCounts.keySet()) {Integer count = classCounts.get(cl);if (count > max) {max = count;- assignedClass = cl;+ assignedClass = cl.clone();}}double score = max / (double) k;- return new ClassificationResult(assignedClass, score);+ return new ClassificationResult<BytesRef>(assignedClass, score);}/**diff --git a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.javaindex 8e1973ad872..17fa9d87a0f 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java@@ -41,7 +41,7 @@ import java.util.LinkedList;** @lucene.experimental*/-public class SimpleNaiveBayesClassifier implements Classifier {+public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {private AtomicReader atomicReader;private String textFieldName;@@ -89,12 +89,12 @@ public class SimpleNaiveBayesClassifier implements Classifier {* {@inheritDoc}*/@Override- public ClassificationResult assignClass(String inputDocument) throws IOException {+ public ClassificationResult<BytesRef> assignClass(String inputDocument) throws IOException {if (atomicReader == null) {throw new RuntimeException("need to train the classifier first");}double max = 0d;- String foundClass = null;+ BytesRef foundClass = new BytesRef();Terms terms = MultiFields.getTerms(atomicReader, classFieldName);TermsEnum termsEnum = terms.iterator(null);@@ -105,10 +105,10 @@ public class SimpleNaiveBayesClassifier implements Classifier {double clVal = calculatePrior(next) * calculateLikelihood(tokenizedDoc, next);if (clVal > max) {max = clVal;- foundClass = next.utf8ToString();+ foundClass = next.clone();}}- return new ClassificationResult(foundClass, max);+ return new ClassificationResult<BytesRef>(foundClass, max);}diff --git a/lucene/classification/src/test/org/apache/lucene/classification/ClassificationTestBase.java b/lucene/classification/src/test/org/apache/lucene/classification/ClassificationTestBase.javaindex 968eaff7421..8d7e1f332bf 100644--- a/lucene/classification/src/test/org/apache/lucene/classification/ClassificationTestBase.java+++ b/lucene/classification/src/test/org/apache/lucene/classification/ClassificationTestBase.java@@ -24,6 +24,7 @@ import org.apache.lucene.document.TextField;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.SlowCompositeReaderWrapper;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.LuceneTestCase;import org.junit.After;import org.junit.Before;@@ -56,15 +57,17 @@ public abstract class ClassificationTestBase extends LuceneTestCase {dir.close();}- protected void checkCorrectClassification(Classifier classifier, Analyzer analyzer) throws Exception {++ protected void checkCorrectClassification(Classifier<BytesRef> classifier, Analyzer analyzer) throws Exception {SlowCompositeReaderWrapper compositeReaderWrapper = null;try {populateIndex(analyzer);compositeReaderWrapper = new SlowCompositeReaderWrapper(indexWriter.getReader());classifier.train(compositeReaderWrapper, textFieldName, classFieldName, analyzer);String newText = "Much is made of what the likes of Facebook, Google and Apple know about users. Truth is, Amazon may know more.";- ClassificationResult classificationResult = classifier.assignClass(newText);- assertEquals("technology", classificationResult.getAssignedClass());+ ClassificationResult<BytesRef> classificationResult = classifier.assignClass(newText);+ assertNotNull(classificationResult.getAssignedClass());+ assertEquals(new BytesRef("technology"), classificationResult.getAssignedClass());assertTrue(classificationResult.getScore() > 0);} finally {if (compositeReaderWrapper != null)