package org.netevolved.analyzer.bayes; import java.util.Collection; import java.util.Comparator; import java.util.SortedSet; import java.util.TreeSet; /** * A concrete implementation of the abstract Classifier class. The Bayes * classifier implements a naive Bayes approach to classifying a given set of * features: classify(feat1,...,featN) = argmax(P(cat)*PROD(P(featI|cat) * * @author Philipp Nolte * * @see http://en.wikipedia.org/wiki/Naive_Bayes_classifier * * @param The feature class. * @param The category class. */ public class BayesClassifier extends BaseClassifier { protected LearningPreprocessor fieldPreProcessor; @Override public LearningPreprocessor getPreProcessor() { return fieldPreProcessor; } public void setPreProcessor(LearningPreprocessor inPreprocessor) { fieldPreProcessor = inPreprocessor; } /** * Calculates the product of all feature probabilities: PROD(P(featI|cat) * * @param features The set of features to use. * @param category The category to test for. * @return The product of all feature probabilities. */ private float featuresProbabilityProduct(Collection features, K category) { float product = 1.0f; for (T feature : features) product *= this.featureWeighedAverage(feature, category); return product; } /** * Calculates the probability that the features can be classified as the * category given. * * @param features The set of features to use. * @param category The category to test for. * @return The probability that the features can be classified as the * category. */ private float categoryProbability(Collection features, K category) { return ((float) this.categoryCount(category) / (float) this.getCategoriesTotal()) * featuresProbabilityProduct(features, category); } /** * Retrieves a sorted Set of probabilities that the given set * of features is classified as the available categories. * * @param features The set of features to use. * @return A sorted Set of category-probability-entries. */ private SortedSet> categoryProbabilities( Collection features) { /* * Sort the set according to the possibilities. Because we have to sort * by the mapped value and not by the mapped key, we can not use a * sorted tree (TreeMap) and we have to use a set-entry approach to * achieve the desired functionality. A custom comparator is therefore * needed. */ SortedSet> probabilities = new TreeSet>( new Comparator>() { @Override public int compare(Classification o1, Classification o2) { int toReturn = Float.compare( o1.getProbability(), o2.getProbability()); if ((toReturn == 0) && !o1.getCategory().equals(o2.getCategory())) toReturn = -1; return toReturn; } }); for (K category : this.getCategories()) probabilities.add(new Classification( features, category, this.categoryProbability(features, category))); return probabilities; } /** * Classifies the given set of features. * * @return The category the set of features is classified as. */ @Override public Classification classify(Collection features) { SortedSet> probabilites = this.categoryProbabilities(features); if (probabilites.size() > 0) { return probabilites.last(); } return null; } /** * Classifies the given set of features. and return the full details of the * classification. * * @return The set of categories the set of features is classified as. */ public Collection> classifyDetailed( Collection features) { return this.categoryProbabilities(features); } @Override public boolean isTrained() { return (getTrainingTally()!=0);//change this: should be considered trained if tally is above a certain point } }