Python拼写检查器线性搜索

时间:2013-07-05 14:20:24

标签: python-3.x

我正在学习Python,其中一个实验室要求我导入一个单词列表作为字典,然后将该单词列表与一些也导入的文本进行比较。这不适合上课,我只是自己学习,或者我会问老师。在进行比较之前,我已经开始讨论如何将导入的文本转换为大写。

以下是实验室的网址:http://programarcadegames.com/index.php?chapter=lab_spell_check

我看过下面的帖子/答案以及一些YouTube视频,我仍然无法弄清楚如何做到这一点。任何帮助将不胜感激。

Convert a Python list with strings all to lowercase or uppercase

How to convert upper case letters to lower case

这是我到目前为止的代码:

# Chapter 16 Lab 11

import re

# This function takes in a line of text and returns
# a list of words in the line.
def split_line(line):
    return re.findall('[A-Za-z]+(?:\'[A-Za-z]+)?',line)

dfile = open("dictionary.txt")

dictfile = []
for line in dfile:
    line = line.strip()
    dictfile.append(line)

dfile.close()

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

afile = open("AliceInWonderLand200.txt")

for line in afile:
    words = []
    line = split_line(line)
    words.append(line)
    for word in words:   
        lineNumber = 0
        lineNumber += 1
        if word != (dictfile):
            print ("Line ",(lineNumber)," possible misspelled word: ",(word))

afile.close()

1 个答案:

答案 0 :(得分:1)

就像lb所说:你使用.upper()

dictfile = []
for line in dfile:
    line = line.strip()
    dictfile.append(line.upper()) # <- here.