2016-10-14 17 views
0

私は関数を作っていますが、各単語がファイル内で何回使用されたかを数えました。現在、関数はすべての単語の合計を計算し、7つの最も一般的な単語とそれらが何回使用されたかを表示できます。今私は最初のファイルを比較したいのですが、私は単語の頻度を別のファイルと分析しました。私は英語で最もよく使われている単語を持っていました。いずれかの単語が一致します。リストを比較してリストを比較するpython

私が目にしたことは、2つのファイルのリストを作成してお互いに比較することです。しかし、私がこれについて書いたコードは、どのような出力、私はこれを解決する方法についての任意のアイデアを与えることはありません?

def CountWords(): 
filename = input('What is the name of the textfile you want to open?: ') 
if filename == "alice" or "alice-ch1.txt" or " ": 
    file = open("alice-ch1.txt","r") 
    print('You want to open alice-ch1.txt') 
    wordcount = {} 
    for word in file.read().split(): 
     if word not in wordcount: 
      wordcount[word] = 1 
     else: 
      wordcount[word] += 1           
    wordcount = {k.lower(): v for k, v in wordcount.items() } 
    print (wordcount) 

    sum = 0 
    for val in wordcount.values(): 
     sum += val 
    print ('The total amount of words in Alice adventures in wonderland: ' + str(sum)) 
    sortList = sorted(wordcount.values(), reverse = True) 
    most_freq_7 = sortList[0:7] 
    #print (most_freq_7) 
    print ('Totoro says: The 7 most common words in Alice Adventures in Wonderland:') 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[0])] + " " + str(most_freq_7[0])) 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[1])] + " " + str(most_freq_7[1])) 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[2])] + " " + str(most_freq_7[2])) 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[3])] + " " + str(most_freq_7[3])) 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[4])] + " " + str(most_freq_7[4])) 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[5])] + " " + str(most_freq_7[5])) 
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[6])] + " " + str(most_freq_7[6])) 

    file_common = open("common-words.txt", "r") 
    commonwords = [] 
    contents = file_common.readlines() 

    for i in range(len(contents)): 
     commonwords.append(contents[i].strip('\n')) 
    print(commonwords) 

#From here's the code were I need to find out how to compare the lists: 
    alice_keys = wordcount.keys() 
    result = set(filter(set(alice_keys).__contains__, commonwords)) 
    newlist = list() 


    for elm in alice_keys: 
     if elm not in result: 
      newlist.append(elm) 
    print('Here are the similar words: ' + str(newlist)) #Why doesn't show? 


else: 
    print ('I am sorry, that filename does not exist. Please try again.')    

答えて

0

私は通訳者の前にいないので、私のコードは多少オフかもしれません。しかし、もっとこのようなものを試してみてください。

from collections import Counter 
with open("some_file_with_words") as f_file 
    counter = Counter(f_file.read()) 
    top_seven = counter.most_common(7) 
    with open("commonwords") as f_common: 
    common_words = f_common.read().split() 
    for word, count in top_seven: 
     if word in common_words: 
     print "your word " + word + " is in the most common words! It appeared " + str(count) + " times!" 
+0

ありがとう@ bravosierra99! – Allizon

+0

「あなたの言葉eは最も一般的な言葉にあります....」という言葉ではなく、文字として出てきます。 – Allizon

+0

あなたの一般的な言葉のファイルはどのように設定されていますか?私はスペースを使って単語を区切る必要があることを意味する.split()を使用しています。一般的な単語ファイルの設定方法をこのコードで調整する必要があります。 – bravosierra99

関連する問題