Text cleaning pipeline in Data Analysis Python - Time & Space Complexity
We want to understand how the time needed to clean text grows as the amount of text grows.
How does the cleaning process scale when we have more words or sentences?
Analyze the time complexity of the following code snippet.
import re
def clean_text(texts):
cleaned = []
for text in texts:
text = text.lower()
text = re.sub(r'[^a-z ]', '', text)
words = text.split()
cleaned.append(words)
return cleaned
This code cleans a list of text strings by making them lowercase, removing non-letter characters, and splitting into words.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each text string and processing each character inside the regex substitution and splitting.
- How many times: Once for each text string, and inside each string, operations run proportional to its length.
As the number of texts and their length grow, the cleaning time grows roughly in proportion to total characters.
| Input Size (n texts) | Approx. Operations (proportional to total characters) |
|---|---|
| 10 (short texts) | Low, quick cleaning |
| 100 (medium texts) | About 10 times more work |
| 1000 (long texts) | About 100 times more work |
Pattern observation: The time grows roughly linearly with the total amount of text to clean.
Time Complexity: O(n * m)
This means the cleaning time grows linearly with the number of texts (n) and the average length of each text (m).
[X] Wrong: "The cleaning time depends only on the number of texts, not their length."
[OK] Correct: Each text's length affects how many characters are processed, so longer texts take more time even if the count is the same.
Understanding how text cleaning scales helps you explain your approach clearly and shows you think about efficiency in real data tasks.
"What if we added a nested loop to check each word against a list of stopwords? How would the time complexity change?"
Practice
Solution
Step 1: Understand text cleaning goal
Text cleaning aims to prepare text data by making it consistent and easier to work with.Step 2: Identify correct purpose
Adding punctuation or increasing length does not help analysis; uniformity does.Final Answer:
To make text uniform and easier to analyze -> Option BQuick Check:
Text cleaning = uniform text [OK]
- Thinking cleaning adds punctuation
- Confusing cleaning with translation
- Believing cleaning increases text length
text to lowercase?Solution
Step 1: Recall Python string method for lowercase
Python strings have a method calledlower()to convert text to lowercase.Step 2: Check each option
text.lower() uses correct syntax. Options B, C, D are invalid in Python.Final Answer:
text.lower() -> Option AQuick Check:
Python lowercase method = lower() [OK]
text.lower() to lowercase strings in Python [OK]- Using JavaScript method
toLowerCase()in Python - Trying a non-existent function
lower()outside string - Using incorrect method name like
lowercase()
cleaned_text?
import string
text = " Hello, World! "
cleaned_text = text.lower().strip().translate(str.maketrans('', '', string.punctuation))
print(cleaned_text)Solution
Step 1: Apply
The text becomes "hello, world!" after lowering and removing spaces at ends.lower()andstrip()Step 2: Remove punctuation with
All punctuation marks like comma and exclamation are removed, resulting in "hello world".translate()Final Answer:
"hello world" -> Option CQuick Check:
Lowercase + strip + remove punctuation = "hello world" [OK]
- Forgetting to strip spaces
- Not removing punctuation correctly
- Confusing case conversion order
def clean_text(text):
text = text.lower()
text = text.strip
return text
print(clean_text(" DATA Science "))Solution
Step 1: Check method usage
stripis a method and must be called with parentheses().Step 2: Identify fix
Changingtext.striptotext.strip()correctly removes spaces.Final Answer:
Changetext.striptotext.strip()-> Option AQuick Check:
Method calls need parentheses [OK]
- Forgetting parentheses on methods
- Using wrong syntax for lower()
- Removing return statement by mistake
texts = ["Data Science! ", " Machine Learning.", "AI, is fun!"]Which code snippet correctly cleans all texts by making them lowercase, removing punctuation, and trimming spaces?
Solution
Step 1: Understand cleaning steps needed
We need lowercase, remove punctuation, and trim spaces for each text.Step 2: Check each option
import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts] applies all three steps correctly using list comprehension. Others miss one or more steps.Final Answer:
import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts] -> Option DQuick Check:
All steps in list comprehension = import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts] [OK]
- Missing punctuation removal
- Not trimming spaces
- Removing spaces incorrectly with replace
Ready to prove it?
Pass the test and this pattern moves unknown
