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  package net.sf.classifier4J;
52  
53  import java.io.ByteArrayInputStream;
54  
55  import java.util.Arrays;
56  import java.util.Map;
57  
58  import junit.framework.TestCase;
59  
60  /***
61   * @author Nick Lothian
62   * @author Peter Leschev
63   */
64  public class UtilitiesTest extends TestCase {
65  
66  	String sentence = "Hello there hello again and hello again.";
67  
68  	public void testGetWordFrequency() {
69  
70  		// standard test
71  		Map result = Utilities.getWordFrequency(sentence);
72  		assertNotNull(result);
73  		assertEquals(2, result.size());
74  		assertNotNull(result.get("hello"));
75  		assertEquals(new Integer(3), (Integer) result.get("hello"));
76  		//assertEquals(new Integer(1), (Integer)result.get("there"));
77  		//assertEquals(new Integer(1), (Integer)result.get("and"));
78  		assertEquals(new Integer(2), (Integer) result.get("again"));
79  
80  		// test case sensitivity
81  		result = Utilities.getWordFrequency(sentence, true);
82  		assertNotNull(result);
83  		assertEquals(3, result.size());
84  		assertNotNull(result.get("hello"));
85  		assertEquals(new Integer(2), (Integer) result.get("hello"));
86  		assertEquals(new Integer(1), (Integer) result.get("Hello"));
87  		//assertEquals(new Integer(1), (Integer)result.get("there"));
88  		//assertEquals(new Integer(1), (Integer)result.get("and"));
89  		assertEquals(new Integer(2), (Integer) result.get("again"));
90  
91  		// test without a stop word provider
92  		result = Utilities.getWordFrequency(sentence, false, new DefaultTokenizer(), null);
93  		assertNotNull(result);
94  		assertEquals(4, result.size());
95  		assertNotNull(result.get("hello"));
96  		assertEquals(new Integer(3), (Integer) result.get("hello"));
97  		assertEquals(new Integer(1), (Integer) result.get("there"));
98  		assertEquals(new Integer(1), (Integer) result.get("and"));
99  		assertEquals(new Integer(2), (Integer) result.get("again"));
100 
101 	}
102 
103 	public void testGetUniqueWords() {
104 		String[] result = Utilities.getUniqueWords(null);
105 		assertNotNull(result);
106 		assertEquals(0, result.length);
107 
108 		String[] input = { "one", "one", "one", "two", "three" };
109 		String[] expectedResult = { "one", "three", "two" };
110 
111 		result = Utilities.getUniqueWords(input);
112 
113 		assertNotNull(result);
114 		assertEquals(expectedResult.length, result.length);
115 
116 		Arrays.sort(expectedResult);
117 		Arrays.sort(result);
118 
119 		for (int i = 0; i < expectedResult.length; i++) {
120 			assertEquals(expectedResult[i], result[i]);
121 		}
122 
123 		String[] words = new DefaultTokenizer().tokenize(sentence.toLowerCase());
124 		result = Utilities.getUniqueWords(words);
125 		assertEquals(4, result.length);
126 	}
127 
128 	public void testCountWords() {
129 		String[] words = { "word", "word", "word", "notword", "z", "a" };
130 		Arrays.sort(words);
131 		assertEquals(3, Utilities.countWords("word", words));
132 
133 		String[] words2 = { "word", "word", "word" };
134 		Arrays.sort(words2);
135 		assertEquals(3, Utilities.countWords("word", words2));
136 
137 		String[] words3 = {
138 		};
139 		Arrays.sort(words3);
140 		assertEquals(0, Utilities.countWords("word", words3));
141 
142 		String[] words4 = { "notword", "z", "a" };
143 		Arrays.sort(words4);
144 		assertEquals(0, Utilities.countWords("word", words4));
145 	}
146 
147 	public void testGetSentences() {
148 
149 		String[] result = Utilities.getSentences(null);
150 		assertNotNull(result);
151 		assertEquals(0, result.length);
152 
153 		String sentence1 = "This is sentence one";
154 		String sentence2 = "This is sentence two";
155 		String someSentences = sentence1 + "... " + sentence2 + "..";
156 		result = Utilities.getSentences(someSentences);
157 		assertNotNull(result);
158 		assertEquals(2, result.length);
159 		assertEquals(sentence1, result[0].trim());
160 		assertEquals(sentence2, result[1].trim());
161 
162 		someSentences = sentence1 + "! " + sentence2 + ".";
163 		result = Utilities.getSentences(someSentences);
164 		assertNotNull(result);
165 		assertEquals(2, result.length);
166 		assertEquals(sentence1, result[0].trim());
167 		assertEquals(sentence2, result[1].trim());
168 
169 		someSentences = sentence1 + "? " + sentence2 + ".";
170 		result = Utilities.getSentences(someSentences);
171 		assertNotNull(result);
172 		assertEquals(2, result.length);
173 		assertEquals(sentence1, result[0].trim());
174 		assertEquals(sentence2, result[1].trim());
175 	}
176 
177     public void testGetString() throws Exception {       
178         assertEquals(sentence, Utilities.getString(
179                                    new ByteArrayInputStream(
180                                        sentence.getBytes())));
181     }
182     
183     public static void main(String[] args) throws Exception {
184         junit.textui.TestRunner.run(UtilitiesTest.class);
185     }        
186 }