/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
- package spellingCorrection;
-
- import java.io.IOException;
- import java.util.*;
-
-
- public class AnotherImplementationOfSpellingCorrector {
-
- private static final int INITIAL_LINKED_HASHSET_CAPACITY = 100000;
-
- private Set<String> dictionarySet = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
-
- private List<String> wordsForCorrection = new LinkedList<>();
-
- public AnotherImplementationOfSpellingCorrector() throws IOException {
- SpellingUtils.initializeDictionaryListAndWordList(dictionarySet, wordsForCorrection);
- }
-
-
- private Set<String> getAllFirstEditsForWord(String word) {
-
- Set<String> result = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
- for (int i = 0; i < word.length(); ++i)
- result.add(word.substring(0, i) + word.substring(i + 1));
-
-
- for (int i = 0; i < word.length() + 1; ++i)
- for (char c = 'a'; c <= 'z'; ++c)
- result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
- return result;
- }
-
- public Set<String> getCorrectionsFirstEdits(Set<String> possibleEdits) {
- Set<String> firstEdits = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
-
- Iterator<String> editsIterator = possibleEdits.iterator();
- String edits = null;
-
- while (editsIterator.hasNext()) {
- edits = editsIterator.next();
- if (dictionarySet.contains(edits)) {
- firstEdits.add(edits);
- }
- }
- return firstEdits;
- }
-
- public Set<String> getFinalCorrectedWords(String wordForCorrection) {
- Set<String> correctedWordSet = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
-
- Set<String> allFirstEdits = null;
-
- Set<String> firstCorrectedEdits = null;
-
-
- if (dictionarySet.contains(wordForCorrection)) {
- correctedWordSet.add(wordForCorrection);
- } else {
- allFirstEdits = getAllFirstEditsForWord(wordForCorrection);
- firstCorrectedEdits = this.getCorrectionsFirstEdits(allFirstEdits);
-
-
- if (firstCorrectedEdits.isEmpty()) {
- Set<String> allSecondEdits = getCorrectionsSecondEdits(allFirstEdits);
- for (String str : allSecondEdits) {
- if (dictionarySet.contains(str)) {
- correctedWordSet.add(str);
- }
- }
- } else {
-
- correctedWordSet.addAll(firstCorrectedEdits);
- }
-
-
- }
-
-
- return correctedWordSet;
- }
-
-
- public Set<String> getCorrectionsSecondEdits(Set<String> firstEdits) {
- Set<String> secondCorrections = new LinkedHashSet<>(INITIAL_LINKED_HASHSET_CAPACITY);
-
- for (String str : firstEdits) {
-
- secondCorrections.addAll(getAllFirstEditsForWord(str));
-
- }
- return secondCorrections;
- }
-
-
- public Set<String> getDictionarySet() {
- return dictionarySet;
- }
-
- public void setDictionarySet(Set<String> dictionarySet) {
- this.dictionarySet = dictionarySet;
- }
-
- public List<String> getWordsForCorrection() {
- return wordsForCorrection;
- }
-
- public void setWordsForCorrection(List<String> wordsForCorrection) {
- this.wordsForCorrection = wordsForCorrection;
- }
-
- public static void main(String[] args) throws IOException, InterruptedException {
- AnotherImplementationOfSpellingCorrector spelling = new AnotherImplementationOfSpellingCorrector();
-
-
- for (String str : spelling.getFinalCorrectedWords("mainy"))
- System.out.println(str);
-
- }
- }