Word frequency analysis in Data Analysis Python - Time & Space Complexity
We want to know how the time to count words grows as the text gets longer.
How does the work change when we have more words to analyze?
Analyze the time complexity of the following code snippet.
text = "this is a sample text with several words this is a test"
words = text.split()
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
This code counts how many times each word appears in the text.
- Primary operation: Looping through each word in the list.
- How many times: Once for every word in the text.
As the number of words grows, the loop runs more times, increasing work linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and updates |
| 100 | About 100 checks and updates |
| 1000 | About 1000 checks and updates |
Pattern observation: The work grows directly with the number of words.
Time Complexity: O(n)
This means the time to count words grows in direct proportion to the number of words.
[X] Wrong: "Checking if a word is in the dictionary takes time proportional to the number of words counted so far."
[OK] Correct: Dictionary lookups are very fast and do not grow with the number of words counted, so each check is almost constant time.
Understanding how counting words scales helps you explain how your code handles larger texts efficiently.
"What if we used a list instead of a dictionary to store counts? How would the time complexity change?"
Practice
What does word frequency analysis help us understand about a text?
Solution
Step 1: Understand the purpose of word frequency analysis
Word frequency analysis counts how often each word appears in a text.Step 2: Compare options with the purpose
Only How many times each word appears in the text matches this purpose exactly.Final Answer:
How many times each word appears in the text -> Option AQuick Check:
Word frequency = count of each word [OK]
- Confusing word count with character count
- Thinking it counts sentences
- Assuming it finds longest word length
Which Python code correctly imports the tool to count word frequencies?
?Solution
Step 1: Recall the correct import syntax for Counter
The correct way is 'from collections import Counter'.Step 2: Check each option's syntax
Only from collections import Counter uses valid Python import syntax and correct name.Final Answer:
from collections import Counter -> Option DQuick Check:
Correct import = from collections import Counter [OK]
- Using wrong import order
- Trying to import non-existent modules
- Using invalid syntax like 'import Counter from'
What is the output of this code?
from collections import Counter
text = "apple banana apple orange banana apple"
words = text.split()
count = Counter(words)
print(count)Solution
Step 1: Split the text into words
The text splits into ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'].Step 2: Count each word using Counter
'apple' appears 3 times, 'banana' 2 times, 'orange' 1 time.Final Answer:
Counter({'apple': 3, 'banana': 2, 'orange': 1}) -> Option CQuick Check:
Count matches word occurrences [OK]
- Ignoring Counter output format
- Miscounting word frequencies
- Confusing dictionary and Counter print styles
Find the error in this code that tries to count words:
from collections import Counter
text = "Hello world! Hello"
words = text.split()
count = Counter(words.lower())
print(count)Solution
Step 1: Identify the type of 'words'
words is a list of strings after split().Step 2: Check the use of lower()
Calling words.lower() is invalid because lists have no lower() method; lower() applies to strings.Final Answer:
Cannot call lower() on a list; should call on each word -> Option AQuick Check:
lower() works on strings, not lists [OK]
- Calling lower() on list instead of string
- Ignoring error messages
- Thinking split() needs arguments
You want to count words ignoring case and punctuation in this text:
text = "Hello, hello! How are you? Are you fine?"Which code correctly cleans and counts words?
Solution
Step 1: Understand the need to remove punctuation and lowercase
To count words ignoring case and punctuation, text must be lowercased and punctuation removed.Step 2: Check each option's approach
import string from collections import Counter clean_text = text.lower().translate(str.maketrans('', '', string.punctuation)) words = clean_text.split() count = Counter(words) print(count) uses string.punctuation with translate() to remove punctuation and lowercases text before splitting, which is correct.Final Answer:
Code that lowercases and removes punctuation before counting -> Option BQuick Check:
Clean text + Counter = accurate word frequency [OK]
- Not removing punctuation
- Counting words with mixed cases separately
- Using split() without cleaning text
Ready to prove it?
Pass the test and this pattern moves unknown
