public class BM25NBClassifier extends java.lang.Object implements Classifier<BytesRef>
| Modifier and Type | Field and Description |
|---|---|
private Analyzer |
analyzer
Analyzer to be used for tokenizing unseen input text |
private java.lang.String |
classFieldName
name of the field to be used as a class / category output
|
private IndexReader |
indexReader
IndexReader used to access the Classifier's
index |
private IndexSearcher |
indexSearcher
IndexSearcher to run searches on the index for retrieving frequencies |
private Query |
query
Query used to eventually filter the document set to be used to classify |
private java.lang.String[] |
textFieldNames
names of the fields to be used as input text
|
| Constructor and Description |
|---|
BM25NBClassifier(IndexReader indexReader,
Analyzer analyzer,
Query query,
java.lang.String classFieldName,
java.lang.String... textFieldNames)
Creates a new NaiveBayes classifier.
|
| Modifier and Type | Method and Description |
|---|---|
ClassificationResult<BytesRef> |
assignClass(java.lang.String inputDocument)
Assign a class (with score) to the given text String
|
private java.util.List<ClassificationResult<BytesRef>> |
assignClassNormalizedList(java.lang.String inputDocument)
Calculate probabilities for all classes for a given input text
|
private double |
calculateLogLikelihood(java.lang.String[] tokens,
Term term) |
private double |
calculateLogPrior(Term term) |
java.util.List<ClassificationResult<BytesRef>> |
getClasses(java.lang.String text)
Get all the classes (sorted by score, descending) assigned to the given text String.
|
java.util.List<ClassificationResult<BytesRef>> |
getClasses(java.lang.String text,
int max)
Get the first
max classes (sorted by score, descending) assigned to the given text String. |
private double |
getTermProbForClass(Term classTerm,
java.lang.String... words) |
private java.util.ArrayList<ClassificationResult<BytesRef>> |
normClassificationResults(java.util.List<ClassificationResult<BytesRef>> assignedClasses)
Normalize the classification results based on the max score available
|
private java.lang.String[] |
tokenize(java.lang.String text)
tokenize a
String on this classifier's text fields and analyzer |
private final IndexReader indexReader
IndexReader used to access the Classifier's
indexprivate final java.lang.String[] textFieldNames
private final java.lang.String classFieldName
private final IndexSearcher indexSearcher
IndexSearcher to run searches on the index for retrieving frequenciespublic BM25NBClassifier(IndexReader indexReader, Analyzer analyzer, Query query, java.lang.String classFieldName, java.lang.String... textFieldNames)
indexReader - the reader on the index to be used for classificationanalyzer - an Analyzer used to analyze unseen textquery - a Query to eventually filter the docs used for training the classifier, or null
if all the indexed docs should be usedclassFieldName - the name of the field used as the output for the classifier NOTE: must not be heavely analyzed
as the returned class will be a token indexed for this fieldtextFieldNames - the name of the fields used as the inputs for the classifier, NO boosting supported per fieldpublic ClassificationResult<BytesRef> assignClass(java.lang.String inputDocument) throws java.io.IOException
ClassifierassignClass in interface Classifier<BytesRef>inputDocument - a String containing text to be classifiedClassificationResult holding assigned class of type T and scorejava.io.IOException - If there is a low-level I/O error.public java.util.List<ClassificationResult<BytesRef>> getClasses(java.lang.String text) throws java.io.IOException
ClassifiergetClasses in interface Classifier<BytesRef>text - a String containing text to be classifiedClassificationResult, the classes and scores. Returns null if the classifier can't make lists.java.io.IOException - If there is a low-level I/O error.public java.util.List<ClassificationResult<BytesRef>> getClasses(java.lang.String text, int max) throws java.io.IOException
Classifiermax classes (sorted by score, descending) assigned to the given text String.getClasses in interface Classifier<BytesRef>text - a String containing text to be classifiedmax - the number of return list elementsClassificationResult, the classes and scores. Cut for "max" number of elements. Returns null if the classifier can't make lists.java.io.IOException - If there is a low-level I/O error.private java.util.List<ClassificationResult<BytesRef>> assignClassNormalizedList(java.lang.String inputDocument) throws java.io.IOException
inputDocument - the input text as a StringList of ClassificationResult, one for each existing classjava.io.IOException - if assigning probabilities failsprivate java.util.ArrayList<ClassificationResult<BytesRef>> normClassificationResults(java.util.List<ClassificationResult<BytesRef>> assignedClasses)
assignedClasses - the list of assigned classesprivate java.lang.String[] tokenize(java.lang.String text)
throws java.io.IOException
String on this classifier's text fields and analyzertext - the String representing an input text (to be classified)String array of the resulting tokensjava.io.IOException - if tokenization failsprivate double calculateLogLikelihood(java.lang.String[] tokens,
Term term)
throws java.io.IOException
java.io.IOExceptionprivate double getTermProbForClass(Term classTerm, java.lang.String... words) throws java.io.IOException
java.io.IOExceptionprivate double calculateLogPrior(Term term) throws java.io.IOException
java.io.IOException