enchant: Access to the enchant spellchecking library — PyEnchant 3.2.2 documentation

# enchant: Access to the enchant spellchecking library

This module provides several classes for performing spell checking via the Enchant spellchecking library. For more details on Enchant, visit the project website:

> https://abiword.github.io/enchant/

Spellchecking is performed using ‘Dict’ objects, which represent a language dictionary. Their use is best demonstrated by a quick example:

    >>> import enchant
    >>> d = enchant.Dict("en_US")   # create dictionary for US English
    >>> d.check("enchant")
    True
    >>> d.check("enchnt")
    False
    >>> d.suggest("enchnt")
    ['enchant', 'enchants', 'enchanter', 'penchant', 'incant', 'enchain', 'enchanted']

Languages are identified by standard string tags such as “en” (English) and “fr” (French). Specific language dialects can be specified by including an additional code - for example, “en_AU” refers to Australian English. The later form is preferred as it is more widely supported.

To check whether a dictionary exists for a given language, the function ‘dict_exists’ is available. Dictionaries may also be created using the function ‘request_dict’.

A finer degree of control over the dictionaries and how they are created can be obtained using one or more ‘Broker’ objects. These objects are responsible for locating dictionaries for a specific language.

Note that unicode strings are expected throughout the entire API. Bytestrings should not be passed into any function.

Errors that occur in this module are reported by raising subclasses of ‘Error’.

class enchant.Broker

  Broker object for the Enchant spellchecker.

  Broker objects are responsible for locating and managing dictionaries. Unless custom functionality is required, there is no need to use Broker objects directly. The ‘enchant’ module provides a default broker object so that ‘Dict’ objects can be created directly.

  The most important methods of this class include:

  > - `dict_exists()`: check existence of a specific language dictionary

  > - `request_dict()`: obtain a dictionary for specific language

  > - `set_ordering()`: specify which dictionaries to try for a given language.

  describe()

    Return list of provider descriptions.

    This method returns a list of descriptions of each of the dictionary providers available. Each entry in the list is a ProviderDesc object.

  dict_exists(tag)

    Check availability of a dictionary.

    This method checks whether there is a dictionary available for the language specified by ‘tag’. It returns True if a dictionary is available, and False otherwise.

  get_param(name)

    Get the value of a named parameter on this broker.

    Parameters are used to provide runtime information to individual provider backends. See the method `set_param()` for more details.

    Warning

    This method does not work when using the Enchant C library version 2.0 and above

  list_dicts()

    Return list of available dictionaries.

    This method returns a list of dictionaries available to the broker. Each entry in the list is a two-tuple of the form:

    > (tag,provider)

    where <tag> is the language lag for the dictionary and <provider> is a ProviderDesc object describing the provider through which that dictionary can be obtained.

  list_languages()

    List languages for which dictionaries are available.

    This function returns a list of language tags for which a dictionary is available.

  request_dict(tag=None)

    Request a Dict object for the language specified by <tag>.

    This method constructs and returns a Dict object for the requested language. ‘tag’ should be a string of the appropriate form for specifying a language, such as “fr” (French) or “en_AU” (Australian English). The existence of a specific language can be tested using the ‘dict_exists’ method.

    If <tag> is not given or is None, an attempt is made to determine the current language in use. If this cannot be determined, Error is raised.

    Note

    this method is functionally equivalent to calling the Dict() constructor and passing in the <broker> argument.

  request_pwl_dict(pwl)

    Request a Dict object for a personal word list.

    This method behaves as ‘request_dict’ but rather than returning a dictionary for a specific language, it returns a dictionary referencing a personal word list. A personal word list is a file of custom dictionary entries, one word per line.

  set_ordering(tag, ordering)

    Set dictionary preferences for a language.

    The Enchant library supports the use of multiple dictionary programs and multiple languages. This method specifies which dictionaries the broker should prefer when dealing with a given language. ‘tag’ must be an appropriate language specification and ‘ordering’ is a string listing the dictionaries in order of preference. For example a valid ordering might be “aspell,myspell,ispell”. The value of ‘tag’ can also be set to “*” to set a default ordering for all languages for which one has not been set explicitly.

  set_param(name, value)

    Set the value of a named parameter on this broker.

    Parameters are used to provide runtime information to individual provider backends.

    Warning

    This method does not work when using the Enchant C library version 2.0 and above

class enchant.Dict(tag=None, broker=None)

  Dictionary object for the Enchant spellchecker.

  Dictionary objects are responsible for checking the spelling of words and suggesting possible corrections. Each dictionary is owned by a Broker object, but unless a new Broker has explicitly been created then this will be the ‘enchant’ module default Broker and is of little interest.

  The important methods of this class include:

  > - check(): check whether a word id spelled correctly

  > - suggest(): suggest correct spellings for a word

  > - add(): add a word to the user’s personal dictionary

  > - remove(): add a word to the user’s personal exclude list

  > - add_to_session(): add a word to the current spellcheck session

  > - store_replacement(): indicate a replacement for a given word

  Information about the dictionary is available using the following attributes:

  > - tag: the language tag of the dictionary

  > - provider: a ProviderDesc object for the dictionary provider

  add(word)

    Add a word to the user’s personal word list.

  add_to_pwl(word)

    Add a word to the user’s personal word list.

  add_to_session(word)

    Add a word to the session personal list.

  check(word)

    Check spelling of a word.

    This method takes a word in the dictionary language and returns True if it is correctly spelled, and false otherwise.

  is_added(word)

    Check whether a word is in the personal word list.

  is_removed(word)

    Check whether a word is in the personal exclude list.

  remove(word)

    Add a word to the user’s personal exclude list.

  remove_from_session(word)

    Add a word to the session exclude list.

  store_replacement(mis, cor)

    Store a replacement spelling for a miss-spelled word.

    This method makes a suggestion to the spellchecking engine that the miss-spelled word <mis> is in fact correctly spelled as <cor>. Such a suggestion will typically mean that <cor> appears early in the list of suggested spellings offered for later instances of <mis>.

  suggest(word)

    Suggest possible spellings for a word.

    This method tries to guess the correct spelling for a given word, returning the possibilities in a list.

class enchant.DictWithPWL(tag, pwl=None, pel=None, broker=None)

  Dictionary with separately-managed personal word list.

  Note

  As of version 1.4.0, enchant manages a per-user pwl and exclude list. This class is now only needed if you want to explicitly maintain a separate word list in addition to the default one.

  This class behaves as the standard Dict class, but also manages a personal word list stored in a separate file. The file must be specified at creation time by the ‘pwl’ argument to the constructor. Words added to the dictionary are automatically appended to the pwl file.

  A personal exclude list can also be managed, by passing another filename to the constructor in the optional ‘pel’ argument. If this is not given, requests to exclude words are ignored.

  If either ‘pwl’ or ‘pel’ are None, an in-memory word list is used. This will prevent calls to add() and remove() from affecting the user’s default word lists.

  The Dict object managing the PWL is available as the ‘pwl’ attribute. The Dict object managing the PEL is available as the ‘pel’ attribute.

  To create a DictWithPWL from the user’s default language, use None as the ‘tag’ argument.

  add(word)

    Add a word to the associated personal word list.

    This method adds the given word to the personal word list, and automatically saves the list to disk.

  add_to_pwl(word)

    Add a word to the associated personal word list.

    This method adds the given word to the personal word list, and automatically saves the list to disk.

  check(word)

    Check spelling of a word.

    This method takes a word in the dictionary language and returns True if it is correctly spelled, and false otherwise. It checks both the dictionary and the personal word list.

  is_added(word)

    Check whether a word is in the personal word list.

  is_removed(word)

    Check whether a word is in the personal exclude list.

  remove(word)

    Add a word to the associated exclude list.

  suggest(word)

    Suggest possible spellings for a word.

    This method tries to guess the correct spelling for a given word, returning the possibilities in a list.

class enchant.ProviderDesc(name, desc, file)

  Simple class describing an Enchant provider.

  Each provider has the following information associated with it:

  > - name: Internal provider name (e.g. “aspell”)

  > - desc: Human-readable description (e.g. “Aspell Provider”)

  > - file: Location of the library containing the provider

enchant.dict_exists(tag)

  Check availability of a dictionary.

  This method checks whether there is a dictionary available for the language specified by ‘tag’. It returns True if a dictionary is available, and False otherwise.

enchant.get_enchant_version()

  Get the version string for the underlying enchant library.

enchant.get_param(name)

  Get the value of a named parameter on this broker.

  Parameters are used to provide runtime information to individual provider backends. See the method `set_param()` for more details.

  Warning

  This method does not work when using the Enchant C library version 2.0 and above

enchant.get_user_config_dir()

  Return the path that will be used by some Enchant providers to look for custom dictionaries.

enchant.list_dicts()

  Return list of available dictionaries.

  This method returns a list of dictionaries available to the broker. Each entry in the list is a two-tuple of the form:

  > (tag,provider)

  where <tag> is the language lag for the dictionary and <provider> is a ProviderDesc object describing the provider through which that dictionary can be obtained.

enchant.list_languages()

  List languages for which dictionaries are available.

  This function returns a list of language tags for which a dictionary is available.

enchant.request_dict(tag=None)

  Request a Dict object for the language specified by <tag>.

  This method constructs and returns a Dict object for the requested language. ‘tag’ should be a string of the appropriate form for specifying a language, such as “fr” (French) or “en_AU” (Australian English). The existence of a specific language can be tested using the ‘dict_exists’ method.

  If <tag> is not given or is None, an attempt is made to determine the current language in use. If this cannot be determined, Error is raised.

  Note

  this method is functionally equivalent to calling the Dict() constructor and passing in the <broker> argument.

enchant.request_pwl_dict(pwl)

  Request a Dict object for a personal word list.

  This method behaves as ‘request_dict’ but rather than returning a dictionary for a specific language, it returns a dictionary referencing a personal word list. A personal word list is a file of custom dictionary entries, one word per line.

enchant.set_param(name, value)

  Set the value of a named parameter on this broker.

  Parameters are used to provide runtime information to individual provider backends.

  Warning

  This method does not work when using the Enchant C library version 2.0 and above

enchant.set_prefix_dir(path)

  Set the prefix used by the Enchant library to find its plugins

  Called automatically when the Python library is imported when required.