Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public abstract class SpellChecker
- Object
- SpellChecker
Checks the spelling of words and text. A checker reads either a Hunspell dictionary the
application supplies (create) or the platform’s own checker where the port has one
(platform); either way it adds a user dictionary and a list of ignored words, and skips
what is not a word to check: numbers, single letters, acronyms in capitals, URLs and email
addresses.
The build adds a Hunspell dictionary to an application that uses this class, for each
language named by the spellcheck.languages build hint (en_US when it is not set), and
forLanguage reads it. An application that brings its own dictionary sets
spellcheck.dictionaries=false and calls create. The simulator runs the application
unbuilt, so there forLanguage finds a dictionary only when the project depends on
com.codenameone:codenameone-spellcheck-dictionaries, and otherwise falls back to the
platform checker.
String text = "Teh quick brown fox";
SpellChecker spell = SpellChecker.forLanguage("en_US");
if (spell == null) {
// no dictionary on this device: bring one, as a Hunspell .aff and .dic pair
spell = SpellChecker.create(HunspellDictionary.fromBytes(
"SET UTF-8\n".getBytes(), "3\nquick\nbrown\nfox\n".getBytes()));
}
for (ProofIssue i : spell.check(text)) {
java.util.List<String> fixes = spell.suggest(text.substring(i.start, i.end), 5);
Log.p(text.substring(i.start, i.end) + " -> " + fixes);
}Fields
public static final String DICTIONARY_PREFIX = "cn1dict_" | The resource name prefix of the dictionaries the build adds: cn1dict_en_US.aff and cn1dict_en_US.dic for en_US. |
public static final String LANGUAGES_RESOURCE = "/cn1dict_languages.txt" | The resource listing the languages of the dictionaries the build added, one per line. |
Constructors
public SpellChecker() |
Methods
Inherited methods
Field details
DICTIONARY_PREFIX
public static final String DICTIONARY_PREFIX = "cn1dict_"cn1dict_en_US.aff and
cn1dict_en_US.dic for en_US. Names are flat because some ports flatten resource
folders.LANGUAGES_RESOURCE
public static final String LANGUAGES_RESOURCE = "/cn1dict_languages.txt"Constructor details
SpellChecker
public SpellChecker()Method details
lookup
protected abstract boolean lookup(String word)Parameters
wordString- the word as written
Returns
suggestions
protected abstract List<String> suggestions(String word, int max)Parameters
wordString- the misspelled word
maxint- the most to return
Returns
create
public static SpellChecker create(HunspellDictionary d)Parameters
dHunspellDictionary- the dictionary
Returns
forLanguage
public static SpellChecker forLanguage(String language)Parameters
languageString- a language tag such as
en_US;en-USis the same language
Returns
getBundledLanguages
public static List<String> getBundledLanguages()Returns
platform
public static SpellChecker platform(String language)Parameters
languageString- a language tag such as
en_US, or null for the device’s language
Returns
addWord
public void addWord(String word)Parameters
wordString- the word
removeWord
public void removeWord(String word)Parameters
wordString- the word
getUserWords
public Set<String> getUserWords()Returns
ignore
public void ignore(String word)clearIgnored.Parameters
wordString- the word
clearIgnored
public void clearIgnored()isCorrect
public boolean isCorrect(String word)Parameters
wordString- the word
Returns
suggest
public List<String> suggest(String word, int max)Parameters
wordString- the word
maxint- the most to return
Returns
words
public static List<int[]> words(String text){start, end} pairs, apostrophes and hyphens at either end
left out.Parameters
textString- the text
Returns
check
public List<ProofIssue> check(String text)suggest, which is far
slower than a check).Parameters
textString- the text, typically a paragraph