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

public final class HunspellDictionary

  1. Object
  2. HunspellDictionary

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"));

Constructors

public HunspellDictionary(String aff, String dic)Reads a dictionary from the text of its two files.

Methods

public static HunspellDictionary fromBytes(byte[] aff, byte[] dic)Reads a dictionary from the raw bytes of its two files, decoded by the SET line.
public boolean lookup(String w)Is this exact form, with its case as given, in the dictionary or derived by its rules?
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.
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.
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.
public int size()The number of words in the word list, before affixes.

Inherited methods

Constructor details

HunspellDictionary

public HunspellDictionary(String aff, String dic)
Reads a dictionary from the text of its two files.

Parameters

aff String
the affix file
dic String
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 byte[]
the affix file
dic byte[]
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 String
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 Set<String>
words that run into the next word, or null
trailing Set<String>
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 String
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 String
the misspelled word
max int
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