Python Forum

Full Version: Counting number of words and organize for the bigger frequencies to the small ones.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, everybody
I am doing a systematic literature review and I copied all the references used in my sample. I would like to identify the most common words in order to identify the authors most citted. I did this so far:
import sys


print(sys.argv[1])

f = open(sys.argv[1])
my_file_contents = f.read()

#print(f.read())

def word_count(str):
    counts = dict()
    words = str.split()

    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1

    return counts

print(word_count(my_file_contents))
I was able to identify every word's number, but I do not know how to present from the terms with the most frequent to the fewer ones. Can somebody help me?
For counting Python has Counter which has convenient most_common and other stuff useful while conducting counting.