Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add word frequency counter feature #628

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions word_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import sys
from collections import Counter

def count_word_frequency(file_path):
"""Counts the frequency of each word in a given text file."""
try:
with open(file_path, 'r') as f:
words = f.read().lower().split()
word_count = Counter(words)
sorted_word_count = word_count.most_common()

print("\nWord Frequency Count (Descending):\n")
for word, count in sorted_word_count:
print(f"{word}: {count}")
except FileNotFoundError:
print("File not found. Please check the file path.")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python word_counter.py <file_path>")
else:
file_path = sys.argv[1]
count_word_frequency(file_path)