──────────────────────────────────────

24. The Message Sorting Desk · Build a Message Triage Tool

Extract a reference code, classify a message and handle invalid lines.

Lesson files · lessons/text-automation/message-triage

Before you start
- Validate a complete ticket code with re.fullmatch().
- Split a line on whitespace and check how many pieces it contains.
- Use a simple pattern to find words in text.

By the end
- Read a ticket code and the remaining message, then label it URGENT or NORMAL.
- Use a partial-word match and a malformed line to check how the program chooses its output.

Where you can use it
Sort a ticket queue into messages to read first and lines that need somebody to correct the code.

Read three example lines before writing a pattern. Each has a code, a space and free text. Split once to separate the code while keeping spaces in the message. Validate the code independently. Decide the label from whole words to avoid treating the mistyped urgentish as urgent.

Predict
Why split with maxsplit=1?
- To discard every word after the code.
- To keep the rest of the message together.

Worked example (example.py)

code, message = "SL-0042 Report ready".split(maxsplit=1)
print(code)
print(message)


Example result
SL-0042
Report ready

Read a code and a message. A valid code uses SL- and four digits. Print code and URGENT when the message contains urgent as a whole word, or code and NORMAL otherwise. Print Needs review. when the code or message is missing or malformed.

Starter program (starter.py)

code, message = "SL-0042 Report ready".split(maxsplit=1)
print(code)
print(message)


Hint
Validate the code with fullmatch. Use re.search(r"\burgent\b", message.lower()) for whole-word matching.

Case 1. Try the sample
Input
SL-0042 URGENT: water report due
Expected output
SL-0042 URGENT

Case 2. Avoid partial-word matches
Input
SL-0042 urgentish notes
Expected output
SL-0042 NORMAL

Case 3. Bad code
Input
SL-42 urgent
Expected output
Needs review.

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Apply your classifier to five example messages and count how many need attention.

Reflect
How did you make sure a word inside a longer word does not trigger the urgent label?

Check your prediction
To keep the rest of the message together. One split yields the first field and the remaining text.
