Skip to content
Closed
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
25 changes: 25 additions & 0 deletions strings/word_frequency_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# word_frequency_counter.py
# This program counts the frequency of each word in a paragraph of text.
# Author: jaimin45-art

def word_frequency(text: str):
# Convert to lowercase and remove punctuation
for ch in [',', '.', '?', '!', ';', ':']:
text = text.replace(ch, '')
words = text.lower().split()

freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1

# Sort by frequency (descending) then alphabetically
sorted_words = sorted(freq.items(), key=lambda x: (-x[1], x[0]))

print("Word Frequency:\n")
for word, count in sorted_words:
print(f"{word} : {count}")

if __name__ == "__main__":
print("Enter a paragraph of text:")
paragraph = input()
word_frequency(paragraph)