From 98aabf40d6c746a199ed6871bd00b1987ec27b21 Mon Sep 17 00:00:00 2001 From: THOMAS <88090386+tomtricks@users.noreply.github.com> Date: Wed, 30 Oct 2024 23:43:14 +0530 Subject: [PATCH] Create Frequency_of_words.py Username: tomtricks date: 10/30/2024 Key Features of the Code Word Splitting: The code splits the input text into individual words. Case Insensitivity: It counts words in a case-insensitive manner by converting them to lowercase. Frequency Count: It uses a dictionary to keep track of how many times each word appears. --- Python/Frequency_of_words.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Python/Frequency_of_words.py diff --git a/Python/Frequency_of_words.py b/Python/Frequency_of_words.py new file mode 100644 index 00000000..b8b66e62 --- /dev/null +++ b/Python/Frequency_of_words.py @@ -0,0 +1,20 @@ +def word_frequency(text): + # Split the text into words + words = text.split() + frequency = {} + + # Count the frequency of each word + for word in words: + word = word.lower() # Convert to lowercase for uniformity + if word in frequency: + frequency[word] += 1 + else: + frequency[word] = 1 + + return frequency + +# Example usage +if __name__ == "__main__": + sample_text = "Hello world! Welcome to Hacktoberfest. Hello again!" + freq = word_frequency(sample_text) + print(freq)