Python中的 enchant.request_pwl_dict()
Enchant
是Python中的一个模块,用于检查单词的拼写,给出正确单词的建议。此外,给出单词的反义词和同义词。它检查字典中是否存在单词。
附魔.request_pwl_dict()
附魔enchant.request_pwl_dict()
是enchant
模块的内置方法。它用于构建自定义词典,也称为个人词表 (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
示例 1:
样本文件“PWL.txt”的内容是:
样本文件“PWL.txt”的内容是:
qwerty
jabba
gfg
# 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"))
输出 :
True
示例 2:使用add()
将新词添加到 PWL 字典中。
# 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")
# addin 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")
输出 :
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
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。