Class SpellChecker

java.lang.Object
com.codename1.spellcheck.SpellChecker

public abstract class SpellChecker extends Object

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
    Modifier and Type
    Field
    Description
    static final String
    The resource name prefix of the dictionaries the build adds: cn1dict_en_US.aff and cn1dict_en_US.dic for en_US.
    static final String
    The resource listing the languages of the dictionaries the build added, one per line.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

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

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DICTIONARY_PREFIX

      public static final String DICTIONARY_PREFIX
      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.
      See Also:
    • LANGUAGES_RESOURCE

      public static final String 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

      protected abstract boolean lookup(String word)

      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

      protected abstract List<String> suggestions(String word, int max)

      Suggestions from the underlying dictionary.

      Parameters
      • word: the misspelled word

      • max: the most to return

      Returns

      the suggestions, best first

    • create

      public static SpellChecker create(HunspellDictionary d)

      A checker over a Hunspell dictionary.

      Parameters
      • d: 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: 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: 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: the word
    • removeWord

      public void removeWord(String word)

      Removes a word from the user dictionary.

      Parameters
      • word: 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: 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: 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: the word

      • max: 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: 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: the text, typically a paragraph
      Returns

      the issues in order