andbrs
andbrs

Reputation: 59

compare words from a list with a text file using counter.collection

I am making a small script that tries to compare words from a text file, for now, I have been able to compare extracting all the words and counting their frequency, now, how could I make the algorithm only extract the words from the .txt that are in a list determined by me... so far I have this

from collections import Counter
def word_count(filename):
    with open('hola.txt','r') as f:
        return Counter(f.read().split())

counter = word_count('hola.txt')
for i in counter:
    print (i, ":", counter [i])

Upvotes: 0

Views: 103

Answers (1)

Djaouad
Djaouad

Reputation: 22776

You can create your set of the words you want to consider, and feed the Counter a generator that contains only the words in your set, using a generator comprehension:

from collections import Counter

words_to_keep = {'only', 'keep', 'these', 'words'}

def word_count(filename):
    with open(filename, 'r') as f: # use `filename`
        return Counter(w for w in f.read().split() if w in words_to_keep)

counter = word_count('hola.txt')

for w, c in counter.items():
    print (w, ":", c)

Upvotes: 1

Related Questions