Class HunspellDictionary

java.lang.Object
com.codename1.spellcheck.HunspellDictionary

public final class HunspellDictionary extends Object

A Hunspell dictionary -- an .aff affix file and a .dic word list -- read for spelling checks and suggestions. Hunspell dictionaries exist for most languages under open licenses (LibreOffice ships them); none is bundled with Codename One, so an application supplies the pair, typically from its resources.

Supported: SET (UTF-8, ISO8859-1, ISO8859-2), FLAG (character, long, num), AF aliases, PFX/SFX rules with strip, add, condition and cross products, continuation classes (two suffixes in a row), FORBIDDENWORD, NEEDAFFIX, ONLYINCOMPOUND, compounding by COMPOUNDFLAG/COMPOUNDBEGIN/COMPOUNDMIDDLE/COMPOUNDEND with COMPOUNDMIN and COMPOUNDWORDMAX, ICONV/OCONV, TRY, MAP and REP. Words that need COMPOUNDRULE or the CHECKCOMPOUND* restrictions are reported as misspelled.

// the .aff and .dic files of a Hunspell dictionary, here as two tiny strings
byte[] aff = "SET UTF-8\nSFX S Y 1\nSFX S 0 s .\n".getBytes();
byte[] dic = "2\ncat/S\ndog/S\n".getBytes();
HunspellDictionary d = HunspellDictionary.fromBytes(aff, dic);
SpellChecker spell = SpellChecker.create(d);
Log.p("cats: " + spell.isCorrect("cats") + ", cots: " + spell.isCorrect("cots"));
  • Constructor Summary

    Constructors
    Constructor
    Description
    Reads a dictionary from the text of its two files.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    check(String word)
    Is the word correct? A capitalized or all capitals word is also accepted in lower case (sentence starts, headings), but not the reverse: a name listed capitalized is wrong in lower case.
    fromBytes(byte[] aff, byte[] dic)
    Reads a dictionary from the raw bytes of its two files, decoded by the SET line.
    boolean
    Is this exact form, with its case as given, in the dictionary or derived by its rules?
    void
    setSplitWords(Set<String> leading, Set<String> trailing)
    Short words that often run into the next word (leading) or the one before (trailing): a missing space next to one ranks before other splits in suggest.
    int
    The number of words in the word list, before affixes.
    suggest(String word, int max)
    Suggestions for a misspelled word, best first: a case change, MAP slips (a missing diacritic first), REP confusions, a missing space, one typo with the TRY letters, and when those find too few, the dictionary words that start like it within two typos.

    Methods inherited from class Object

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

    • HunspellDictionary

      public HunspellDictionary(String aff, String dic)

      Reads a dictionary from the text of its two files.

      Parameters
      • aff: the affix file

      • dic: the word list

  • Method Details

    • fromBytes

      public static HunspellDictionary fromBytes(byte[] aff, byte[] dic)

      Reads a dictionary from the raw bytes of its two files, decoded by the SET line.

      Parameters
      • aff: the affix file

      • dic: the word list

      Returns

      the dictionary

    • lookup

      public boolean lookup(String w)

      Is this exact form, with its case as given, in the dictionary or derived by its rules?

      Parameters
      • w: the word
      Returns

      true when it is

    • setSplitWords

      public void setSplitWords(Set<String> leading, Set<String> trailing)

      Short words that often run into the next word (leading) or the one before (trailing): a missing space next to one ranks before other splits in suggest.

      Parameters
      • leading: words that run into the next word, or null

      • trailing: words that run into the previous one, or null

    • check

      public boolean check(String word)

      Is the word correct? A capitalized or all capitals word is also accepted in lower case (sentence starts, headings), but not the reverse: a name listed capitalized is wrong in lower case.

      Parameters
      • word: the word
      Returns

      true when it is spelled correctly

    • suggest

      public List<String> suggest(String word, int max)

      Suggestions for a misspelled word, best first: a case change, MAP slips (a missing diacritic first), REP confusions, a missing space, one typo with the TRY letters, and when those find too few, the dictionary words that start like it within two typos.

      Parameters
      • word: the misspelled word

      • max: the most suggestions to return

      Returns

      the suggestions, in the case the word was written in

    • size

      public int size()

      The number of words in the word list, before affixes.

      Returns

      the root count