How to calculates the frequency of each word in text file with Python

1 Answer

0 votes
frequency_words = {}


def sort_words(desc=False):
    words = [(word, count) for word, count in frequency_words.items()]
    return sorted(words, key=lambda s: s[1], reverse=desc)


def frequency_word_count(words):
    for word in words:
        if word != '':
            if word.lower() in frequency_words:
                frequency_words[word.lower()] += 1
            else:
                frequency_words[word.lower()] = 1


with open('info.txt') as f:
    count = 0
    for line in f:
        print("line {}: {}".format(count + 1, line))
        frequency_word_count(line.strip().split(' '))
        count += 1
sorted_words = sort_words(desc=True)
print("Most frequent 12 words {}".format(sorted_words[:12]))


'''
run:
 
line 1: Python is an interpreted

line 2: high-level, general-purpose

line 3: programming language

line 4: python design philosophy emphasizes code readability

line 5: with its notable use of significant whitespace

line 6: python java java c++ c php nodejs c++ c++ c++

Most frequent 12 words [('c++', 4), ('python', 3), ('java', 2), ('is', 1), 
                       ('an', 1), ('interpreted', 1), ('high-level,', 1), 
                       ('general-purpose', 1), ('programming', 1), 
                       ('language', 1), ('design', 1), ('philosophy', 1)]
 
'''

 



answered Jun 23, 2020 by avibootz

Related questions

1 answer 100 views
1 answer 243 views
1 answer 217 views
1 answer 188 views
1 answer 181 views
1 answer 225 views
1 answer 206 views
...