📘
Text Normalizer
MediumText Processing
Implement a text normalizer that performs the following steps in order:
- Convert to lowercase
- Expand common contractions:
- "don't" -> "do not"
- "can't" -> "cannot"
- "won't" -> "will not"
- "i'm" -> "i am"
- "it's" -> "it is"
- "i've" -> "i have"
- "they're" -> "they are"
- "we're" -> "we are"
- "you're" -> "you are"
- "isn't" -> "is not"
- "aren't" -> "are not"
- "wasn't" -> "was not"
- "weren't" -> "were not"
- "hasn't" -> "has not"
- "haven't" -> "have not"
- "wouldn't" -> "would not"
- "couldn't" -> "could not"
- "shouldn't" -> "should not"
- "didn't" -> "did not"
- "let's" -> "let us"
- Remove all punctuation (keep only alphanumeric and spaces)
- Collapse multiple spaces into a single space and strip leading/trailing spaces
Input: A single line of text Output: The normalized text
Example:
Input:
I can't believe it's already 2024!
Output:
i cannot believe it is already 2024
Reasoning:
Step 1: Lowercase "i can't believe it's already 2024!"
Step 2: Expand contractions "can't" -> "cannot", "it's" -> "it is" "i cannot believe it is already 2024!"
Step 3: Remove punctuation Remove "!" -> "i cannot believe it is already 2024"
Step 4: Collapse spaces Already single-spaced, result: "i cannot believe it is already 2024"
Constraints:
- Input is a single string
- Apply steps in the order listed: lowercase, expand, remove punctuation, collapse spaces
- Only the listed contractions need to be handled
- After normalization, words are separated by single spaces
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.