Check if a Word is in English Dictionary Python | On this page, we will discuss how to check for a word whether it is valid or not i.e. we will check whether the word is in the English dictionary or not. Also see:- Remove First Character from String
If the word is present in English then the code returns ‘True’ or else it returns ‘False’. To do this there is a built-in module in python called enchant, this module is used to check the spelling of the words if the words given are wrong then give the suggestion according to the English dictionary.
To check the word whether is present in English or not, we use the check() function, and for the suggestions for the correct word, we can use suggest().
Import enchant Module in Python
Before writing the code we should install the enchant module, else while running the code we will get:- ModuleNotFoundError: No module named ‘enchant’.
We can install enchant module as follows:-
pip install --user pyenchant
Check if a Word is in English Dictionary Python using enchant
Let us check whether the word is in the English dictionary or not by taking the user input.
Check if a Word is in English Dictionary Python using check() of enchant Module
import enchant dict = enchant.Dict("en_US") word = input("Enter the word: ") print(dict.check(word))
Enter the word: Hello
A scenario where the output is false.
Enter the word: KnowProgram
Observe the below explanation to understand the code in more detail:-
Step-1: Import enchant. The enchant is a module that checks for the spelling therefore we need to import it.
Step-2: Take input from the user from the input() method if needed print some statement we have asked to “Enter the word: ”
Step-3: Then by using the check method in enchant check whether the word is in the English dictionary or not, if the word is in the English dictionary it returns true, or else it returns false.
The string “Hello” is there in the English dictionary and hence the code return “True” but the word “KnowProgram” is not there and hence it returns False.
Check if a Word is in English Dictionary Python using suggest() method
Program to check if a Word is in English Dictionary Python using suggest() Method of enchant module.
import enchant dict = enchant.Dict("en_US") word = input("Enter the word: ") print(dict.suggest(word))
Enter the word: Jav
[‘Av’, ‘Java’, ‘Jan’, ‘Lav’, ‘Jap’, ‘Jay’, ‘J av’, ‘Jab’, ‘Jar’, ‘Jag’, ‘Jam’, ‘Jaw’]
Enter the word: Payth
Enter the word: Hi
[‘HI’, ‘Ho’, ‘H’, ‘I’, ‘Hui’, ‘He’, ‘Ii’, ‘Ha’, ‘Ti’, ‘Oi’, ‘Hg’, ‘Mi’, ‘Pi’, ‘Hi’, ‘Bi’]
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!