Knowing whether a user sounds frustrated, satisfied, or confused lets a chatbot adjust its tone, offer help earlier, or escalate to a human. Full neural sentiment models are overkill for this signal: lexicon scoring and tiny classifiers deliver useful accuracy with zero network calls.
Lexicon scoring: the simplest baseline
A sentiment lexicon maps words to polarity scores — 'excellent' at +3, 'okay' at +1, 'broken' at -2, 'terrible' at -3. Score a message by summing its words' values, flipping signs after negations:
function sentimentScore(tokens, lexicon) {
let score = 0;
let negate = false;
for (const t of tokens) {
if (t === 'not' || t === 'never' || t === 'no') { negate = !negate; continue; }
const v = lexicon[t] || 0;
score += negate ? -v : v;
if (/[.?!]/.test(t)) negate = false;
}
return score;
}
A lexicon of two to three thousand words covers most chat vocabulary and ships as a ~30 KB JSON file. Keep negation words out of your stop-word list or this step silently breaks.
Handling intensifiers and tone
Raw sums miss 'very', 'extremely', and stretched words like 'sooo'. Multiply the next word's score by 1.5 after intensifiers, and normalize elongated spellings ('baaad' becomes 'bad' with a small magnitude boost) during normalization. Emoji carry strong signal too: map common emoji to scores in the same lexicon rather than stripping them.
A tiny learned classifier
When lexicons plateau, train a logistic regression over unigram and bigram features. A few hundred labeled chat messages produce a model with a few thousand weights — small enough to hardcode as JSON and evaluate with a dot product. This is the same prototype classification spirit: simple geometry over text features, no deep learning required.
Evaluate with a confusion matrix to see which classes confuse the model. Sentiment classifiers typically confuse neutral with mildly positive more than they confuse positive with negative, which matters when only strong negativity triggers escalation.
Using sentiment in conversation
Sentiment is a control signal, not a display feature. Practical uses:
- Early help: two consecutive negative messages trigger a clarifying question or a human handoff.
- Tone matching: brief, direct answers for frustrated users; fuller explanations for curious ones.
- Topic guarding: negative sentiment plus topic correction suggests the last answer missed, so re-retrieve instead of continuing.
Never announce the detected sentiment ('You seem angry!'). Users find it creepy and it is wrong often enough to erode trust.
Privacy advantage
Server-side sentiment APIs send every message to a third party for analysis. A lexicon or tiny classifier runs entirely on device, consistent with a privacy-first architecture. The sentiment score never leaves the tab — only the conversation behavior it triggers is visible.