【问题标题】:Python Spell Check With Suggestions带有建议的 Python 拼写检查
【发布时间】:2014-11-01 08:01:15
【问题描述】:

我正在开发一个程序,它接受输入并检查它是否与文件中的字典拼写正确。但是,我想回复一两个建议,以及这个人的意思。关于如何做到这一点的任何建议?我找到了一些可以做到这一点的模块,但不适用于文件中的特定字典。任何帮助表示赞赏!

这是我现在拥有的:

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

【问题讨论】:

    标签: python spell-checking


    【解决方案1】:

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

    你的字典是否在文件中并不重要,因为无论如何你都是将它加载到一个列表中。不妨看看上述模块中的get_close_matches() 方法。

    【讨论】:

      猜你喜欢
      • 2019-04-11
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 2014-12-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      相关资源
      最近更新 更多