A router: rules, a small classifier, a default
The orders route is a stand-in: a real one would look the booking up in a database.
Not every question belongs in the handbook index. Routing sends a question to the right place (documents, a database, or nowhere): clear rules first, then a small classifier, and a safe default when nothing is sure.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write route(question) that returns orders if any word matches lb followed by digits (use re.fullmatch), else docs. Print the route for three questions.
Write classify(scores, minimum, default) that returns the name with the highest score only when it reaches minimum and no other name has the same score. Otherwise return default. Print the result for three score dicts.
Explain it without notes
Why use rules before a classifier?
What should the default route be, and why?
Why does the example send "Can I cancel my booking and get a refund?" to the documents?
How would you test a router?
Expected output
Where is my booking LB1042? orders rule: order id lb1042
show my last invoice orders classifier score 2
Hello! none classifier score 1
Do shops open on holidays? docs default
What is the weather tomorrow? none classifier score 1
Can I cancel my booking and get a refund? docs default