You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2017/09/02 15:44:41 UTC

[48/50] lucene-solr:feature/autoscaling_72: LUCENE-7950 - fixed potential NPE when no docs have the class field

LUCENE-7950 - fixed potential NPE when no docs have the class field


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/c2c2e8a8
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/c2c2e8a8
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/c2c2e8a8

Branch: refs/heads/feature/autoscaling_72
Commit: c2c2e8a85e92024d627381858cd1dbcff4cbab72
Parents: cd471cc
Author: Tommaso Teofili <to...@apache.org>
Authored: Sat Sep 2 14:43:59 2017 +0200
Committer: Tommaso Teofili <to...@apache.org>
Committed: Sat Sep 2 14:43:59 2017 +0200

----------------------------------------------------------------------
 .../SimpleNaiveBayesDocumentClassifier.java     | 30 +++++++++++---------
 1 file changed, 16 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c2c2e8a8/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
----------------------------------------------------------------------
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
index 21ad7d1..6bc8573 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
@@ -113,24 +113,26 @@ public class SimpleNaiveBayesDocumentClassifier extends SimpleNaiveBayesClassifi
     Map<String, List<String[]>> fieldName2tokensArray = new LinkedHashMap<>();
     Map<String, Float> fieldName2boost = new LinkedHashMap<>();
     Terms classes = MultiFields.getTerms(indexReader, classFieldName);
-    TermsEnum classesEnum = classes.iterator();
-    BytesRef c;
+    if (classes != null) {
+      TermsEnum classesEnum = classes.iterator();
+      BytesRef c;
 
-    analyzeSeedDocument(inputDocument, fieldName2tokensArray, fieldName2boost);
+      analyzeSeedDocument(inputDocument, fieldName2tokensArray, fieldName2boost);
 
-    int docsWithClassSize = countDocsWithClass();
-    while ((c = classesEnum.next()) != null) {
-      double classScore = 0;
-      Term term = new Term(this.classFieldName, c);
-      for (String fieldName : textFieldNames) {
-        List<String[]> tokensArrays = fieldName2tokensArray.get(fieldName);
-        double fieldScore = 0;
-        for (String[] fieldTokensArray : tokensArrays) {
-          fieldScore += calculateLogPrior(term, docsWithClassSize) + calculateLogLikelihood(fieldTokensArray, fieldName, term, docsWithClassSize) * fieldName2boost.get(fieldName);
+      int docsWithClassSize = countDocsWithClass();
+      while ((c = classesEnum.next()) != null) {
+        double classScore = 0;
+        Term term = new Term(this.classFieldName, c);
+        for (String fieldName : textFieldNames) {
+          List<String[]> tokensArrays = fieldName2tokensArray.get(fieldName);
+          double fieldScore = 0;
+          for (String[] fieldTokensArray : tokensArrays) {
+            fieldScore += calculateLogPrior(term, docsWithClassSize) + calculateLogLikelihood(fieldTokensArray, fieldName, term, docsWithClassSize) * fieldName2boost.get(fieldName);
+          }
+          classScore += fieldScore;
         }
-        classScore += fieldScore;
+        assignedClasses.add(new ClassificationResult<>(term.bytes(), classScore));
       }
-      assignedClasses.add(new ClassificationResult<>(term.bytes(), classScore));
     }
     return normClassificationResults(assignedClasses);
   }