Get similar words suggestion using Enchant in Python


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

Example

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

Example code

import enchant, difflib
d = enchant.Dict("en_US")
my_word="prfomnc"
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])

Output

performance

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")
True
>>> d.check("Helo")
False
>>>

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

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

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")
False
>>> enchant.dict_exists("en_US")
True
>>> 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

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements