如何完成拼写检查代码?

2024-06-01 02:02:28 发布

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

我需要创建一个函数来读取文本文件并创建拼写错误单词的字典。 这就是我目前所拥有的

def spellCheck(textFileName):

    file=open("words.txt","r")
    wordsList = file.read()
    print wordsList
    file.close()

    file=open(textFileName, "r")
    wordsToCheck = file.read()
    print wordsToCheck
    file.close()

    # The next line creates the dictionary 
    # This dictionary will have the word that has been spelt wrong as the key and the number of times it has been spelt wrong as the value
    spellingErrors = dict= {'words', wordsToCheck}

我需要:

^{pr2}$

Tags: theclosereaddictionaryopenfilehaswords
2条回答

这非常简单,而且非常缓慢:

wordsList = wordsList.lower().split()
for word in wordsToCheck.lower().split():
    if not word in wordsList:
        if word not in spellingErrors:
            spellingErrors[word]=0
        spellingErrors[word] += 1
# load the dictonary
dict_text = open('words.txt','r').read().split()
# convert dictionary to lowercase set() for easy access
dictionary = set( [i.lower() for i in dict_text] )

# load a text
words_to_check = open( text_file_name, 'r').read().split()
# and check it
for word in words_to_check :
    if word.lower() not in dictionary :
        print 'misspelled:', word

相关问题 更多 >