1   /*
2    * ====================================================================
3    * 
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2003 Nick Lothian. All rights reserved.
7    *
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   * 1. Redistributions of source code must retain the above copyright
13   *    notice, this list of conditions and the following disclaimer. 
14   *
15   * 2. Redistributions in binary form must reproduce the above copyright
16   *    notice, this list of conditions and the following disclaimer in
17   *    the documentation and/or other materials provided with the
18   *    distribution.
19   *
20   * 3. The end-user documentation included with the redistribution, if
21   *    any, must include the following acknowlegement:  
22   *       "This product includes software developed by the 
23   *        developers of Classifier4J (http://classifier4j.sf.net/)."
24   *    Alternately, this acknowlegement may appear in the software itself,
25   *    if and wherever such third-party acknowlegements normally appear.
26   *
27   * 4. The name "Classifier4J" must not be used to endorse or promote 
28   *    products derived from this software without prior written 
29   *    permission. For written permission, please contact   
30   *    http://sourceforge.net/users/nicklothian/.
31   *
32   * 5. Products derived from this software may not be called 
33   *    "Classifier4J", nor may "Classifier4J" appear in their names 
34   *    without prior written permission. For written permission, please 
35   *    contact http://sourceforge.net/users/nicklothian/.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
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 }