From 010c5f8d98da986408b84c02add27deaedb3f033 Mon Sep 17 00:00:00 2001 From: Tommaso Teofili Date: Tue, 30 Oct 2012 17:41:32 +0000 Subject: [PATCH] [LUCENE-4345] - adding kNN score as count / k for the most frequent class in results git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1403798 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/classification/KNearestNeighborClassifier.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java index 5680ccfaafe..faa94a26801 100644 --- a/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java +++ b/lucene/classification/src/java/org/apache/lucene/classification/KNearestNeighborClassifier.java @@ -32,6 +32,7 @@ import java.util.Map; /** * A k-Nearest Neighbor classifier (see http://en.wikipedia.org/wiki/K-nearest_neighbors) based * on {@link MoreLikeThis} + * * @lucene.experimental */ public class KNearestNeighborClassifier implements Classifier { @@ -44,6 +45,7 @@ public class KNearestNeighborClassifier implements Classifier { /** * Create a {@link Classifier} using kNN algorithm + * * @param k the number of neighbors to analyze as an int */ public KNearestNeighborClassifier(int k) { @@ -62,8 +64,7 @@ public class KNearestNeighborClassifier implements Classifier { Integer count = classCounts.get(cl); if (count != null) { classCounts.put(cl, count + 1); - } - else { + } else { classCounts.put(cl, 1); } } @@ -76,7 +77,7 @@ public class KNearestNeighborClassifier implements Classifier { assignedClass = cl; } } - double score = 1; // TODO : derive score from query + double score = classCounts.get(assignedClass) / k; return new ClassificationResult(assignedClass, score); }