Monday, November 18, 2024
Google search engine
HomeLanguagesenchant.request_pwl_dict() in Python

enchant.request_pwl_dict() in Python

Enchant is a module in python which is used to check the spelling of a word, gives suggestions to correct words. Also, gives antonym and synonym of words. It checks whether a word exists in dictionary or not.

enchant.request_pwl_dict()

enchant.request_pwl_dict() is an inbuilt method of enchant module. It is used to build a custom dictionary also known as Personal Word List(PSL).

Syntax : enchant.request_pwl_dict(text_file) Parameter : text_file : the path of the text file which contains the words to be included, one word per line. Returns : a Dict object

Example 1: The contents of the sample file “PWL.txt” are:

qwerty jabba gfg

Python3




# import the enchant module
import enchant
 
# the path of the text file
file_path = "PWL.txt"
 
# instantiating the enchant dictionary
# with request_pwl_dict()
pwl = enchant.request_pwl_dict(file_path)
 
# checking whether the words are
# in the new dictionary
print(pwl.check("gfg"))


Output : 

True

Example 2: Adding new words to the PWL dictionary using add(). 

Python3




# import the enchant module
import enchant
 
# the path of the text file
file_path = "PWL.txt"
 
# printing the contents of the file
print("File contents:")
with open(file_path, 'r') as f:
    print(f.read())
 
# instantiating the enchant dictionary
# with request_pwl_dict()
pwl = enchant.request_pwl_dict(file_path)
 
# the word to be added
new_word = "asd"
 
# checking whether the word is
# in the dictionary
if pwl.check(new_word):
    print("\nThe word "+ new_word + " exists in the dictionary")
else:
    print("\nThe word "+ new_word + " does not exists in the dictionary")
 
# adding the word to the dictionary
# using add()
pwl.add("asd")
 
# printing the contents of the file
# after adding the new word
print("\nFile contents:")
with open(file_path, 'r') as f:
    print(f.read())
 
# checking for whether the word is
# in the dictionary
if pwl.check(new_word):
    print("The word "+ new_word + " exists in the dictionary")
else:
    print("The word "+ new_word + " does not exists in the dictionary")


Output : 

File contents:
qwerty
jabba
gfg

The word asd does not exists in the dictionary

File contents:
qwerty
jabba
gfg
asd

The word asd exists in the dictionary

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments