diff options
Diffstat (limited to 'spellchecker/test/SpellCorrectorTest.java')
-rw-r--r-- | spellchecker/test/SpellCorrectorTest.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/spellchecker/test/SpellCorrectorTest.java b/spellchecker/test/SpellCorrectorTest.java new file mode 100644 index 0000000..dbea0ab --- /dev/null +++ b/spellchecker/test/SpellCorrectorTest.java @@ -0,0 +1,118 @@ + +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; + + @Before + public void setUp() throws IOException { + fullCR = new MockCorpusReader(); + } + + private void checkGetCandidateWords(CorpusReader cr, String word, + Set<String> expResult) + throws IOException { + System.out.println("getCandidateWords(" + word + ")"); + SpellCorrector instance = new SpellCorrector(cr, null); + HashSet<String> result = instance.getCandidateWords(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<String> 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<String> 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<String> 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"); + checkGetCandidateWords(fullCR, "up", words); + } + + @Test + public void testGetCandidateWords3() throws IOException { + Set<String> 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"); + 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 HashSet<String> inVocabulary(Set<String> set) { + return new HashSet<>(set); + } + } +} |