【发布时间】: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