我有一个我正在处理的程序,它接受一个输入并检查它是否与文件内的字典拼写正确。但是,我想回复一个或两个人的意见。有关如何做到这一点的任何建议?我找到了一些可以做到的模块,但是没有找到文件中的特定字典。任何帮助表示赞赏!!
以下是我现在所拥有的:
def getDictionary():
theDictionary = open("theDictionary.txt", "r")
dictionaryList = []
for eachLine in theDictionary:
splitLines = eachLine.split()
dictionaryList.append(splitLines[0])
theDictionary.close()
return dictionaryList
def spellChecker(theFile, theDictionary):
lowerItems = theFile.lower()
wordList = lowerItems.split()
wrongList = []
for item in wordList:
if item not in theDictionary:
result = False
wrongList.append(item)
else:
result = True
wrongItem = ""
return (result, wrongList)
def main():
theDictionary = getDictionary()
theText = getFile()
theInput = input("Input some words here:")
result, wrongList=spellChecker(theInput,theDictionary)
if result:
print("There are no spelling errors in the sentence! Hooray!")
else:
if len(wrongList) == 1:
print('There is a spelling error in the sentence! The word that is wrong is "' + str(wrongList) + '".')
elif len(wrongList) > 1:
print('There are some spelling errors in the sentence! The words that are wrong are"' + str(wrongList) + '".')
main()
答案 0 :(得分:3)
您可能需要查看标准库中的difflib模块。它允许你进行近似的字符串匹配,这似乎是你想要的。
如果你的字典在文件中是否真的没关系,因为你无论如何都要将它加载到列表中。也许看看上述模块中的get_close_matches()方法。