Enchant是python中的一個模塊,用於檢查單詞的拚寫,並提供糾正單詞的建議。另外,給出單詞的反義詞和同義詞。它檢查字典中是否存在單詞。
enchant .request_pwl_dict()
enchant.request_pwl_dict()
是一種內置方法enchant
模塊。它用於構建自定義詞典,也稱為個人單詞列表(PSL)。
用法: enchant.request_pwl_dict(text_file)
參數:
text_file:包含要包含的單詞的文本文件的路徑,每行一個單詞。
返回:Dict對象
範例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:使用將新單詞添加到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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。