Skip to content

Commit

Permalink
Merge pull request ptnplanet#6 from yuripourre/master
Browse files Browse the repository at this point in the history
Change to maven project structure
  • Loading branch information
ptnplanet committed Mar 22, 2016
2 parents 696a7af + 16c1e0e commit 8790fc5
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Binary files
*.class

# Eclipse files
.classpath
.project
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.daslaboratorium.machinelearning.classifier;
package de.daslaboratorium.machinelearning.classifier.bayes;

import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;

import de.daslaboratorium.machinelearning.classifier.Classification;
import de.daslaboratorium.machinelearning.classifier.Classifier;

/**
* A concrete implementation of the abstract Classifier class. The Bayes
* classifier implements a naive Bayes approach to classifying a given set of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package de.daslaboratorium.machinelearning.classifier.bayes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import de.daslaboratorium.machinelearning.classifier.Classification;
import de.daslaboratorium.machinelearning.classifier.Classifier;

public class BayesClassifierTest {

private static final double EPSILON = 0.001;
private static final String CATEGORY_NEGATIVE = "negative";
private static final String CATEGORY_POSITIVE = "positive";
private Classifier<String, String> bayes;

@Before
public void setUp() {
/*
* 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.
*/
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(CATEGORY_POSITIVE, Arrays.asList(positiveText));

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

@Test
public void testStringClassification() {
final String[] unknownText1 = "today is a sunny day".split("\\s");
final String[] unknownText2 = "there will be rain".split("\\s");

Assert.assertEquals(CATEGORY_POSITIVE, bayes.classify(Arrays.asList(unknownText1)).getCategory());
Assert.assertEquals(CATEGORY_NEGATIVE, bayes.classify(Arrays.asList(unknownText2)).getCategory());
}

@Test
public void testStringClassificationInDetails() {

final String[] unknownText1 = "today is a sunny day".split("\\s");

Collection<Classification<String, String>> classifications = ((BayesClassifier<String, String>) bayes).classifyDetailed(
Arrays.asList(unknownText1));

List<Classification<String, String>> list = new ArrayList<Classification<String,String>>(classifications);

Assert.assertEquals(CATEGORY_NEGATIVE, list.get(0).getCategory());
Assert.assertEquals(0.0078125, list.get(0).getProbability(), EPSILON);

Assert.assertEquals(CATEGORY_POSITIVE, list.get(1).getCategory());
Assert.assertEquals(0.0234375, list.get(1).getProbability(), EPSILON);
}

}

0 comments on commit 8790fc5

Please sign in to comment.