Jimmy
Jimmy

Reputation: 10341

How to get spelling corrected words in Android?

When you type in any text area OS wide in Android (not only in my app) the user could use auto correct or click the misspelled word to correct it. Is there a way to get a list of all the misspelled words and what they were turned into?.

Alternatively, is this possible with a rooted device or can you even replace autocorrect with your own dictionary to track this?

Upvotes: 0

Views: 1585

Answers (2)

Hemanth S
Hemanth S

Reputation: 688

To find suggestion of Typed text

Spannable str = myEditText.getText(); 
SuggestionSpan[] spanned = str.getSpans(startIndex, endIndex, SuggestionSpan.class);

If spanned is not empty, there is an error in the text between startIndex and endIndex. By changing the values of these indices, it will be possible to find which word is erroneous. Each item in the spanned array has a field called mSuggestions, which is an array of strings and provides the suggested words for the erroneous word.

There is other method to check for: here

Upvotes: 0

RayfenWindspear
RayfenWindspear

Reputation: 6274

This functionality present in Android is provided by the Spell Checker Framework. Its documentation here and here shows how you can connect to it and interact with it's suggestions inside an app.

Because this is a service provided by the system, much like something such as Location Services, there isn't a way to hook into it system-wide without being rooted. I don't have any personal experience tinkering with the inner workings of the Android system or its services, but as a starting point, you could take a look into any open source apps that tamper with the input/output of some of these services e.g. GPS spoofing apps.

Worst case scenario would be you'd have to locate where the Spell Checker Framework compiles to and is stored, and overwrite it with a modified version that does what you want.

I was able to find an older version of SpellCheckerService.java here from Android 5.1.1. You might be able to look into this and figure out what you need to do and where.

EDIT: After checking out the entire list of 1149 Android source repos (100+ GB), I found the two most likely candidates for you to scour for an opening.

git clone https://android.googlesource.com/platform/frameworks/base

git clone https://android.googlesource.com/platform/packages/inputmethods/LatinIME

In base, you'll find the SpellChecker sources in base/core/java/android/****/textservice/, where **** is one of service, view, widget, or internal (internal is in com/android). This appears to be the lowest level. Many of the methods and such here are abstract.

In LatinIME, you'll find the higher level SpellChecker sources for the latin character set (there are other repos for other charsets). You will find them in LatinIME/java/src/com/android/inputmethod/latin/spellcheck/. This is where you'll find the implementations.

After tracing through the sources (specifically tracing getSuggestionResults), the calls go down to the Dictionary level. LatinIME/java/src/com/android/inputmethod/latin/Dictionary.java has abstract public ArrayList<SuggestedWordInfo> getSuggestions() which means that it is the Dictionary's responsibility to return the results. Still, I would assume that a user installable dictionary is just a simple database which is used by the Android system's Dictionary handling code, which likely means you are still going to need to modify system code and be rooted to accomplish your goal.

I'm afraid this is as far down the rabbit hole as I go on the subject. I fear I have not completely answered the question, but this should give you some direction on how you want to proceed.

YET ANOTHER EDIT: Maybe I'm wrong about an installable dictionary just being a database. See this example. This example appears to be in the form of an app though, so I am not sure.

Upvotes: 2

Related Questions