v9.1.1
Menu

Text Normalizer

mediumFREEText ProcessingNLP 1: Foundations

Implement a text normalizer that performs the following steps in order:

  1. Convert to lowercase
  2. 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"
  3. Remove all punctuation (keep only alphanumeric and spaces)
  4. 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
  • Input: one sentence, I can't believe it's already 2024!: two capitals, two apostrophes inside contractions, and a trailing !.
  • Lowercase first: it becomes "i can't believe it's already 2024!". This has to come before expansion because the contraction table is written in lowercase, so "I'm" would only match as "i'm".
  • Expand: "can't" becomes "cannot" (one word, not two) and "it's" becomes "it is". Notice the apostrophes are still present at this point; if punctuation were stripped earlier, "can't" would turn into "cant" and no longer match any entry.
  • Strip punctuation: only the ! is left to remove. Letters, the digits 2, 0, 2, 4 and spaces survive, so the year stays intact.
  • Tidy spaces: the gaps here are already single spaces, giving i cannot believe it is already 2024. The space cleanup matters for inputs like "right -- don't": deleting the dashes leaves two spaces side by side, which must shrink to one.

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

Learn this in

More from NLP 1: Foundations

solution.py

Test results

Sign in to run your code against the test cases.