Skip to content

Commit

Permalink
Added runnable example
Browse files Browse the repository at this point in the history
  • Loading branch information
ptnplanet committed May 28, 2012
1 parent 548c66d commit 2cef25d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class Classifier<T, K> implements IFeatureProbability<T, K> {
/**
* The initial memory capacity or how many classifications are memorized.
*/
private int memoryCapacity = 200;
private int memoryCapacity = 1000;

/**
* A dictionary mapping features to their number of occurrences in each
Expand Down
77 changes: 77 additions & 0 deletions example/RunnableExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.daslaboratorium.machinelearning.bayes.example;

import java.util.Arrays;

import de.daslaboratorium.machinelearning.bayes.BayesClassifier;
import de.daslaboratorium.machinelearning.bayes.Classifier;

public class RunnableExample {

public static void main(String[] args) {

/*
* Create a new classifier instance. The context features are
* Strings and the context will be classified with a String according
* to the featureset of the context.
*/
final Classifier<String, String> bayes =
new BayesClassifier<String, String>();

/*
* The classifier can learn from classifications that are handed over
* to the learn methods. Imagin a tokenized text as follows. The tokens
* are the text's features. The category of the text will either be
* positive or negative.
*/
final String[] positiveText = "I love sunny days".split("\\s");
bayes.learn("positive", Arrays.asList(positiveText));

final String[] negativeText = "I hate rain".split("\\s");
bayes.learn("negative", Arrays.asList(negativeText));

/*
* Now that the classifier has "learned" two classifications, it will
* be able to classify similar sentences. The classify method returns
* a Classification Object, that contains the given featureset,
* classification probability and resulting category.
*/
final String[] unknownText1 = "today is a sunny day".split("\\s");
final String[] unknownText2 = "there will be rain".split("\\s");

System.out.println( // will output "positive"
bayes.classify(Arrays.asList(unknownText1)).getCategory());
System.out.println( // will output "negative"
bayes.classify(Arrays.asList(unknownText2)).getCategory());

/*
* The BayesClassifier extends the abstract Classifier and provides
* detailed classification results that can be retrieved by calling
* the classifyDetailed Method.
*
* The classification with the highest probability is the resulting
* classification. The returned List will look like this.
* [
* Classification [
* category=negative,
* probability=0.0078125,
* featureset=[today, is, a, sunny, day]
* ],
* Classification [
* category=positive,
* probability=0.0234375,
* featureset=[today, is, a, sunny, day]
* ]
* ]
*/
((BayesClassifier<String, String>) bayes).classifyDetailed(
Arrays.asList(unknownText1));

/*
* Please note, that this particular classifier implementation will
* "forget" learned classifications after a few learning sessions. The
* number of learning sessions it will record can be set as follows:
*/
bayes.setMemoryCapacity(500); // remember the last 500 learned classifications
}

}

0 comments on commit 2cef25d

Please sign in to comment.