2016-06-14 15 views
0

私はプログラムアーケードゲームで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 

A setはdictよりも適切かもしれません。なぜなら、tの辞書の値彼はサンプルが重要ではない。しかし、これはあなたに行く必要があります

+0

これは基本的に上記の私のコードは何ですか、私のコードはリストにAliceInWonderLand200テキストを追加しませんでした。 正常に動作するようにします。ありがとう。 – Skrizzy

関連する問題