Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public abstract class SpellChecker

  1. Object
  2. 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

protected abstract boolean lookup(String word)Is this word, already trimmed and not empty, in the underlying dictionary?
protected abstract List<String> suggestions(String word, int max)Suggestions from the underlying dictionary.
public static SpellChecker create(HunspellDictionary d)A checker over a Hunspell dictionary.
public static SpellChecker forLanguage(String language)A checker for a language: the dictionary the build added for it, or else the platform’s own checker.
public static List<String> getBundledLanguages()The languages of the dictionaries the build added.
public static SpellChecker platform(String language)The platform’s own checker for a language, where the port has one.
public void addWord(String word)Adds a word to the user dictionary, so it is spelled correctly in any case.
public void removeWord(String word)Removes a word from the user dictionary.
public Set<String> getUserWords()The words of the user dictionary, in lower case.
public void ignore(String word)Ignores a word, exactly as written, until clearIgnored.
public void clearIgnored()Forgets every ignored word.
public boolean isCorrect(String word)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.
public List<String> suggest(String word, int max)Suggestions for a misspelled word, best first, user dictionary words included.
public static List<int[]> words(String text)The words of a text as {start, end} pairs, apostrophes and hyphens at either end left out.
public List<ProofIssue> check(String text)The misspelled words of a text, without suggestions (ask suggest, which is far slower than a check).

Inherited methods

Field details

DICTIONARY_PREFIX

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. Names are flat because some ports flatten resource folders.

LANGUAGES_RESOURCE

public static final String LANGUAGES_RESOURCE = "/cn1dict_languages.txt"
The resource listing the languages of the dictionaries the build added, one per line.

Constructor details

SpellChecker

public SpellChecker()

Method details

lookup

protected abstract boolean lookup(String word)
Is this word, already trimmed and not empty, in the underlying dictionary?

Parameters

word String
the word as written

Returns

true when it is spelled correctly

suggestions

protected abstract List<String> suggestions(String word, int max)
Suggestions from the underlying dictionary.

Parameters

word String
the misspelled word
max int
the most to return

Returns

the suggestions, best first

create

public static SpellChecker create(HunspellDictionary d)
A checker over a Hunspell dictionary.

Parameters

d HunspellDictionary
the dictionary

Returns

the checker

forLanguage

public static SpellChecker forLanguage(String language)
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 String
a language tag such as en_US; en-US is the same language

Returns

the checker, or null when there is neither a dictionary nor a platform checker

getBundledLanguages

public static List<String> getBundledLanguages()
The languages of the dictionaries the build added.

Returns

the language tags, empty when there are none

platform

public static SpellChecker platform(String language)
The platform’s own checker for a language, where the port has one.

Parameters

language String
a language tag such as en_US, or null for the device’s language

Returns

the checker, or null when the platform cannot check that language

addWord

public void addWord(String word)
Adds a word to the user dictionary, so it is spelled correctly in any case.

Parameters

word String
the word

removeWord

public void removeWord(String word)
Removes a word from the user dictionary.

Parameters

word String
the word

getUserWords

public Set<String> getUserWords()
The words of the user dictionary, in lower case.

Returns

a copy of the set

ignore

public void ignore(String word)
Ignores a word, exactly as written, until clearIgnored.

Parameters

word String
the word

clearIgnored

public void clearIgnored()
Forgets every ignored word.

isCorrect

public boolean isCorrect(String word)
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 String
the word

Returns

true when it is

suggest

public List<String> suggest(String word, int max)
Suggestions for a misspelled word, best first, user dictionary words included.

Parameters

word String
the word
max int
the most to return

Returns

the suggestions

words

public static List<int[]> words(String text)
The words of a text as {start, end} pairs, apostrophes and hyphens at either end left out.

Parameters

text String
the text

Returns

the word ranges in order

check

public List<ProofIssue> check(String text)
The misspelled words of a text, without suggestions (ask suggest, which is far slower than a check).

Parameters

text String
the text, typically a paragraph

Returns

the issues in order