Я пытаюсь написать код для чтения двух отдельных текстовых файлов, фильтрации общих слов, вычисления частоты слов в каждом файле и, наконец, вывода в порядке относительной частоты между двумя списками. Поэтому в идеале слова, которые относительно чаще встречаются в файле 1, должны отображаться вверху списка, слова, относительно более часто встречающиеся в файле 2, должны располагаться внизу списка, а те слова, которые встречаются в обоих файлах, должны находиться в середине. Например:
word, freq file 1, freq file 2
Cat 5,0
Dog 4,0
Mouse 2,2
Carrot 1,4
Lettuce 0,5
​
Мой код в настоящее время выводит слова в порядке их частоты в файле 1, но я не могу понять, как организовать список, чтобы слова, более распространенные в файле 2, появлялись в внизу списка. Я понимаю, что мне нужно вычесть частоту слов в файле 1 из частоты тех же слов в файле 2, но я не могу понять, как работать с кортежем в словаре ...
Пожалуйста, помогите!
import re
f1=open('file1.txt','r', encoding="utf-8") #file 1
f2=open('file2.txt','r', encoding="utf-8") #file 2
file_list = [f1, f2] # This will hold all the files
num_files = len(file_list)
stopwords = ["a", "and", "the", "i", "of", "this", "it", "but", "is", "in", "im", "my", "to", "for", "as", "on", "helpful", "comment", "report", "stars", "reviewed", "united", "kingdom", "was", "with", "-", "it", "not", "about", "which", "so", "at", "out", "abuse", "than","any", "if", "be", "can", "its", "customer", "dont", "just", "other", "too", "only", "people", "found", "helpful", "have", "wasnt", "purchase", "do", "only", "bought", "etc", "verified", "", "wasnt", "thanks", "thanx", "could", "think", "your", "thing", "much", "ive", "you", "they", "vine", "had", "more", "that"]
frequencies = {} # One dictionary to hold the frequencies
for i, f in enumerate(file_list): # Loop over the files, keeping an index i
for line in f: # Get the lines of that file
for word in line.split(): # Get the words of that file
word = re.sub(r'[^\w\s]','',word) # Strip punctuation
word = word.lower() # Make lowercase
if not word in stopwords: # Remove stopwords
if not word.isdigit(): # Ignore digits
if not word in frequencies:
frequencies[word] = [0 for _ in range(num_files)] # make a list of 0's for any word not seen yet -- one 0 for each file
frequencies[word][i] += 1 # Increment the frequency count for that word and file
frequency_sorted = sorted(frequencies, key=frequencies.get, reverse=True)
for r in frequency_sorted:
print (r, frequencies[r])