|
| 1 | +package de.daslaboratorium.machinelearning.classifier.bayes; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collection; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import de.daslaboratorium.machinelearning.classifier.Classification; |
| 13 | +import de.daslaboratorium.machinelearning.classifier.Classifier; |
| 14 | + |
| 15 | +public class BayesClassifierTest { |
| 16 | + |
| 17 | + private static final double EPSILON = 0.001; |
| 18 | + private static final String CATEGORY_NEGATIVE = "negative"; |
| 19 | + private static final String CATEGORY_POSITIVE = "positive"; |
| 20 | + private Classifier<String, String> bayes; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() { |
| 24 | + /* |
| 25 | + * Create a new classifier instance. The context features are |
| 26 | + * Strings and the context will be classified with a String according |
| 27 | + * to the featureset of the context. |
| 28 | + */ |
| 29 | + bayes = new BayesClassifier<String, String>(); |
| 30 | + |
| 31 | + /* |
| 32 | + * The classifier can learn from classifications that are handed over |
| 33 | + * to the learn methods. Imagin a tokenized text as follows. The tokens |
| 34 | + * are the text's features. The category of the text will either be |
| 35 | + * positive or negative. |
| 36 | + */ |
| 37 | + final String[] positiveText = "I love sunny days".split("\\s"); |
| 38 | + bayes.learn(CATEGORY_POSITIVE, Arrays.asList(positiveText)); |
| 39 | + |
| 40 | + final String[] negativeText = "I hate rain".split("\\s"); |
| 41 | + bayes.learn(CATEGORY_NEGATIVE, Arrays.asList(negativeText)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testStringClassification() { |
| 46 | + final String[] unknownText1 = "today is a sunny day".split("\\s"); |
| 47 | + final String[] unknownText2 = "there will be rain".split("\\s"); |
| 48 | + |
| 49 | + Assert.assertEquals(CATEGORY_POSITIVE, bayes.classify(Arrays.asList(unknownText1)).getCategory()); |
| 50 | + Assert.assertEquals(CATEGORY_NEGATIVE, bayes.classify(Arrays.asList(unknownText2)).getCategory()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testStringClassificationInDetails() { |
| 55 | + |
| 56 | + final String[] unknownText1 = "today is a sunny day".split("\\s"); |
| 57 | + |
| 58 | + Collection<Classification<String, String>> classifications = ((BayesClassifier<String, String>) bayes).classifyDetailed( |
| 59 | + Arrays.asList(unknownText1)); |
| 60 | + |
| 61 | + List<Classification<String, String>> list = new ArrayList<Classification<String,String>>(classifications); |
| 62 | + |
| 63 | + Assert.assertEquals(CATEGORY_NEGATIVE, list.get(0).getCategory()); |
| 64 | + Assert.assertEquals(0.0078125, list.get(0).getProbability(), EPSILON); |
| 65 | + |
| 66 | + Assert.assertEquals(CATEGORY_POSITIVE, list.get(1).getCategory()); |
| 67 | + Assert.assertEquals(0.0234375, list.get(1).getProbability(), EPSILON); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments