Get similar words suggestion using Enchant in Python

PythonProgrammingServer Side Programming

When we write something, many times happen with us that we misspelled some words. To overcome this problem, Python provides Enchant module. This is mainly used to check the spelling of words and suggest corrections for words that are miss-spelled. It is also used in many popular spellchecking packages to perform this task, including ispell, aspell and MySpell. It is very flexible at handling multiple dictionaries and multiple languages.

For installing this, we use this command line in command prompt.

pip install pyenchant


>>> import enchant
>>> d.suggest("prfomnc")
Output::['prominence', 'performance', 'preform', 'Provence', 'preferment', 'proforma']

Example code

import enchant, difflib
d = enchant.Dict("en_US")
dict,max = {},0
a = set(d.suggest(my_word))
for b in a:
   tmp = difflib.SequenceMatcher(None, my_word, b).ratio();
   dict[tmp] = b
   if tmp > max:
      max = tmp
print (dict[max])



Dict object is the most important object in the PyEnchant module, which represents a dictionary and these objects are used to check the spelling of words and to get suggestions for misspelled words.

>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
>>> d.check("Helo")

Dictionaries are also created using a LANGUAGE TAG which specifies the language to be checked.

>>> d = enchant.Dict()
>>> d.tag
>>> print d.tag

There are different functions in the enchant module which can be used to deal with dictionaries.

dict_exists − To check whether a Dict is available for a given language.

request_dict − To construct and return a new Dict object.

list_languages − Display the list of the languages for which Dicts are available.

>>> enchant.dict_exists("fake")
>>> enchant.dict_exists("en_US")
>>> d = enchant.request_dict("en_US")
>>> d
<enchant.Dict object at 0x2aaaabdffa50>
>>> enchant.list_languages()
['en', 'en_CA', 'en_GB', 'en_US', 'eo', 'fr', 'fr_CH', 'fr_FR']
Updated on 30-Jul-2019 22:30:23