liveBook · Manning

Normalization

As explained in The Quick Python Book, Fourth Edition

Normalization is the process of converting data into a consistent canonical representation.

Normalization converts varied input into a consistent form before further processing. For word counts, case and punctuation matter: `This`, `this.`, and `this,` would otherwise be counted separately. For phone numbers, different separators can be replaced with one output format. section 21.2.2 section 16.6.2

Choose the rules for the data and the task:

  • For text, make letters one case, remove punctuation, then split into words. Processing a file line by line can save memory. section 6.10
  • For phone numbers, extract digits, check length and required area-code and exchange digits, add a missing country code, then insert separators. section 16.6.2

Preparing words for a count

`words = paragraph.lower().replace('.', '').replace(',', '').split()` makes case consistent and removes two punctuation marks before splitting. It does not remove every mark: a token such as `reefs--commerce` can remain joined. For more punctuation characters, `str.translate()` can replace them in one operation. section 21.2.2

import re
 
def normalize_phone_number(phone_number):
    phone_number = re.sub(r'\D', '', phone_number)          #1
 
    if len(phone_number) not in [10, 11]:                   #2
        raise ValueError("Invalid phone number length")
 
    if not re.match(r'^[2-9][0-8][0-9][2-9]', 
                   phone_number[-10:]):                     #3
        raise ValueError("Invalid area code or exchange code")
 
    if len(phone_number) == 10:                             #4
        phone_number = '1' + phone_number
 
    phone_number = re.sub(r'(\d{1})(\d{3})(\d{3})(\d{4})', 
                   r'\1-\2-\3-\4', phone_number)            #5
 
    return phone_number
 
# Test the function.
phone_numbers = ["+1 223-456-7890",
                "1-223-456-7890",
                "+1 223 456-7890",
                "(223) 456-7890",
                "1 223 456 7890",
                "999.456.7890",
                "1-989-111-2222", 
                "223.456.7890"]
 
for number in test_numbers:
    try:
      normalized_phone_number = normalize_phone_number(number)
      print(f'{normalized_phone_number}')
    except ValueError as e:
      print(f'{number} is not a valid phone number: {e}')
 
1-223-456-7890
1-223-456-7890
1-223-456-7890
1-223-456-7890
1-223-456-7890
999.456.7890 is invalid: Invalid area code or exchange code
1-989-111-2222 is invalid: Invalid area code or exchange code
1-223-456-7890
#1 Removes all nondigit (\D) characters
#2 The length must be 10 or 11.
#3 Checks area code and exchange at once
#4 Adds country code if needed
#5 Adds dashes between elements

The phone-number function uses `re.sub(r'\D', '', phone_number)` to discard nondigits, validates the remaining digits, adds `1` when needed, and formats the result with hyphens. Stripping separators is simpler when the input is already known to be a phone number; it is not a substitute for deciding which inputs are acceptable. section 16.6.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through cleaning steps and phone-number validation, while noting incomplete punctuation handling and limits of separator-pattern matching.
Examples
Uses a novel excerpt cleaned into words and phone numbers supplied with spaces, dots, hyphens, or parentheses.
What it assumes
Assumes basic Python string operations; supplies file-reading and writing scaffolding for the text-cleaning exercise.
What you can do after
Practice/perform text cleaning before counting words, or build a Python function that validates and reformats phone numbers.
How it connects
Connects string methods and tokenization to word frequency, and regular expressions and validation to phone-number formatting.

Also explained in

Compare how 21 books explain normalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.