Static spell checking behavior

32 views Asked by At

I am trying to implement behavior similar to spell check, however not exactly same. By static, I mean only on certain words.

So let's say I have an array of 10 strings.

String[] originalWords = new String[] {
        "Apple", "Banana", "Orange", "Pear", "Grape",
        "Pineapple", "Lemon", "Mango", "Cherry", "Peach"
};

Now, I have an EditText where user can type in something. After user finishes typing (they press next, etc.), I want to perform a check on what they typed in.

String userInput = String.valueOf(editText.getText()).trim();

Let's say userInput value is appla. That means what user typed in is only one letter off the world Apple (which is in the array).

That's the check I want to implement. Whatever the user types in, if one SIMILAR (1 or 2 letters off) word exists in the array, I want to get that word. How would I go about implementing that?

Result examples:

aoole ==> Apple
orenge ==> Orange
Cheery ==> Cherry
1

There are 1 answers

0
ᴛʜᴇᴘᴀᴛᴇʟ On

I was able to figure this out. It doesn't go exactly the way I wanted (1 or 2 letters off) but it does the job because it gives the of percentage of similarity.

I used this library.

// array of original words to compare against
String[] originalWords = new String[] {
        "Apple", "Banana", "Orange", "Pear", "Grape",
        "Pineapple", "Lemon", "Mango", "Cherry", "Peach"
};

// convert String array to ArrayList
ArrayList<String> originals = (ArrayList<String>) Arrays.asList(originalWords);

ExtractedResult extractedResult = FuzzySearch.extractOne("aoole", originals);

if(extractedResult.getScore() >= 75) {
    // the word provided (aoole) is at least 75% similar to one of the original words. 
    // To get the original word:
    String result = extractedResult.getString();
}