emma
emma

Reputation: 47

Spell check method

I would like to construct a method where I check the spelling on the input from the user with a word from a dictionary and count the errors. This is my code right now and I don't know what is wrong, it just tells me String index is out of range

public static int spellCheck(String answer, String engWord) {
    int error = 0;

    for (int i = 0; i < engWord.length(); i++) {
      for (int j = 0; j < answer.length(); j++) {
        if (!(answer.charAt(i) == engWord.charAt(i))) {
          error++;
        }
      }
    }
    return error;

Upvotes: 0

Views: 1256

Answers (2)

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

You can roll your own spell checker against whatever word (dictionary) text file you want, for example:

/**
 * Checks the supplied word against the words contained within the supplied 
 * word (dictionary) text file. Each line within a word (dict) file must be 
 * a single word. This method is not letter case sensitive.<br><br>
 * 
 * @param dictPath (String) the path and file name to the Word file.<br>
 * 
 * @param wordToCheck (String) The word to check for spelling.<br>
 * 
 * @return (boolean) True is returned if the supplied word is contained 
 * within the supplied word file. False is returned if it is not.
 */
public static boolean spellCheck(String dictPath, String wordToCheck) {
    boolean res = false;
    java.nio.file.Path path = java.nio.file.Paths.get(dictPath);
    try (java.io.BufferedReader reader = java.nio.file.Files.newBufferedReader(path)) {
        String str;
        while ((str = reader.readLine()) != null) {
            if (str.trim().equalsIgnoreCase(wordToCheck)) {
                res = true;
                break;
            }
        }

    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
    return res;
}

How you might use this method within your console application:

java.util.Scanner userInput = new java.util.Scanner(System.in);
String dictionaryFilePath = "WordFile.txt";
    
String inputString = "";
while (inputString.isEmpty()) {
    System.out.println();
    System.out.println("Enter a String of any length:");
    System.out.print("Entry: --> ");
    inputString = userInput.nextLine().trim();
}
    
String[] words = inputString.split("\\s+");
java.util.List<String> misspelledWords = new java.util.ArrayList<>();
for (int i = 0; i < words.length; i++) {
    if (!spellCheck(dictionaryFilePath, words[i])) {
        misspelledWords.add(words[i]);
    }
}
    
if (!misspelledWords.isEmpty()) {
    System.out.println();
    System.out.println("You misspelled the following words or these");
    System.out.println("words were not within the dictionary:");
    System.out.println("============================================");
    for (String wrds : misspelledWords) {
        System.out.print(wrds + "\t");
    }
}
else {
    System.out.println("Good for you...No misspelled words!");
}

Upvotes: 0

j_b
j_b

Reputation: 2020

You may also consider 3rd party libraries that have similar functionality already baked in apache commons comes to mind. Check the StringUtils.difference method.

Something like:

diff = StringUtils.difference("act","actor);
System.out.println(diff.length());

Upvotes: 1

Related Questions