public final class Enchant extends Glib
Enchant is not itself a spell checking library; it is, rather a facade to various common spelling mechanisms. It provides a simple and sufficient API for doing spelling operations. Which actual back-end provider will be employed depends on the system and user "ordering" files.
Enchant is straight-forward to use. For possibly misspelled
word
, you can do
Enchant.init(); dict = Enchant.requestDictionary("en_CA"); if (dict.check(word)) { return "Spelled correctly!"; } else { possibles = dict.suggest(word); str.append("The word "); str.append(word); str.append(" was misspelled. You could correct it with one of:\n" for (i = 0; i < possibles.length; i++) { str.append(possibles[i]); str.append('\n'); } return str.toString(); }
Modifier and Type | Method and Description |
---|---|
static boolean |
existsDictionary(String lang)
Does a dictionary exist for the given "language"?
|
static void |
init() |
static String[] |
listDictionaries()
Get a list of available dictionaries as known to Enchant.
|
static Dictionary |
requestDictionary(String lang)
Get a Dictionary for the specified language.
|
static Dictionary |
requestPersonalWordList(String filename)
Get a Dictionary for the specified personal word list.
|
formatSize, formatSize, getProgramName, getRealName, getSystemConfigDirs, getSystemDataDirs, getUserCacheDir, getUserConfigDir, getUserDataDir, getUserName, getUserSpecialDir, idleAdd, markupEscapeText, reloadUserSpecialDirsCache, setProgramName
public static boolean existsDictionary(String lang)
Languages are indicated in a locale-like form; while you can use just
the language code en
, specifying a specific language
variant such as "en_UK"
or "fr_CA"
is
preferred.
public static void init()
public static String[] listDictionaries()
en en_AU en_CA en_GB en_US en_ZA es fr_BE fr_CA fr_CH fr_FR fr_LU fr_MC(that was the list on a computer with English, French, and Spanish dictionaries installed via packages
language-support-en
,
language-support-fr
, language-support-es
respectively on, in this case, Ubuntu Linux).
You don't necessarily need to callt this function. You can test for the
existance of a dictionary with Enchant.existsDictionary()
, or even just get on directly with loading
a dictionary with requestDictionary()
.
If you are using the results of this funciton to create a list in a
user interface, you'll probably want to present the language and
country names translated. Use
Internationalization.translateLanguageName()
and
Internationalization.translateCountryName()
although you'll have to
look up the proper ISO 639 and ISO 3166 names in
/usr/share/xml/iso-codes/iso_{639,3166}.xml
first.
public static Dictionary requestDictionary(String lang)
See existsDictionary()
for
discussion of valid language values. You probably want to call that if
you're considering user input values.
Returns null
if no suitable dictionary was found.
public static Dictionary requestPersonalWordList(String filename) throws FileNotFoundException
Word lists are simple files with one word per line. By creating a Dictionary of a personal word list you can add words to a file that is independent of a normal spelling engine back-end.
FileNotFoundException