
π Day 76/150 – Count Word Frequency in Python
Counting the frequency of words is a common task in text analysis, data processing, and natural language processing (NLP). It helps us determine how many times each word appears in a sentence or paragraph.
Let's explore different ways to count word frequencies in Python.
πΉ Method 1 – Using Dictionary
The most fundamental approach is to use a dictionary.
text = "python is easy and python is powerful" words = text.split() freq = {} for word in words: freq[word] = freq.get(word, 0) + 1 print(freq)
Output
{'python': 2,
'is': 2,
'easy': 1,
'and': 1,
'powerful': 1
}
Explanation
- split() converts the sentence into a list of words.
- get(word, 0) returns the current count or 0 if the word doesn't exist.
- Each occurrence increases the count by 1.
πΉ Method 2 – Taking User Input
Output
{
'hello': 2,
'world': 1,
'python': 1
}πΉ Method 3 – Using count()
'hello': 2,
'world': 1,
'python': 1
}
A simple approach for small datasets.
text = "python is easy and python is powerful" words = text.split() for word in set(words): print(word, ":", words.count(word))
Output
python : 2
is : 2
easy : 1
and : 1
powerful : 1
Explanation
- set(words) removes duplicates.
- count() counts occurrences of each word.
- Less efficient for large texts because it scans the list repeatedly.
πΉ Method 4 – Using Function
def word_frequency(text): freq = {} for word in text.split(): freq[word] = freq.get(word, 0) + 1 return freq print(word_frequency("python is easy and python is powerful"))
Output
{
'python': 2,
'is': 2,
'easy': 1,
'and': 1,
'powerful': 1
}
Explanation
- Encapsulates the logic inside a reusable function.
-
Makes the code cleaner and easier to maintain.
π₯ Key Takeaways
✔️ Use a dictionary for learning the logic behind frequency counting.
✔️ get() helps avoid checking whether a key exists.
✔️ Counter is the most efficient and Pythonic solution.
✔️ Word frequency analysis is widely used in data science and NLP.
✔️ split() is commonly used to convert text into words.

0 Comments:
Post a Comment