From d7747e0981980db502604abe54f49405ec6437f2 Mon Sep 17 00:00:00 2001 From: jaimin45-art Date: Fri, 17 Oct 2025 10:17:18 +0530 Subject: [PATCH] Added Word Frequency Counter program in Python --- strings/word_frequency_counter.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 strings/word_frequency_counter.py diff --git a/strings/word_frequency_counter.py b/strings/word_frequency_counter.py new file mode 100644 index 000000000000..87dbd07bf1b5 --- /dev/null +++ b/strings/word_frequency_counter.py @@ -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)