任何人都可以用我的拼写检查代码帮助我吗?

时间:2012-11-26 21:36:48

标签: python python-2.7

这就是我所拥有的,评论描述了我想要做的事情

文字文件中有一些单词拼写错误,测试文本文件也用于拼写检查。

e.g。 >>>拼写检查( “test1.txt的”)     {'exercsie':1,'finised':1}

from string import ascii_uppercase, ascii_lowercase
def spellCheck(textFileName):

    # Use the open method to open the words file.
    # Read the list of words into a list named wordsList
    # Close the file

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

    # Open the file whos name was provided as the textFileName variable
    # Read the text from the file into a list called wordsToCheck
    # Close the file

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

    for i in range(0,len(wordsList)): wordsList[i]=wordsList[i].replace("\n","")
    for i in range(0,len(wordsToCheck)): wordsToCheck[i]=wordsToCheck[i].replace("\n","")


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


    # Loop through the wordsToCheck list
    # Change the current word into lower case
    # If the current word does not exist in the wordsList then
            # Check if the word already exists in the spellingErrors dictionary
                    # If it does not exist than add it to the dictionary with the initial value of 1.
                    # If it does exist in the dictionary then increase the value by 1

    # Return the dictionary
    char_low = ascii_lowercase
    char_up = ascii_uppercase
    for char in wordsToCheck[0]:
       if char in wordsToCheck[0] in char_up:
            result.append(char_low)
    for i in wordsToCheck[0]:
       if wordsToCheck[0] not in wordsList:
            if wordsToCheck[0] in dict(wordsList):
                    dict(wordsList) + 1
            elif wordsToCheck[0] not in dict(wordsList):
                    dict(wordsList) + wordsToCheck[0]
                    dict(wordsList) + 1
    return dict(wordsList)

我的代码返回错误

追踪(最近一次通话):   文件“”,第1行,in     拼写检查( “test1.txt的”)   在拼写检查中输入“J:\ python \ SpellCheck(1).py”,第36行     spellingErrors = dict(wordsList) ValueError:字典更新序列元素#0的长度为5; 2是必需的

那么有人可以帮助我吗?

1 个答案:

答案 0 :(得分:8)

我申请了PEP-8并重新编写了unpythonic代码。

import collections

def spell_check(text_file_name):
    # dictionary for word counting
    spelling_errors = collections.defaultdict(int)

    # put all possible words in a set
    with open("words.txt") as words_file:
        word_pool = {word.strip().lower() for word in words_file}

    # check words
    with open(text_file_name) as text_file:
        for word in (word.strip().lower() for word in text_file):
            if not word in word_pool:
                spelling_errors[word] += 1

    return spelling_errors

您可能希望了解with statementdefaultdict

您的代码包含ascii_uppercaseascii_lowercase尖叫:Read the tutorial并了解基本知识。该代码是“我不知道我在做什么,但无论如何我都这样做。”。

关于旧代码的更多解释

您使用

char_low = ascii_lowercase

不需要char_low,因为您从不操纵该值。只需使用原始ascii_lowercase即可。然后是代码的以下部分:

for char in wordsToCheck[0]:
    if char in wordsToCheck[0] in char_up:
        result.append(char_low)

我不太确定你在这里尝试做什么。您似乎想将列表中的单词转换为小写。实际上,如果该代码将运行 - 它不会运行 - 您将为列表中单词的每个大写字符附加整个小写字母到result。不过,您不会在后面的代码中使用result,因此不会造成任何伤害。在循环之前添加print wordsToCheck[0]或在循环中添加print char以查看其中发生的情况会很容易。

代码的最后一部分只是一团糟。您只访问每个列表中的第一个单词 - 可能是因为您不知道该列表是什么样的。这是通过试错法编码。尝试使用知识进行编码。

你真的不知道dict做了什么以及如何使用它。我可以在这里解释一下,但是www.python.org上有一个很棒的教程,你可能想先阅读,特别是the chapter dealing with dictionaries。如果你研究这些解释但仍然不明白,请随时回答一个有关此问题的新问题。

我使用了defaultdict而不是标准字典,因为它让这里的生活更轻松。如果您将spelling errors定义为dict,则我的代码的一部分必须更改为

if not word in word_pool:
    if not word in spelling_errors:
        spelling_errors[word] = 1
    else:
        spelling_errors[word] += 1
顺便说一下,我写的代码没有任何问题。我得到一个字典,其中缺少单词(小写)作为键,并将该单词的计数作为相应的值。