Python Forum
Counting number of words and organize for the bigger frequencies to the small ones. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Counting number of words and organize for the bigger frequencies to the small ones. (/thread-32370.html)



Counting number of words and organize for the bigger frequencies to the small ones. - valeriorsneto - Feb-05-2021

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?


RE: Counting number of words and organize for the bigger frequencies to the small ones. - perfringo - Feb-05-2021

For counting Python has Counter which has convenient most_common and other stuff useful while conducting counting.