Skip to content

Commit

Permalink
Added the script for word frequrncy counter issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rkt-1597 committed Oct 19, 2024
1 parent de808b4 commit 1082186
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Word_frequency_counter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re
from collections import Counter

def find_words_frequency(file_path):
'''
This script takes the path of the text file to be processed, as input (argument)
and prints the top ten words and also prints their counts in given text file.
'''
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read().lower()

all_words = re.findall(r'\b\w+\b', text)
word_frequency = Counter(all_words)
most_common_words = word_frequency.most_common(10)

# Print in tabular format
print(f"{'Word':<15} {'Count':<5}")
print("-" * 20)
for word, count in most_common_words:
print(f"{word:<15} {count:<5}")

def main():
file_path = input("Enter the path of file : ")
find_words_frequency(file_path)

if __name__ == "__main__":
main()

0 comments on commit 1082186

Please sign in to comment.