8.4.2. Lab#
Clean and summarize support-ticket text: normalize titles, validate IDs, extract fields, tokenize descriptions, and count vocabulary. Each question repeats its inputs so it runs independently. Grading also checks the named functions on alternate inputs.
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: Normalize support-ticket titles
# Define normalize_title(text) to collapse whitespace to single spaces and title-case it.
# Return the cleaned string. Apply it to raw_title and print its word count.
# This starts a support-ticket cleaning workflow; every question is self-contained.
# Print exactly:
# Clean title: Disk Full On Server
# Word count: 4
raw_title = " disk FULL on server "
### Your code starts here.
### Your code ends here.
### Question 2: Validate support-ticket IDs
# Define valid_ticket(code) to return a bool: the entire ID must be TS-, then exactly
# four ASCII digits. Use re.fullmatch() with [0-9], not a prefix check.
# Print each supplied ID followed by its validity.
# Print exactly:
# TS-2024 True
# xTS-2024 False
# TS-2024junk False
# TS-12 False
codes = ['TS-2024', 'xTS-2024', 'TS-2024junk', 'TS-12']
### Your code starts here.
### Your code ends here.
### Question 3: Extract fields safely
# Define parse_ticket(record) to return (ticket_id, owner), or None for a malformed record.
# The complete format is TS- plus four ASCII digits, then " | Owner: ", then
# one or more ASCII letters. Use capture groups and check the match before extraction.
# Print each record's parsed result. This extracts fields from validated ticket records.
# Print exactly:
# ('TS-2024', 'Alice')
# None
# None
records = ['TS-2024 | Owner: Alice', 'TS-12 | Owner: Bob', 'TS-2024 | Owner: Alice junk']
### Your code starts here.
### Your code ends here.
### Question 4: Tokenize ticket descriptions
# Define tokenize(text): replace em dashes with spaces, split on whitespace,
# strip the boundary punctuation . , ! ? ; : " ( ), lowercase, and discard empty tokens.
# Preserve internal hyphens. Return a list of cleaned words.
# Print the tokens from description. These tokens will feed the frequency report.
# Print exactly:
# Tokens: ['disk', 'disk', 'read-only']
description = ' DISK, disk! read-only !!! '
### Your code starts here.
### Your code ends here.
### Question 5: Summarize ticket vocabulary
# Define count_words(tokens) to return a normal dictionary of word counts.
# Ignore empty strings. Use Counter on the same nonempty tokens counted by the dictionary.
# Print the counts and the two most common words using Counter(counts).most_common(2).
# The supplied tokens are the cleaned output from the previous workflow stage;
# repeat them here so this question runs independently.
# Print exactly:
# Counts: {'disk': 2, 'read-only': 1}
# Most common: [('disk', 2), ('read-only', 1)]
tokens = ['disk', 'disk', 'read-only']
### Your code starts here.
### Your code ends here.
Question 1
Question 2
Question 3
Question 4
Question 5