Python拼写检查及建议

2024-06-10 23:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个程序来检查它的输入是否正确。不过,我想回一两个建议,以及这个人的意思。有什么建议吗?我发现了一些模块可以做到这一点,但不是针对一个特定的字典从一个文件。感谢任何帮助!!在

我现在的情况是:

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()

Tags: theinifisdefresultitemare
1条回答
网友
1楼 · 发布于 2024-06-10 23:31:23

您可能想看看标准库中的difflib模块。它允许您进行近似的字符串匹配,这似乎是您想要的。在

你的字典是否在文件中并不重要,因为不管怎样,你都要把它加载到一个列表中。也许可以看看上述模块中的get_close_matches()方法。在

相关问题 更多 >