1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 package net.sf.classifier4J.bayesian;
53
54 import net.sf.classifier4J.IClassifier;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58
59 import junit.framework.TestCase;
60 import junit.textui.TestRunner;
61
62 /***
63 * @author Nick Lothian
64 * @author Peter Leschev
65 */
66 public class WordProbabilityTest extends TestCase {
67
68 private Log log = LogFactory.getLog(this.getClass());
69
70 public WordProbabilityTest(String name) {
71 super(name);
72 }
73
74 public void testAccessors() {
75 WordProbability wp = null;
76
77 wp = new WordProbability("", 0.96d);
78 assertEquals("", wp.getWord());
79 try {
80 assertEquals(0, wp.getMatchingCount());
81 fail("Shouldn't be able to obtain matching count when we haven't set them");
82 } catch (UnsupportedOperationException e) {
83 assertTrue(true);
84 }
85 try {
86 assertEquals(0, wp.getNonMatchingCount());
87 fail("Shouldn't be able to obtain matching count when we haven't set them");
88 } catch (UnsupportedOperationException e) {
89 assertTrue(true);
90 }
91 assertEquals(0.96d, wp.getProbability(), 0);
92
93 wp = new WordProbability("aWord", 10, 30);
94 assertEquals("aWord", wp.getWord());
95 assertEquals(10, wp.getMatchingCount());
96 assertEquals(30, wp.getNonMatchingCount());
97 assertEquals(0.25d, wp.getProbability(), 0d);
98
99 try {
100 wp.setMatchingCount(-10);
101 fail("Shouldn't be able to set -ve matchingCount");
102 }
103 catch(IllegalArgumentException e) {
104 assertTrue(true);
105 }
106
107 try {
108 wp.setNonMatchingCount(-10);
109 fail("Shouldn't be able to set -ve nonMatchingCount");
110 }
111 catch(IllegalArgumentException e) {
112 assertTrue(true);
113 }
114 }
115
116 public void testCalculateProbability() {
117
118 WordProbability wp = null;
119
120 wp = new WordProbability("", 10, 10);
121 assertEquals(IClassifier.NEUTRAL_PROBABILITY, wp.getProbability(), 0);
122
123 wp = new WordProbability("", 20, 10);
124 assertEquals(0.66, wp.getProbability(), 0.01);
125
126 wp = new WordProbability("", 30, 10);
127 assertEquals(0.75, wp.getProbability(), 0);
128
129 wp = new WordProbability("", 10, 20);
130 assertEquals(0.33, wp.getProbability(), 0.01);
131
132 wp = new WordProbability("", 10, 30);
133 assertEquals(0.25, wp.getProbability(), 0);
134
135 wp = new WordProbability("", 10, 0);
136 assertEquals(IClassifier.UPPER_BOUND, wp.getProbability(), 0);
137
138 wp = new WordProbability("", 100, 1);
139 assertEquals(IClassifier.UPPER_BOUND, wp.getProbability(), 0);
140
141 wp = new WordProbability("", 1000, 1);
142 assertEquals(IClassifier.UPPER_BOUND, wp.getProbability(), 0);
143
144 wp = new WordProbability("", 0, 10);
145 assertEquals(IClassifier.LOWER_BOUND, wp.getProbability(), 0);
146
147 wp = new WordProbability("", 1, 100);
148 assertEquals(IClassifier.LOWER_BOUND, wp.getProbability(), 0);
149
150 wp = new WordProbability("", 1, 1000);
151 assertEquals(IClassifier.LOWER_BOUND, wp.getProbability(), 0);
152 }
153
154 public void testComparator() {
155
156 String method = "testComparator() ";
157
158 WordProbability wp = null;
159 WordProbability wp2 = null;
160
161 wp = new WordProbability("a", 0, 0);
162 wp2 = new WordProbability("b", 0, 0);
163
164 try {
165 wp.compareTo(new Object());
166 fail("Shouldn't be able to compareTo objects other than WordProbability");
167 } catch (ClassCastException e) {
168 assertTrue(true);
169 }
170
171 if (log.isDebugEnabled()) {
172 log.debug(method + "wp.getProbability() " + wp.getProbability());
173 log.debug(method + "wp2.getProbability() " + wp2.getProbability());
174 }
175
176 assertTrue(wp.compareTo(wp2) < 0);
177 assertTrue(wp2.compareTo(wp) > 0);
178 }
179
180 public void testMatchingAndNonMatchingCountRollover() {
181
182 WordProbability wp = new WordProbability("aWord", Long.MAX_VALUE, Long.MAX_VALUE);
183 try {
184 wp.registerMatch();
185 fail("Should detect rollover");
186 }
187 catch(UnsupportedOperationException e) {
188 assertTrue(true);
189 }
190 try {
191 wp.registerNonMatch();
192 fail("Should detect rollover");
193 }
194 catch(UnsupportedOperationException e) {
195 assertTrue(true);
196 }
197 }
198
199 public static void main(String[] args) throws Exception {
200 TestRunner.run(WordProbabilityTest.class);
201 }
202 }