The problem
A small online shop's inbox mixes order chases, refunds, broken parcels, payment trouble, product questions, partnership offers and spam. Someone reads every email just to decide who should answer it, and the angry ones wait in the same line as the rest.
Triage does that first read in seconds. It files each email, pulls out the order number and dates the agent will need, puts the urgent ones first with a reply time, and drafts the reply. People still check and send; they just start from a sorted queue and a first draft.
The data
Nila Home is made up: an online home-goods shop in Chennai that sells brass diyas, cast iron tawas, jute rugs and the like. Its support history is 405 emails in 9 categories, generated from templates with a fixed seed and roughed up the way real inboxes are: typos, lowercase typing, Tanglish ("pls do the needful", "10 days back"), vague one-liners, two topics in one email, and about one label in 20 that is plainly wrong.
Today's inbox is 30 emails I wrote by hand, from the weekend and Monday morning: order chases, a refund two weeks late, a broken dinner set, a double charge, a polite letter that ends with the consumer court, and two pieces of spam. They do not come from the templates, so they test the classifier on writing it has not seen.
The split is fixed: 324 history emails train the classifier and 81 are held back to test it. Every name, order number and phone number is invented.
The model
Naive Bayes on words and word pairs. It counts how often each word appears in each category's past emails and adds up the evidence. It trains in a fraction of a second, works with a few hundred examples, and explains itself: the words behind each decision are the ones the demo shows.
Before it reads an email, order numbers, amounts, phone numbers and product names become placeholders, so it learns "mentions an order number" rather than one number, and does not decide that rugs get returned more often than tawas.
Naive Bayes is famously over-confident, so I calibrate it: its log-probabilities are divided by a temperature (3.5 here) chosen on five folds of the training emails. Without that, nearly every email says 100% and a confidence threshold means nothing. Below 70%, an email goes to a person.
TF-IDF finds the three most similar past emails, shown beside each decision as a check. On its own, giving an email the label of its nearest past email gets 88.9% of the held-out emails right; naive Bayes gets 92.6%.
Rules do the rest, because they should be easy to read and change: chrono-node reads dates against the time the email was sent ("last Tuesday", "10 days back", and a bare "Saturday" takes its direction from the tense around it); patterns find order numbers, amounts in rupees and Indian phone numbers; urgency is a sum of points; routing is four rules where the first match wins.
The pipeline
- 01Receive
Sender, subject and the time it was sent, in India time.
- 02Clean
Quoted earlier messages and "Sent from my iPhone" come out; the signature is set aside, since it often holds the phone number.
- 03Classify
Calibrated naive Bayes gives a probability for each of the 9 categories, with the words behind it and the closest past emails.
- 04Extract
Order numbers (NH- and five digits, or written without it), amounts in ₹, dates, phone numbers and products, each highlighted in the email.
- 05Urgency
Points for urgent words, legal threats, public-review threats, chasing, negative words, money at stake and long waits: Low, Normal or High.
- 06Route
Human review below the threshold, spam to spam, complaints and legal threats to Escalations, the rest to their queue, each with a reply time in working hours.
- 07Draft
A reply from the category's template, filled with the name, order number, amount and the reply-by time.
Results
Measured when this page was built, with the same code the demo runs. The history comes from templates, so held-out accuracy flatters the model; the hand-written inbox is the fairer test.
For a client
On a real inbox I would train on the shop's own labelled tickets (a few thousand is plenty for a model like this), retrain every month, and track accuracy for each category so drift shows early.
It would sit beside the helpdesk the team already uses, connected through its API or a workflow tool such as n8n: a new email comes in, and its category, queue, reply time and draft go back as tags and a private note. Nothing is sent without a person.
The order number would be looked up in the order system, so the draft can say where the parcel actually is. A language model could smooth the wording, with the facts still coming from the extractors.
The threshold becomes a business decision: what a wrongly filed email costs against an agent's time. The trade-off chart in the demo is the conversation I would have with the support lead.
Stack
| Part | What it does here | Licence |
|---|---|---|
| My machine-learning library | Naive Bayes, TF-IDF, cross-validation and the seeded sample generator, written in TypeScript and unit-tested | Mine |
| chrono-node | Reads dates written in plain English | MIT |
| Preact | The interactive demo | MIT |
| Astro | The page, built as static HTML | MIT |
Privacy
Everything runs in your browser. The inbox, and any email you write yourself, never leave your device: there is no server behind this page.
Limits
- The history is generated, so the held-out accuracy shows how well the model learns my templates. Real emails are messier, and the first weeks on a real inbox would need a person checking more of them.
- It reads English, including Indian English and a little Tanglish. An email in Tamil script would go to a person.
- The urgency rules are deliberately simple: sarcasm, or anger without any of the listed words, scores Normal.
- Drafts are templates filled with what was found. A product question gets a gap for the agent to fill with the answer.