【问题标题】:Linear search to find spelling errors in Python线性搜索以查找 Python 中的拼写错误
【发布时间】:2016-06-14 17:07:12
【问题描述】:

我正在通过 Program Arcade Games 学习 Python,但我被困在其中一个实验室上。

我应该比较文本文件 (http://programarcadegames.com/python_examples/en/AliceInWonderLand200.txt) 中的每个单词,以查找它是否不在字典文件 (http://programarcadegames.com/python_examples/en/dictionary.txt) 中,如果不在则打印出来。我应该对此使用线性搜索。

问题是即使我知道不在字典文件中的单词也没有被打印出来。任何帮助将不胜感激。

我的代码如下:

# Imports regular expressions
import re

# This function takes a line of text and returns
# a list of words in the line


def split_line(line):
    split = re.findall('[A-Za-z]+(?:\'\"[A-Za-z]+)?', line)
    return split


# Opens the dictionary text file and adds each line to an array, then closes the file
dictionary = open("dictionary.txt")
dict_array = []
for item in dictionary:
    dict_array.append(split_line(item))
print(dict_array)
dictionary.close()

print("---Linear Search---")

# Opens the text for the first chapter of Alice in Wonderland
chapter_1 = open("AliceInWonderland200.txt")

# Breaks down the text by line
for each_line in chapter_1:
    # Breaks down each line to a single word
    words = split_line(each_line)
    # Checks each word against the dictionary array
    for each_word in words:
        i = 0
        # Continues as long as there are more words in the dictionary and no match
        while i < len(dict_array) and each_word.upper() != dict_array[i]:
            i += 1
        # if no match was found print the word being checked
        if not i <= len(dict_array):
            print(each_word)

# Closes the first chapter file
chapter_1.close()

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    在 Python 中使用线性搜索查找拼写错误

    应该这样做(伪代码)

    sampleDict = {}
    For each word in AliceInWonderLand200.txt:
        sampleDict[word] = True
    
    actualWords = {}
    For each word in dictionary.txt:
        actualWords[word] = True
    
    For each word in sampleDict:
        if not (word in actualDict):
            # Oh no!  word isn't in the dictionary
    

    set 可能比字典更合适,因为样本中字典的值并不重要。不过,这应该会让你继续前进

    【讨论】:

    • 虽然这基本上是我上面的代码所做的,但我的代码没有将 AliceInWonderLand200 文本添加到列表中。让它做到这一点让它正常工作。谢谢。
    猜你喜欢
    • 2013-07-03
    • 2013-12-24
    • 2013-03-02
    • 1970-01-01
    • 2011-05-02
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多