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

use*_*493 -3 python python-2.7

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

在文本文件中放置了一些单词拼写错误的单词以及测试文本文件以及用于拼写检查的单词.

例如>>> spellCheck("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)
Run Code Online (Sandbox Code Playgroud)

我的代码返回一个错误

回溯(最近一次调用最后一次):文件"",第1行,在spellCheck("test1.txt")文件"J:\ python\SpellCheck(1).py",第36行,拼写检查拼写错误= dict(wordsList) ValueError:字典更新序列元素#0的长度为5; 2是必需的

所以有人可以帮助我吗?

Mat*_*ias 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
Run Code Online (Sandbox Code Playgroud)

您可能想要阅读有关with语句defaultdict的信息.

您的代码与ascii_uppercaseascii_lowercase尖叫声:阅读教程和学习的基础知识.该代码是"我不知道我在做什么,但无论如何我都这样做"的集合.

有关旧代码的更多解释:

你用

char_low = ascii_lowercase
Run Code Online (Sandbox Code Playgroud)

没有必要char_low因为你从不操纵那个价值.只需使用原件ascii_lowercase.然后是您的代码的以下部分:

for char in wordsToCheck[0]:
    if char in wordsToCheck[0] in char_up:
        result.append(char_low)
Run Code Online (Sandbox Code Playgroud)

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

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

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

我用了一个defaultdict而不是标准字典,因为它让这里的生活更轻松.如果你定义spelling errorsdict我的代码的一部分将不得不改为

if not word in word_pool:
    if not word in spelling_errors:
        spelling_errors[word] = 1
    else:
        spelling_errors[word] += 1
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我写的代码没有任何问题.我得到一个字典,其中缺少单词(小写)作为键,并将该单词的计数作为相应的值.