当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python enchant.request_pwl_dict()用法及代码示例


Enchant是python中的一个模块,用于检查单词的拼写,并提供纠正单词的建议。另外,给出单词的反义词和同义词。它检查字典中是否存在单词。

enchant .request_pwl_dict()

enchant.request_pwl_dict()是一种内置方法enchant模块。它用于构建自定义词典,也称为个人单词列表(PSL)。

用法: enchant.request_pwl_dict(text_file) 

参数:
text_file:包含要包含的单词的文本文件的路径,每行一个单词。

返回:Dict对象



范例1:
样本文件“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:使用将新单词添加到PWL词典中add()

# 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



相关用法


注:本文由纯净天空筛选整理自Yash_R大神的英文原创作品 enchant.request_pwl_dict() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。