Count Word Frequencies Practice Problem
This data science coding problem helps you practice String Manipulation, count word frequencies, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of String Manipulation.
- Problem ID: 97
- Problem key: 97-count-word-frequencies
- URL: https://datacrack.app/solve/97-count-word-frequencies
- Difficulty: easy
- Topic: String Manipulation
- Module: Python Fundamentals
Problem Statement
# š§© Count Word Frequencies
---
### šÆ Goal
Word frequency counting is the **backbone of NLP**. Every text analysis task ā sentiment analysis, topic modeling, TF-IDF, search engines ā starts with counting how often each word appears. This problem combines string splitting with the frequency counter pattern.
---
### š The Task
```
"the cat sat on the mat the cat"
ā {"the": 3, "cat": 2, "sat": 1, "on": 1, "mat": 1}
```
---
### š» Task
Implement `word_frequencies(text)`.
---
### š„ Input
- `text`: a string of space-separated words (all lowercase)
### š¤ Output
- dict mapping each unique word to its count in the text
---
### š§© Starter Code
```python
def word_frequencies(text):
"""
Count how many times each word appears in the text.
Args:
text (str): Space-separated lowercase words
Returns:
dict: {word: count} for each unique word
"""
words = text.split()
frequencies = {}
for word in words:
# š§ TODO: Increment the count for each word
pass
return frequencies
```
---
### š” Example
```python
word_frequencies("the cat sat on the mat the cat")
# Expected: {"the": 3, "cat": 2, "sat": 1, "on": 1, "mat": 1}
```
---
### š Key Concepts
- Combines `str.split()` (tokenization) with the frequency counter dict pattern
- This is the manual version of `Counter(text.split())` and `pd.Series(text.split()).value_counts()`
- Term Frequency (TF) in TF-IDF = count / total_words ā built on this exact functionStarter Code
def word_frequencies(text):
"""
Count how many times each word appears in the text.
Args:
text (str): Space-separated lowercase words
Returns:
dict: {word: count} for each unique word
"""
words = text.split()
frequencies = {}
for word in words:
# š§ TODO: Increment the count for each word
pass
return frequencies