2013-07-05 61 views
2

我正在學習Python,並且其中一個實驗室要求我導入單詞列表以用作字典,然後將該單詞列表與也導入的某些文本進行比較。這不適合上課,我只是自己學習,或者我會問老師。我一直掛在如何在進行比較之前將導入的文本轉換爲大寫。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() 
+0

像彼得·諾維格著名的咒語檢查器:http://norvig.com/spell-correct.html。我建議你分解這個問題。閱讀字典是你最擔心的問題。這就是那個神奇的東西。 – duffymo

回答

1

像LB說:你使用.upper()

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

字典中的單詞已經是大寫字母了,它不是AliceInWonderland文本。我試過「words.append(line.upper())」,並且我找回了「AttributeError:'列表'對象沒有屬性'upper'」。我可能誤解了你的答案。 – user30869

+0

@ user30869:是的,顯然你也需要在這裏執行'.upper()'。 –