diff options
author | Peter Wu <peter@lekensteyn.nl> | 2015-03-31 22:50:10 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2015-03-31 22:50:10 +0200 |
commit | 79c2db9d109a91d0b3d821b588c7ce3915d2ff0c (patch) | |
tree | f270caeeeae48a6cbda582f82c1b2306e11bc682 /spellchecker | |
parent | 925caaf80c022cd1ff82e640a71bb3c796de820b (diff) | |
download | assignment4-79c2db9d109a91d0b3d821b588c7ce3915d2ff0c.tar.gz |
Refactor getCandidateWords into separate methods
For readability and testability.
Diffstat (limited to 'spellchecker')
-rw-r--r-- | spellchecker/src/SpellCorrector.java | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/spellchecker/src/SpellCorrector.java b/spellchecker/src/SpellCorrector.java index e1ee640..682101a 100644 --- a/spellchecker/src/SpellCorrector.java +++ b/spellchecker/src/SpellCorrector.java @@ -55,6 +55,16 @@ public class SpellCorrector { ListOfWords.add(word2); }; + makeWordInsertion(word, push); + makeWordSubstitution(word, push); + makeWordDeletion(word, push); + makeWordTransposition(word, push); + + return cr.inVocabulary(ListOfWords); + } + + private void makeWordInsertion(String word, + TriFunction<String, String, String> push) { // Generate words by insertion of a character // |p]en -> [ap]en, [p|en -> [pi]en, p[e|n -> p[ei]n, pe[n| -> pe[nm]. for (int i = 0; i <= word.length(); i++) { @@ -75,8 +85,11 @@ public class SpellCorrector { } } } + } - // by substitution of a character + private void makeWordSubstitution(String word, + TriFunction<String, String, String> push) { + // Generate words by substitution of a character. // |p]en -> [P]en, p|e]n -> p[E]n, pe|n] -> pe[N]. for (int i = 0; i < word.length(); i++) { String head, middle, tail; @@ -90,8 +103,12 @@ public class SpellCorrector { push.call(head + c + tail, middle, "" + c); } } - // by deletion of a character. Requires at least two characters in the - // word (an empty string is invalid). + } + + private void makeWordDeletion(String word, + TriFunction<String, String, String> push) { + // Generare words by deletion of a character. Requires at least two + // characters in the word (an empty string is invalid). // |pe]in -> [e]in, p|ei]n -> p[i]n, pe|in] -> pe[n], pe[i|n] -> pe[i]. if (word.length() > 1) { for (int i = 0; i < word.length(); i++) { @@ -113,7 +130,10 @@ public class SpellCorrector { push.call(head + tail, error, middle); } } + } + private void makeWordTransposition(String word, + TriFunction<String, String, String> push) { // The Damerau-Levenshtein distance also includes transposition of two // adjacent letters to account for spelling mistakes. // [p|e]n -> [ep]n, p[e|n] -> p[ne]. @@ -128,7 +148,5 @@ public class SpellCorrector { push.call(head + transposed + tail, middle, transposed); } - - return cr.inVocabulary(ListOfWords); } } |