Apache Lucene open-source search software
[LUCENE-4345] - fixed wrong integer division for score now using double, plus minor fixes to javadoc and FieldType definition in tests
Tommaso Teofiliad028348829c
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.javaindex 7cc5a8dd5f4..2a24322558b 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java@@ -52,14 +52,20 @@ public class KNearestNeighborClassifier implements Classifier {this.k = k;}+ /**+ * {@inheritDoc}+ */@Overridepublic ClassificationResult assignClass(String text) throws IOException {Query q = mlt.like(new StringReader(text), textFieldName);- TopDocs docs = indexSearcher.search(q, k);+ TopDocs topDocs = indexSearcher.search(q, k);+ return selectClassFromNeighbors(topDocs);+ }+ private ClassificationResult selectClassFromNeighbors(TopDocs topDocs) throws IOException {// TODO : improve the nearest neighbor selectionMap<String, Integer> classCounts = new HashMap<String, Integer>();- for (ScoreDoc scoreDoc : docs.scoreDocs) {+ for (ScoreDoc scoreDoc : topDocs.scoreDocs) {String cl = indexSearcher.doc(scoreDoc.doc).getField(classFieldName).stringValue();Integer count = classCounts.get(cl);if (count != null) {@@ -68,7 +74,7 @@ public class KNearestNeighborClassifier implements Classifier {classCounts.put(cl, 1);}}- int max = 0;+ double max = 0;String assignedClass = null;for (String cl : classCounts.keySet()) {Integer count = classCounts.get(cl);@@ -77,10 +83,13 @@ public class KNearestNeighborClassifier implements Classifier {assignedClass = cl;}}- double score = max / k;+ double score = max / (double) k;return new ClassificationResult(assignedClass, score);}+ /**+ * {@inheritDoc}+ */@Overridepublic void train(AtomicReader atomicReader, String textFieldName, String classFieldName, Analyzer analyzer) throws IOException {this.textFieldName = textFieldName;diff --git a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.javaindex ce55826b552..c99399bba8e 100644--- a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java+++ b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java@@ -38,6 +38,7 @@ import java.util.LinkedList;/*** A simplistic Lucene based NaiveBayes classifier, see <code>http://en.wikipedia.org/wiki/Naive_Bayes_classifier</code>+ ** @lucene.experimental*/public class SimpleNaiveBayesClassifier implements Classifier {@@ -48,14 +49,19 @@ public class SimpleNaiveBayesClassifier implements Classifier {private int docsWithClassSize;private Analyzer analyzer;private IndexSearcher indexSearcher;-- /**++ /*** Creates a new NaiveBayes classifier.* Note that you must call {@link #train(AtomicReader, String, String, Analyzer) train()} before you can* classify any documents.*/- public SimpleNaiveBayesClassifier() {}+ public SimpleNaiveBayesClassifier() {+ }+ /**+ * {@inheritDoc}+ */+ @Overridepublic void train(AtomicReader atomicReader, String textFieldName, String classFieldName, Analyzer analyzer)throws IOException {this.atomicReader = atomicReader;@@ -79,6 +85,10 @@ public class SimpleNaiveBayesClassifier implements Classifier {return result.toArray(new String[result.size()]);}+ /**+ * {@inheritDoc}+ */+ @Overridepublic ClassificationResult assignClass(String inputDocument) throws IOException {if (atomicReader == null) {throw new RuntimeException("need to train the classifier first");@@ -89,7 +99,7 @@ public class SimpleNaiveBayesClassifier implements Classifier {Terms terms = MultiFields.getTerms(atomicReader, classFieldName);TermsEnum termsEnum = terms.iterator(null);BytesRef next;- while((next = termsEnum.next()) != null) {+ while ((next = termsEnum.next()) != null) {// TODO : turn it to be in log scaledouble clVal = calculatePrior(next) * calculateLikelihood(inputDocument, next);if (clVal > 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 a9925841351..5352e210be4 100644--- a/lucene/classification/src/test/org/apache/lucene/classification/ClassificationTestBase.java+++ b/lucene/classification/src/test/org/apache/lucene/classification/ClassificationTestBase.java@@ -19,6 +19,7 @@ package org.apache.lucene.classification;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;+import org.apache.lucene.document.FieldType;import org.apache.lucene.document.TextField;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.SlowCompositeReaderWrapper;@@ -71,52 +72,57 @@ public abstract class ClassificationTestBase extends LuceneTestCase {private void populateIndex(Analyzer analyzer) throws Exception {+ FieldType ft = new FieldType(TextField.TYPE_STORED);+ ft.setStoreTermVectors(true);+ ft.setStoreTermVectorOffsets(true);+ ft.setStoreTermVectorPositions(true);+Document doc = new Document();- doc.add(new TextField(textFieldName, "The traveling press secretary for Mitt Romney lost his cool and cursed at reporters " ++ doc.add(new Field(textFieldName, "The traveling press secretary for Mitt Romney lost his cool and cursed at reporters " +"who attempted to ask questions of the Republican presidential candidate in a public plaza near the Tomb of " +- "the Unknown Soldier in Warsaw Tuesday.", Field.Store.YES));- doc.add(new TextField(classFieldName, "politics", Field.Store.YES));+ "the Unknown Soldier in Warsaw Tuesday.", ft));+ doc.add(new Field(classFieldName, "politics", ft));indexWriter.addDocument(doc, analyzer);doc = new Document();- doc.add(new TextField(textFieldName, "Mitt Romney seeks to assure Israel and Iran, as well as Jewish voters in the United" +- " States, that he will be tougher against Iran's nuclear ambitions than President Barack Obama.", Field.Store.YES));- doc.add(new TextField(classFieldName, "politics", Field.Store.YES));+ doc.add(new Field(textFieldName, "Mitt Romney seeks to assure Israel and Iran, as well as Jewish voters in the United" ++ " States, that he will be tougher against Iran's nuclear ambitions than President Barack Obama.", ft));+ doc.add(new Field(classFieldName, "politics", ft));indexWriter.addDocument(doc, analyzer);doc = new Document();- doc.add(new TextField(textFieldName, "And there's a threshold question that he has to answer for the American people and " ++ doc.add(new Field(textFieldName, "And there's a threshold question that he has to answer for the American people and " +"that's whether he is prepared to be commander-in-chief,\" she continued. \"As we look to the past events, we " +- "know that this raises some questions about his preparedness and we'll see how the rest of his trip goes.\"", Field.Store.YES));- doc.add(new TextField(classFieldName, "politics", Field.Store.YES));+ "know that this raises some questions about his preparedness and we'll see how the rest of his trip goes.\"", ft));+ doc.add(new Field(classFieldName, "politics", ft));indexWriter.addDocument(doc, analyzer);doc = new Document();- doc.add(new TextField(textFieldName, "Still, when it comes to gun policy, many congressional Democrats have \"decided to " ++ doc.add(new Field(textFieldName, "Still, when it comes to gun policy, many congressional Democrats have \"decided to " +"keep quiet and not go there,\" said Alan Lizotte, dean and professor at the State University of New York at " +- "Albany's School of Criminal Justice.", Field.Store.YES));- doc.add(new TextField(classFieldName, "politics", Field.Store.YES));+ "Albany's School of Criminal Justice.", ft));+ doc.add(new Field(classFieldName, "politics", ft));indexWriter.addDocument(doc, analyzer);doc = new Document();- doc.add(new TextField(textFieldName, "Standing amongst the thousands of people at the state Capitol, Jorstad, director of " ++ doc.add(new Field(textFieldName, "Standing amongst the thousands of people at the state Capitol, Jorstad, director of " +"technology at the University of Wisconsin-La Crosse, documented the historic moment and shared it with the " +- "world through the Internet.", Field.Store.YES));- doc.add(new TextField(classFieldName, "technology", Field.Store.YES));+ "world through the Internet.", ft));+ doc.add(new Field(classFieldName, "technology", ft));indexWriter.addDocument(doc, analyzer);doc = new Document();- doc.add(new TextField(textFieldName, "So, about all those experts and analysts who've spent the past year or so saying " +- "Facebook was going to make a phone. A new expert has stepped forward to say it's not going to happen.", Field.Store.YES));- doc.add(new TextField(classFieldName, "technology", Field.Store.YES));+ doc.add(new Field(textFieldName, "So, about all those experts and analysts who've spent the past year or so saying " ++ "Facebook was going to make a phone. A new expert has stepped forward to say it's not going to happen.", ft));+ doc.add(new Field(classFieldName, "technology", ft));indexWriter.addDocument(doc, analyzer);doc = new Document();- doc.add(new TextField(textFieldName, "More than 400 million people trust Google with their e-mail, and 50 million store files" ++ doc.add(new Field(textFieldName, "More than 400 million people trust Google with their e-mail, and 50 million store files" +" in the cloud using the Dropbox service. People manage their bank accounts, pay bills, trade stocks and " +- "generally transfer or store huge volumes of personal data online.", Field.Store.YES));- doc.add(new TextField(classFieldName, "technology", Field.Store.YES));+ "generally transfer or store huge volumes of personal data online.", ft));+ doc.add(new Field(classFieldName, "technology", ft));indexWriter.addDocument(doc, analyzer);indexWriter.commit();