/SpellChecking/src/spellingCorrection/AnotherImplementationOfSpellingCorrector.java

https://github.com/angry-gopher/spell_check · Java · 119 lines · 78 code · 41 blank · 0 comment · 13 complexity · 868ff655e2d21caf8e68d2bf7d217f50 MD5 · raw file

  1. package spellingCorrection;
  2. import java.io.IOException;
  3. import java.util.*;
  4. public class AnotherImplementationOfSpellingCorrector {
  5. private static final int INITIAL_LINKED_HASHSET_CAPACITY = 100000;
  6. private Set<String> dictionarySet = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
  7. private List<String> wordsForCorrection = new LinkedList<>();
  8. public AnotherImplementationOfSpellingCorrector() throws IOException {
  9. SpellingUtils.initializeDictionaryListAndWordList(dictionarySet, wordsForCorrection);
  10. }
  11. private Set<String> getAllFirstEditsForWord(String word) {
  12. Set<String> result = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
  13. for (int i = 0; i < word.length(); ++i)
  14. result.add(word.substring(0, i) + word.substring(i + 1));
  15. for (int i = 0; i < word.length() + 1; ++i)
  16. for (char c = 'a'; c <= 'z'; ++c)
  17. result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
  18. return result;
  19. }
  20. public Set<String> getCorrectionsFirstEdits(Set<String> possibleEdits) {
  21. Set<String> firstEdits = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
  22. Iterator<String> editsIterator = possibleEdits.iterator();
  23. String edits = null;
  24. while (editsIterator.hasNext()) {
  25. edits = editsIterator.next();
  26. if (dictionarySet.contains(edits)) {
  27. firstEdits.add(edits);
  28. }
  29. }
  30. return firstEdits;
  31. }
  32. public Set<String> getFinalCorrectedWords(String wordForCorrection) {
  33. Set<String> correctedWordSet = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
  34. Set<String> allFirstEdits = null;
  35. Set<String> firstCorrectedEdits = null;
  36. if (dictionarySet.contains(wordForCorrection)) {
  37. correctedWordSet.add(wordForCorrection);
  38. } else {
  39. allFirstEdits = getAllFirstEditsForWord(wordForCorrection);
  40. firstCorrectedEdits = this.getCorrectionsFirstEdits(allFirstEdits);
  41. if (firstCorrectedEdits.isEmpty()) {
  42. Set<String> allSecondEdits = getCorrectionsSecondEdits(allFirstEdits);
  43. for (String str : allSecondEdits) {
  44. if (dictionarySet.contains(str)) {
  45. correctedWordSet.add(str);
  46. }
  47. }
  48. } else {
  49. correctedWordSet.addAll(firstCorrectedEdits);
  50. }
  51. }
  52. return correctedWordSet;
  53. }
  54. public Set<String> getCorrectionsSecondEdits(Set<String> firstEdits) {
  55. Set<String> secondCorrections = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
  56. for (String str : firstEdits) {
  57. secondCorrections.addAll(getAllFirstEditsForWord(str));
  58. }
  59. return secondCorrections;
  60. }
  61. public Set<String> getDictionarySet() {
  62. return dictionarySet;
  63. }
  64. public void setDictionarySet(Set<String> dictionarySet) {
  65. this.dictionarySet = dictionarySet;
  66. }
  67. public List<String> getWordsForCorrection() {
  68. return wordsForCorrection;
  69. }
  70. public void setWordsForCorrection(List<String> wordsForCorrection) {
  71. this.wordsForCorrection = wordsForCorrection;
  72. }
  73. public static void main(String[] args) throws IOException, InterruptedException {
  74. AnotherImplementationOfSpellingCorrector spelling = new AnotherImplementationOfSpellingCorrector();
  75. for (String str : spelling.getFinalCorrectedWords("mainy"))
  76. System.out.println(str);
  77. }
  78. }