Python enchant.DictWithPWL() Function

Enchant Module in Python:

Enchant is a Python module that checks a word’s spelling and provides suggestions for correcting it. The antonyms and synonyms of the words are also provided. It determines whether or not a word is in the dictionary.

The Enchant module is also used to check the spelling of words.

Python enchant.DictWithPWL() Function:

enchant. DictWithPWL() is a built-in method of the enchant module. It is used to combine a language dictionary and a custom dictionary, also known as a Personal Word List (PSL).

Syntax:

enchant.DictWithPWL(tag, textFile)

Parameters

tag: It is the code of the language dictionary

textFile: It is the path to the text file given as input containing the words to be included, one word per line.

Return Value:

A Dictionary(Dict) object is returned by the DictWithPWL() function of the enchant module

Let us take the “sampletextfile.txt” as an example that has the following content:

NOTE: The text file should contain one word per line

sampletextfile.txt:

welcome
Hello 
all

enchant.DictWithPWL() Function in Python

Approach:

  • Import enchant module using the import keyword
  • Pass some random language code to the Dict() function of the enchant module to create a dictionary for the language given.
  • Here we create a dictionary for en_US language
  • Give some random word to be searched as static input and store it in a variable.
  • Check if the above-given word present(exists) in the dictionary using the check() function and the if conditional statement
  • If it is true, then print the “The given word is present in the dictionary”
  • Else print “The given word is NOT present in the dictionary”
  • Give the text file path as static input and store it in another variable.
  • Pass the language code, above text file as arguments to the DictWithPWL() function to instantiate the enchant dictionary
  • Check if the above given word present(exists) in the new(enchant)dictionary using the check() function and if conditional statement
  • If it is true, then print the “The given word is present in the Enchant dictionary”
  • Else print “The given word is NOT present in the Enchant dictionary”.
  • The Exit of the Program.

Below is the implementation:

# Import enchant module using the import keyword
import enchant

# Pass some random language code to the Dict() function of the enchant module
# to create dictionary for the language given.
# Here we create dictionary for en_US language
dictionry  = enchant.Dict("en_US")

# Give some random word to be searched as static input and store it in a variable.
gvn_word = "welcome"

# Check if the above given word present(exists) in the dictionary using the check() function 
# and if conditional statement
if dictionry.check(gvn_word):
    # If it is true, then print the "The given word is present in the dictionary"
    print("The given word{", gvn_word, "} is present the dictionary")
else:
    # Else print "The given word is NOT present in the dictionary"
    print("The given word{", gvn_word, "} is NOT present the dictionary")

# Give the text file path as static input and store it in another variable.
Textfilepath = r"C:\Users\cirus\Desktop\sampletextfile.txt"

# Pass the language code, above text file as arguments to the DictWithPWL() function to 
# instantiate the enchant dictionary
dictionry = enchant.DictWithPWL("en_US", Textfilepath)

# Check if the above given word present(exists) in the new(enchant)dictionary using the check() function 
# and if conditional statement
if dictionry.check(gvn_word):
    # If it is true, then print the "The given word is present in the Enchant dictionary"
    print("The given word{", gvn_word, "} is present in the Enchant dictionary")
else:
    # Else print "The given word is NOT present in the Enchant dictionary"
    print("The given word{", gvn_word, "} is NOT present in the Enchant dictionary")

Output:

The given word{ welcome } is present in the dictionary
The given word{ welcome } is present in the Enchant dictionary