2016-06-14 79 views
0

我正在學習使用Program Arcade Games學習Python,並且我已經陷入其中一個實驗室。線性搜索在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() 

回答

0

線性搜索找到拼寫錯誤在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可能比字典更合適,因爲字典的值在t他抽樣並不重要。這應該讓你去,雖然

+0

儘管這基本上是我的代碼上面做的,我的代碼並沒有將AliceInWonderLand200文本添加到列表。 使它能夠正常工作。謝謝。 – Skrizzy