| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 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()); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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()); |
| 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 |
|
| 97 |
assertTrue(spell1.findReplacements("Rzękunia").contains("Rzekunia")); |
| 98 |
|
| 99 |
assertTrue(spell1.isInDictionary("Rzekunia")); |
| 100 |
assertTrue(spell1.findReplacements("Rzekunia").isEmpty()); |
| 101 |
|
| 102 |
assertTrue(spell1.findReplacements("Strefakibica").isEmpty()); |
| 103 |
|
| 104 |
assertTrue(spell1.findReplacements("").isEmpty()); |
| 105 |
|
| 106 |
assertTrue(spell1.findReplacements("\u0000").isEmpty()); |
| 107 |
|
| 108 |
assertTrue(spell1.findReplacements("«…»").isEmpty()); |
| 109 |
|
| 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")); |
| 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 |
|
| 133 |
assertTrue(spell.findReplacements("gesla").contains("gęślą")); |
| 134 |
assertTrue(spell.findReplacements("swieto").contains("Święto")); |
| 135 |
assertTrue(spell.findReplacements("zolwiowa").contains("żółwiową")); |
| 136 |
|
| 137 |
assertTrue(spell.findReplacements("jexn").contains("jaźń")); |
| 138 |
|
| 139 |
assertTrue(spell.findReplacements("zażulv").contains("zażółć")); |
| 140 |
|
| 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 |
|
| 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 |
|
| 155 |
List<String> reps = spell.findReplacements("jist"); |
| 156 |
for (final String word: reps) { |
| 157 |
assertTrue(spell.isInDictionary(word)); |
| 158 |
} |
| 159 |
|
| 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 |
|
| 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")); |
| 186 |
assertTrue(!spell.isMisspelled("!")); |
| 187 |
assertTrue(spell.isMisspelled("dziekie")); d |
| 188 |
assertTrue(!spell.isMisspelled("SłowozGarbem")); ąda |
| 189 |
assertTrue(!spell.isMisspelled("Ćwikła"));ry |
| 190 |
assertTrue(!spell.isMisspelled("TOJESTTEST")); |
| 191 |
final Speller oldStyleSpell = new Speller(dictionary, 1); |
| 192 |
assertTrue(oldStyleSpell.isMisspelled("Paragraf22")); |
| 193 |
assertTrue(oldStyleSpell.isMisspelled("!")); |
| 194 |
ąda |
| 195 |
assertTrue(oldStyleSpell.isMisspelled("Abaka")); |
| 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")); |
| 201 |
assertTrue(!spell1.isMisspelled("RZEKUNIA")); |
| 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 |
|
| 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 |
|
| 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); |
| 256 |
|
| 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 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |