Create a spelling checker using Enchant in Python

In Python, Enchant is a built-in module. It helps in checking the spelling of the word, suggesting the correct words related to the misspelled word. MySpell, ispell, aspell are a few of the important packages for checking the spelling of the words. We can also check the synonyms and antonyms of a word through this module.

Installation of Enchant module:

For installing Enchant module type on the command prompt:

!pip install pyenchant

Through this, the enchant module gets installed.

List of languages enchant supports:

# import enchant module
import enchant 
  
# list the languages 
print(enchant.list_languages())
Output:
['en_BW', 'en_AU', 'en_BZ', 'en_GB', 'en_JM', 'en_DK', 'en_HK', 'en_GH', 'en_US', 'en_ZA', 'en_ZW',
 'en_SG', 'en_NZ', 'en_BS', 'en_AG', 'en_PH', 'en_IE', 'en_NA', 'en_TT', 'en_IN', 'en_NG', 'en_CA']

The dictionaries of these languages are available in the enchant module.

Check the spelling of the word using Enchant in Python

With the help of the check() method, the dictionary will check that does the given word don exist or not. If the given word exists in the dictionary then the method will return ‘True’ otherwise ‘False’.

Example1:

# dictionary of a particular language
d = enchant.Dict('en_US')

#check the spelling
print(d.check('color'))
print(d.check('colour'))
Output:
True
False

The word ‘color’ exists in the dictionary ‘en_US’ so it returned ‘True’ and for the word ‘colour’ it returned ‘False’ as it doesn’t exist the dictionary.

Example2:

# dictionary of a particular language
d = enchant.Dict('en_US')
#check the spelling
print(d.check('oestrogen'))

d = enchant.Dict('en_AU')
# check the spelling 
print(d.check('oestrogen'))
Output:
False
True

Here as in American language, the correct spelling is ‘estrogen’ not ‘oestrogen’ so that’s why when it is checked in ‘en_US’ the method returned ‘False’ but when the word ‘oestrogen’ is checked in ‘en_AU’ dictionary it returned ‘True’.

Spelling Checker:

Creating a function to check the correctly and wrongly spelled words in a list passed to the function. The correctly spelled words append in the correct word list otherwise in the wrong word list. Here the check() method will check that the word exists in the dictionary ‘en_US’ or not.

# import the enchant module
import enchant

# function to check the spelling of each element of the list
def enchant_func(l):
    
    wrong = []
    correct = []
    d = enchant.Dict('en_US')
    
    #check the individual element in list
    for i in range(len(l)):
        # if word doesn't exist
        if d.check(str(l[i])) == False:
            wrong.append(l[i])
        else:
            correct.append(l[i])
    return (f"list of wrong words:{wrong}\nlist of correct word:{correct}")

list_enchant = ['code', 'helo', 'colour', 'speed', 'speedy']
print(enchant_func(list_enchant))
Output:
list of wrong words:['helo', 'colour']
list of correct word:['code', 'speed', 'speedy']

Also read: Solve Word Wrap Problem in Python

Leave a Reply