1   
2   package net.sf.classifier4J.vector;
3   
4   import net.sf.classifier4J.ClassifierException;
5   import junit.framework.TestCase;
6   
7   
8   public class VectorClassifierTest extends TestCase {
9   
10      String sentence1 = "hello there is this a long sentence yes it is blah. blah hello";
11      
12      /*
13       * Class under test for double classify(String, String)
14       */
15      public void testClassifyStringString() {
16          TermVectorStorage storage = new HashMapTermVectorStorage();
17          VectorClassifier vc = new VectorClassifier(storage);
18          try {
19              String category = "test";
20              vc.teachMatch(category, sentence1);
21              assertEquals(0.852d, vc.classify(category, "hello blah"), 0.001);
22              assertEquals(0.301d, vc.classify(category, "sentence"), 0.001);
23              assertEquals(0.0d, vc.classify(category, "bye"), 0.001);
24              
25              assertEquals(0.0d, vc.classify("does not exist", "bye"), 0.001);
26          } catch (ClassifierException e) {
27              e.printStackTrace();
28              fail(e.getLocalizedMessage());
29          }            
30      }
31  
32      /*
33       * Class under test for boolean isMatch(String, String)
34       */
35      public void testIsMatchStringString() {
36          TermVectorStorage storage = new HashMapTermVectorStorage();
37          VectorClassifier vc = new VectorClassifier(storage);
38          try {
39              String category = "test";
40              vc.teachMatch(category, sentence1);
41              assertTrue(vc.isMatch(category, "hello blah"));
42              assertFalse(vc.isMatch(category, "sentence"));
43              assertFalse(vc.isMatch(category, "bye"));
44          } catch (ClassifierException e) {
45              e.printStackTrace();
46              fail(e.getLocalizedMessage());
47          }            
48      }
49  
50      /*
51       * Class under test for void teachMatch(String, String)
52       */
53      public void testTeachMatchStringString() {
54          TermVectorStorage storage = new HashMapTermVectorStorage();
55          VectorClassifier vc = new VectorClassifier(storage);
56          try {
57              String category = "test";
58              vc.teachMatch(category, sentence1);
59              TermVector tv = storage.getTermVector(category);
60              assertNotNull("TermVector should not be null", tv);
61              assertEquals(5, tv.getTerms().length);
62              assertEquals(tv.getValues().length, tv.getTerms().length);
63              assertEquals("blah", tv.getTerms()[0]);
64              assertEquals(2, tv.getValues()[0]);
65              assertEquals("hello", tv.getTerms()[1]);
66              assertEquals(2, tv.getValues()[1]);
67              assertEquals("long", tv.getTerms()[2]);
68              assertEquals(1, tv.getValues()[2]);
69              assertEquals("sentence", tv.getTerms()[3]);
70              assertEquals(1, tv.getValues()[3]);
71              assertEquals("yes", tv.getTerms()[4]);
72              assertEquals(1, tv.getValues()[4]);
73                          
74              assertEquals("{[blah, 2] [hello, 2] [long, 1] [sentence, 1] [yes, 1] }", tv.toString());
75          } catch (ClassifierException e) {
76              e.printStackTrace();
77              fail(e.getLocalizedMessage());
78          }
79      }
80      
81  
82  }