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 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
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
77
78 assertEquals(new Integer(2), (Integer) result.get("again"));
79
80
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
88
89 assertEquals(new Integer(2), (Integer) result.get("again"));
90
91
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 }