Class 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);
}
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a word to the user dictionary, so it is spelled correctly in any case.The misspelled words of a text, without suggestions (asksuggest, which is far slower than a check).voidForgets every ignored word.static SpellCheckerA checker over a Hunspell dictionary.static SpellCheckerforLanguage(String language) A checker for a language: the dictionary the build added for it, or else the platform's own checker.The languages of the dictionaries the build added.The words of the user dictionary, in lower case.voidIgnores a word, exactly as written, untilclearIgnored.booleanIs the word spelled correctly? Numbers, words with digits, single letters and words in capitals are; each part of a hyphenated word is checked on its own.protected abstract booleanIs this word, already trimmed and not empty, in the underlying dictionary?static SpellCheckerThe platform's own checker for a language, where the port has one.voidremoveWord(String word) Removes a word from the user dictionary.Suggestions for a misspelled word, best first, user dictionary words included.suggestions(String word, int max) Suggestions from the underlying dictionary.static List<int[]> The words of a text as{start, end}pairs, apostrophes and hyphens at either end left out.
-
Field Details
-
DICTIONARY_PREFIX
The resource name prefix of the dictionaries the build adds:cn1dict_en_US.affandcn1dict_en_US.dicforen_US. Names are flat because some ports flatten resource folders.- See Also:
-
LANGUAGES_RESOURCE
The resource listing the languages of the dictionaries the build added, one per line.- See Also:
-
-
Constructor Details
-
SpellChecker
public SpellChecker()
-
-
Method Details
-
lookup
Is this word, already trimmed and not empty, in the underlying dictionary?
Parameters
word: the word as written
Returns
true when it is spelled correctly
-
suggestions
-
create
A checker over a Hunspell dictionary.
Parameters
d: the dictionary
Returns
the checker
-
forLanguage
A checker for a language: the dictionary the build added for it, or else the platform's own checker. Each call returns a new checker, with its own user words, over a dictionary read once and shared; reading one takes a moment, so call this off the EDT when a delay would show.
Parameters
language: a language tag such asen_US;en-USis the same language
Returns
the checker, or null when there is neither a dictionary nor a platform checker
-
getBundledLanguages
-
platform
The platform's own checker for a language, where the port has one.
Parameters
language: a language tag such asen_US, or null for the device's language
Returns
the checker, or null when the platform cannot check that language
-
addWord
Adds a word to the user dictionary, so it is spelled correctly in any case.
Parameters
word: the word
-
removeWord
Removes a word from the user dictionary.
Parameters
word: the word
-
getUserWords
-
ignore
Ignores a word, exactly as written, until
clearIgnored.Parameters
word: the word
-
clearIgnored
public void clearIgnored()Forgets every ignored word. -
isCorrect
Is the word spelled correctly? Numbers, words with digits, single letters and words in capitals are; each part of a hyphenated word is checked on its own.
Parameters
word: the word
Returns
true when it is
-
suggest
-
words
-
check
The misspelled words of a text, without suggestions (ask
suggest, which is far slower than a check).Parameters
text: the text, typically a paragraph
Returns
the issues in order
-