import java.io.IOException; import java.util.HashSet; import java.util.Set; import org.junit.Test; import static org.junit.Assert.*; import org.junit.Before; /** * @author Peter Wu */ public class SpellCorrectorTest { private CorpusReader fullCR; private ConfusionMatrixReader cmr; @Before public void setUp() throws IOException { fullCR = new MockCorpusReader(); cmr = new ConfusionMatrixReader(); } private void checkGetCandidateWords(CorpusReader cr, String word, Set expResult) throws IOException { // ignore unchanged word expResult.remove(word); System.out.println("getCandidateWords(" + word + ")"); SpellCorrector instance = new SpellCorrector(cr, cmr); Set result = instance.getCandidateWords(word).keySet(); assertFalse("Should not contain word " + word, result.contains(word)); // quick check: are the results of the same size? assertEquals(expResult.size(), result.size()); // verbose test: are all letters as expected? assertEquals(expResult, result); } @Test public void testGetCandidateWords0() throws IOException { Set words = new HashSet<>(); // test for empty word (only a letter can be inserted) for (char c : SpellCorrector.ALPHABET) { // insertion words.add("" + c); } checkGetCandidateWords(fullCR, "", words); } @Test public void testGetCandidateWords1() throws IOException { Set words = new HashSet<>(); // test for a single letter word for (char c : SpellCorrector.ALPHABET) { // insertion before and after words.add(c + "a"); words.add("a" + c); // substitution should replace the letter. words.add("" + c); } // deletion should not yield a string //words.add(""); checkGetCandidateWords(fullCR, "a", words); } @Test public void testGetCandidateWords2() throws IOException { Set words = new HashSet<>(); // test for a two letter word for (char c : SpellCorrector.ALPHABET) { // insertion before and after words.add(c + "up"); words.add("u" + c + "p"); words.add("up" + c); // substitution should replace the letter. words.add(c + "p"); words.add("u" + c); } // deletion words.add("p"); words.add("u"); // transposition words.add("pu"); checkGetCandidateWords(fullCR, "up", words); } @Test public void testGetCandidateWords3() throws IOException { Set words = new HashSet<>(); // test for a three letter word for (char c : SpellCorrector.ALPHABET) { // insertion before and after words.add(c + "ups"); words.add("u" + c + "ps"); words.add("up" + c + "s"); words.add("ups" + c); // substitution should replace the letter. words.add(c + "ps"); words.add("u" + c + "s"); words.add("up" + c); } // deletion words.add("ps"); words.add("us"); words.add("up"); // transposition words.add("pus"); words.add("usp"); checkGetCandidateWords(fullCR, "ups", words); } /** * Fake CorpusReader which tests whether getCandidateWords can produce all * words. */ private class MockCorpusReader extends CorpusReader { public MockCorpusReader() throws IOException { super(); } @Override public boolean inVocabulary(String word) { return true; } @Override public HashSet inVocabulary(Set set) { return new HashSet<>(set); } } }