Debian Codegraph @

morfologik-speller/src/test/java/morfologik/speller/SpellerTest.java

raw symbols SCIP
1 package morfologik.speller;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.IOException;
7 import java.net.URL;
8 import java.util.Arrays;
9 import java.util.List;
10
11 import morfologik.stemming.Dictionary;
12
13 import org.assertj.core.api.Assertions;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16
17 public class SpellerTest {
18 private static Dictionary dictionary;
19
20 @BeforeClass
21 public static void setup() throws Exception {
22 final URL url = SpellerTest.class.getResource("slownik.dict");
23 dictionary = Dictionary.read(url);
24 }
25
26 /*
27 @Test
28 public void testAbka() throws Exception {
29 final Speller spell = new Speller(dictionary, 2);
30 System.out.println("Replacements:");
31 for (String s : spell.findReplacements("abka")) {
32 System.out.println(s);
33 }
34 }
35 */
36
37 @Test
38 public void testRunonWords() throws IOException {
39 final Speller spell = new Speller(dictionary);
40 Assertions.assertThat(spell.replaceRunOnWords("abaka")).isEmpty();
41 Assertions.assertThat(spell.replaceRunOnWords("abakaabace")).contains("abaka abace");
42 Assertions.assertThat(spell.replaceRunOnWords("Abakaabace")).contains("Abaka abace");
43
44 // Test on an morphological dictionary - should work as well
45 final URL url1 = getClass().getResource("test-infix.dict");
46 final Speller spell1 = new Speller(Dictionary.read(url1));
47 assertTrue(spell1.replaceRunOnWords("Rzekunia").isEmpty());
48 assertTrue(spell1.replaceRunOnWords("RzekuniaRzeczypospolitej").contains("Rzekunia Rzeczypospolitej"));
49 assertTrue(spell1.replaceRunOnWords("RzekuniaRze").isEmpty()); //Rze is not found but is a prefix
50
51 final URL url2 = getClass().getResource("single-char-word.dict");
52 final Speller spell2 = new Speller(Dictionary.read(url2));
53 assertTrue(spell2.replaceRunOnWords("alot").contains("a lot"));
54 assertTrue(spell2.replaceRunOnWords("aalot").contains("aa lot"));
55 assertTrue(spell2.replaceRunOnWords("aamusement").contains("a amusement"));
56 assertTrue(spell2.replaceRunOnWords("clot").isEmpty());
57 assertTrue(spell2.replaceRunOnWords("foobar").isEmpty());
58 }
59
60 @Test
61 public void testIsInDictionary() throws IOException {
62 // Test on an morphological dictionary, including separators
63 final URL url1 = getClass().getResource("test-infix.dict");
64 final Speller spell1 = new Speller(Dictionary.read(url1));
65 assertTrue(spell1.isInDictionary("Rzekunia"));
66 assertTrue(!spell1.isInDictionary("Rzekunia+"));
67 assertTrue(!spell1.isInDictionary("Rzekunia+aaa"));
68 // test UTF-8 dictionary
69 final URL url = getClass().getResource("test-utf-spell.dict");
70 final Speller spell = new Speller(Dictionary.read(url));
71 assertTrue(spell.isInDictionary("jaźń"));
72 assertTrue(spell.isInDictionary("zażółć"));
73 assertTrue(spell.isInDictionary("żółwiową"));
74 assertTrue(spell.isInDictionary("ćwikła"));
75 assertTrue(spell.isInDictionary("Żebrowski"));
76 assertTrue(spell.isInDictionary("Święto"));
77 assertTrue(spell.isInDictionary("Świerczewski"));
78 assertTrue(spell.isInDictionary("abc"));
79 }
80
81 @Test
82 public void testFindReplacements() throws IOException {
83 final Speller spell = new Speller(dictionary, 1);
84 assertTrue(spell.findReplacements("abka").contains("abak"));
85 //check if we get only dictionary words...
86 List<String> reps = spell.findReplacements("bak");
87 for (final String word: reps) {
88 assertTrue(spell.isInDictionary(word));
89 }
90 assertTrue(spell.findReplacements("abka~~").isEmpty()); // 2 characters more -> edit distance too large
91 assertTrue(!spell.findReplacements("Rezkunia").contains("Rzekunia"));
92
93 final URL url1 = getClass().getResource("test-infix.dict");
94 final Speller spell1 = new Speller(Dictionary.read(url1));
95 assertTrue(spell1.findReplacements("Rezkunia").contains("Rzekunia"));
96 //diacritics
97 assertTrue(spell1.findReplacements("Rzękunia").contains("Rzekunia"));
98 //we should get no candidates for correct words
99 assertTrue(spell1.isInDictionary("Rzekunia"));
100 assertTrue(spell1.findReplacements("Rzekunia").isEmpty());
101 //and no for things that are too different from the dictionary
102 assertTrue(spell1.findReplacements("Strefakibica").isEmpty());
103 //nothing for nothing
104 assertTrue(spell1.findReplacements("").isEmpty());
105 //nothing for weird characters
106 assertTrue(spell1.findReplacements("\u0000").isEmpty());
107 //nothing for other characters
108 assertTrue(spell1.findReplacements("«…»").isEmpty());
109 //nothing for separator
110 assertTrue(spell1.findReplacements("+").isEmpty());
111
112 }
113
114 @Test
115 public void testFrequencyNonUTFDictionary() throws IOException {
116 final URL url1 = getClass().getResource("test_freq_iso.dict");
117 final Speller spell = new Speller(Dictionary.read(url1));
118 assertTrue(spell.isInDictionary("a"));
119 assertTrue(!spell.isInDictionary("aõh")); //non-encodable in UTF-8
120 }
121
122 @Test
123 public void testFindReplacementsInUTF() throws IOException {
124 final URL url = getClass().getResource("test-utf-spell.dict");
125 final Speller spell = new Speller(Dictionary.read(url));
126 assertTrue(spell.findReplacements("gęslą").contains("gęślą"));
127 assertTrue(spell.findReplacements("ćwikla").contains("ćwikła"));
128 assertTrue(spell.findReplacements("Swierczewski").contains("Świerczewski"));
129 assertTrue(spell.findReplacements("zółwiową").contains("żółwiową"));
130 assertTrue(spell.findReplacements("Żebrowsk").contains("Żebrowski"));
131 assertTrue(spell.findReplacements("święto").contains("Święto"));
132 //note: no diacritics here, but we still get matches!
133 assertTrue(spell.findReplacements("gesla").contains("gęślą"));
134 assertTrue(spell.findReplacements("swieto").contains("Święto"));
135 assertTrue(spell.findReplacements("zolwiowa").contains("żółwiową"));
136 //using equivalent characters 'x' = 'ź'
137 assertTrue(spell.findReplacements("jexn").contains("jaźń"));
138 // 'u' = 'ó', so the edit distance is still small...
139 assertTrue(spell.findReplacements("zażulv").contains("zażółć"));
140 // 'rz' = 'ż', so the edit distance is still small, but with string replacements...
141 assertTrue(spell.findReplacements("zarzulv").contains("zażółć"));
142 assertTrue(spell.findReplacements("Rzebrowski").contains("Żebrowski"));
143 assertTrue(spell.findReplacements("rzółw").contains("żółw"));
144 assertTrue(spell.findReplacements("Świento").contains("Święto"));
145 // avoid mixed-case words as suggestions when using replacements ('rz' = 'ż')
146 assertTrue(spell.findReplacements("zArzółć").get(0).equals("zażółć"));
147 }
148
149 @Test
150 public void testFindReplacementsUsingFrequency() throws IOException {
151 final URL url = getClass().getResource("dict-with-freq.dict");
152 final Speller spell = new Speller(Dictionary.read(url));
153
154 //check if we get only dictionary words...
155 List<String> reps = spell.findReplacements("jist");
156 for (final String word: reps) {
157 assertTrue(spell.isInDictionary(word));
158 }
159 // get replacements ordered by frequency
160 assertTrue(reps.get(0).equals("just"));
161 assertTrue(reps.get(1).equals("list"));
162 assertTrue(reps.get(2).equals("fist"));
163 assertTrue(reps.get(3).equals("mist"));
164 assertTrue(reps.get(4).equals("jest"));
165 assertTrue(reps.get(5).equals("dist"));
166 assertTrue(reps.get(6).equals("gist"));
167 }
168
169 @Test
170 public void testConcurrentReplacements() throws IOException {
171 final URL url = getClass().getResource("dict-with-freq.dict");
172 final Speller spell = new Speller(Dictionary.read(url));
173 //only the longest key is selected in replacement pairs
174 List<String> reps = spell.getAllReplacements("teached", 0, 0);
175 assertTrue(reps.contains("teached"));
176 assertTrue(reps.contains("taught"));
177 assertTrue(!reps.contains("tgheached"));
178 }
179
180
181 @Test
182 public void testIsMisspelled() throws IOException {
183 final URL url = getClass().getResource("test-utf-spell.dict");
184 final Speller spell = new Speller(Dictionary.read(url));
185 assertTrue(!spell.isMisspelled("Paragraf22")); //ignorujemy liczby
186 assertTrue(!spell.isMisspelled("!")); //ignorujemy znaki przestankowe
187 assertTrue(spell.isMisspelled("dziekie")); //test, czy znajdujemy błąd
188 assertTrue(!spell.isMisspelled("SłowozGarbem")); //ignorujemy słowa w stylu wielbłąda
189 assertTrue(!spell.isMisspelled("Ćwikła")); //i małe litery
190 assertTrue(!spell.isMisspelled("TOJESTTEST")); //i wielkie litery
191 final Speller oldStyleSpell = new Speller(dictionary, 1);
192 assertTrue(oldStyleSpell.isMisspelled("Paragraf22")); // nie ignorujemy liczby
193 assertTrue(oldStyleSpell.isMisspelled("!")); //nie ignorujemy znaków przestankowych
194 // assertTrue(oldStyleSpell.isMisspelled("SłowozGarbem")); //ignorujemy słowa w stylu wielbłąda
195 assertTrue(oldStyleSpell.isMisspelled("Abaka")); //i małe litery
196 final URL url1 = getClass().getResource("test-infix.dict");
197 final Speller spell1 = new Speller(Dictionary.read(url1));
198 assertTrue(!spell1.isMisspelled("Rzekunia"));
199 assertTrue(spell1.isAllUppercase("RZEKUNIA"));
200 assertTrue(spell1.isMisspelled("RZEKUNIAA")); // finds a typo here
201 assertTrue(!spell1.isMisspelled("RZEKUNIA")); // but not here
202 }
203
204 @Test
205 public void testCamelCase() {
206 final Speller spell = new Speller(dictionary, 1);
207 assertTrue(spell.isCamelCase("CamelCase"));
208 assertTrue(!spell.isCamelCase("Camel"));
209 assertTrue(!spell.isCamelCase("CAMEL"));
210 assertTrue(!spell.isCamelCase("camel"));
211 assertTrue(!spell.isCamelCase("cAmel"));
212 assertTrue(!spell.isCamelCase("CAmel"));
213 assertTrue(!spell.isCamelCase(""));
214 assertTrue(!spell.isCamelCase(null));
215 }
216
217 @Test
218 public void testCapitalizedWord() {
219 final Speller spell = new Speller(dictionary, 1);
220 assertTrue(spell.isNotCapitalizedWord("CamelCase"));
221 assertTrue(!spell.isNotCapitalizedWord("Camel"));
222 assertTrue(spell.isNotCapitalizedWord("CAMEL"));
223 assertTrue(spell.isNotCapitalizedWord("camel"));
224 assertTrue(spell.isNotCapitalizedWord("cAmel"));
225 assertTrue(spell.isNotCapitalizedWord("CAmel"));
226 assertTrue(spell.isNotCapitalizedWord(""));
227 }
228
229 @Test
230 public void testGetAllReplacements() throws IOException {
231 final URL url = getClass().getResource("test-utf-spell.dict");
232 final Speller spell = new Speller(Dictionary.read(url));
233 assertTrue(spell.isMisspelled("rzarzerzarzu"));
234 assertEquals("[rzarzerzarzu]",
235 Arrays.toString(spell.getAllReplacements("rzarzerzarzu", 0, 0).toArray()));
236 }
237
238 @Test
239 public void testEditDistanceCalculation() throws IOException {
240 final Speller spell = new Speller(dictionary, 5);
241 //test examples from Oflazer's paper
242 assertTrue(getEditDistance(spell, "recoginze", "recognize") == 1);
243 assertTrue(getEditDistance(spell, "sailn", "failing") == 3);
244 assertTrue(getEditDistance(spell, "abc", "abcd") == 1);
245 assertTrue(getEditDistance(spell, "abc", "abcde") == 2);
246 //test words from fsa_spell output
247 assertTrue(getEditDistance(spell, "abka", "abaka") == 1);
248 assertTrue(getEditDistance(spell, "abka", "abakan") == 2);
249 assertTrue(getEditDistance(spell, "abka", "abaką") == 2);
250 assertTrue(getEditDistance(spell, "abka", "abaki") == 2);
251 }
252
253 @Test
254 public void testCutOffEditDistance() throws IOException {
255 final Speller spell2 = new Speller(dictionary, 2); //note: threshold = 2
256 //test cut edit distance - reprter / repo from Oflazer
257 assertTrue(getCutOffDistance(spell2, "repo", "reprter") == 1);
258 assertTrue(getCutOffDistance(spell2, "reporter", "reporter") == 0);
259 }
260
261 @Test
262 public void testReplacementsAndDistance2() throws Exception {
263 /*File infoFile = new File("/tmp/morfologik.info");
264 FileWriter fw1 = new FileWriter(infoFile);
265 fw1.write("fsa.dict.separator=+\n");
266 fw1.write("fsa.dict.encoding=utf-8\n");
267 fw1.write("fsa.dict.speller.replacement-pairs=s ss,t d,R Rh,y ij,ę em,em ę\n");
268 fw1.close();
269
270 File inputFile = new File("/tmp/morfologik.txt");
271 FileWriter fw2 = new FileWriter(inputFile);
272 fw2.write("Mitmuss\n");
273 fw2.write("Rhythmus\n");
274 fw2.write("Wald\n");
275 fw2.write("Band\n");
276 fw2.write("ijo\n");
277 fw2.write("ijond\n");
278 fw2.write("youd\n");
279 fw2.write("ijoussud\n");
280 fw2.write("ijoussuud\n");
281 fw2.write("ijussuud\n");
282 fw2.write("ijousod\n");
283 fw2.write("ij\n");
284 fw2.write("ijo\n");
285 fw2.write("Ciarkę\n");
286 fw2.write("Czarkę\n");
287 fw2.write("Clarke\n");
288 fw2.write("Clarkiem\n");
289 fw2.write("Clarkom\n");
290
291 fw2.close();
292
293 File dictFile = new File("/tmp/morfologik.dict");
294 String[] buildToolOptions =
295 {"-i", inputFile.getAbsolutePath(), "-o", dictFile.getAbsolutePath()};
296 FSABuildTool.main(buildToolOptions);
297 Dictionary dictionary = Dictionary.read(dictFile);
298 Speller speller = new Speller(dictionary, 3);*/
299
300 final URL url = getClass().getResource("reps_dist2.dict");
301 final Speller speller = new Speller(Dictionary.read(url), 3);
302
303 List<String> reps = speller.findReplacements("Rytmus");
304 assertTrue(reps.get(0).equals("Rhythmus"));
305 assertTrue(reps.get(1).equals("Mitmuss"));
306 reps = speller.findReplacements("Walt");
307 assertTrue(reps.get(0).equals("Wald"));
308 assertTrue(reps.get(1).equals("Band"));
309 reps = speller.findReplacements("yout");
310 assertTrue(reps.get(0).equals("youd"));
311 assertTrue(reps.get(1).equals("ijond"));
312 assertTrue(reps.get(2).equals("ijo"));
313 reps = speller.findReplacements("yousut");
314 assertTrue(reps.get(0).equals("ijoussud"));
315 assertTrue(reps.get(1).equals("ijousod"));
316 assertTrue(reps.get(2).equals("ijoussuud"));
317 assertTrue(reps.get(3).equals("youd"));
318 reps = speller.findReplacements("yo");
319 assertTrue(reps.get(0).equals("ijo"));
320 assertTrue(reps.get(1).equals("ij"));
321 reps = speller.findReplacements("Clarkem");
322 assertTrue(reps.get(0).equals("Ciarkę"));
323 assertTrue(reps.get(1).equals("Clarke"));
324 assertTrue(reps.get(2).equals("Clarkiem"));
325 assertTrue(reps.get(3).equals("Clarkom"));
326 assertTrue(reps.get(4).equals("Czarkę"));
327 }
328
329 @Test
330 public void testIssue94() throws Exception {
331 final URL url = getClass().getResource("issue94.dict");
332 final Speller speller = new Speller(Dictionary.read(url));
333 List<String> reps = speller.findReplacements("schänken");
334 assertTrue(reps.get(0).equals("Schänken"));
335 assertTrue(reps.get(1).equals("schenken"));
336 }
337
338 private int getCutOffDistance(final Speller spell, final String word, final String candidate) {
339 // assuming there is no pair-replacement
340 spell.setWordAndCandidate(word, candidate);
341 final int [] ced = new int[spell.getCandLen() - spell.getWordLen()];
342 for (int i = 0; i < spell.getCandLen() - spell.getWordLen(); i++) {
343 ced[i] = spell.cuted(spell.getWordLen() + i, spell.getWordLen() + i, spell.getWordLen() + i);
344 }
345 Arrays.sort(ced);
346 //and the min value...
347 if (ced.length > 0) {
348 return ced[0];
349 }
350 return 0;
351 }
352
353 private int getEditDistance(final Speller spell, final String word, final String candidate) {
354 // assuming there is no pair-replacement
355 spell.setWordAndCandidate(word, candidate);
356 final int maxDistance = spell.getEffectiveED();
357 final int candidateLen = spell.getCandLen();
358 final int wordLen = spell.getWordLen();
359 int ed = 0;
360 for (int i = 0; i < candidateLen; i++) {
361 if (spell.cuted(i, i, i) <= maxDistance) {
362 if (Math.abs(wordLen - 1 - i) <= maxDistance) {
363 ed = spell.ed(wordLen - 1, i, wordLen - 1, i);
364 }
365 }
366 }
367 return ed;
368 }
369 }