【问题标题】:Python Spell Checker Linear SearchPython 拼写检查器线性搜索
【发布时间】:2013-07-03 16:27:40
【问题描述】:

我正在学习 Python,其中一个实验要求我导入一个单词列表以用作字典,然后将该单词列表与一些同样导入的文本进行比较。这不是上课的,我只是自学,或者我会问老师。在进行比较之前,我一直在关注如何将导入的文本转换为大写。

这是实验室的 URL: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()

【问题讨论】:

  • 就像 Peter Norvig 著名的拼写检查器:norvig.com/spell-correct.html。我建议你分解问题。阅读字典是您最不必担心的事情。这就是神奇的部分之后发生的事情。

标签: python-3.x


【解决方案1】:

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

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

【讨论】:

  • 字典中的单词已经是大写的了,AliceInWonderland 的文本不是。我尝试了“words.append(line.upper())”,我得到了“AttributeError:'list'对象没有属性'upper'”。我可能误解了你的回答。
  • @user30869:是的,显然你也需要在那里做.upper()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2020-09-27
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多