clarity-engine

Cleaning wrong labels out of a text dataset before training anything, and measuring how far it gets.

CI status Python 3.11 to 3.14 MIT licence

What the neighbours voted

930 short customer messages across five intents, labelled by a simulated sloppy annotator who got 41.8% of them wrong. Each message's 15 nearest neighbours in embedding space vote on its label, weighted by how similar they are. Five real rows:

MessageLabel givenNeighbours votedOutcome
“I was charged twice for the mobile app”technical (wrong)billingfixed
“it says server error every time”praise (wrong)technicalfixed
“my rder has not arrived yet asap”billing (wrong)shippingfixed
“can you ship your product faster thanks”praise (wrong)shippingfixed
“i love the new the premium plan feature”praisetechnical (wrong)broken

The first four had wrong labels, and the vote put them right, typos and all. The last one was labelled correctly, but its closest neighbours are complaints that the premium plan's button does nothing: the shared product name outweighed the praise. Cleaning does both, as the tables below show, and these five come out the same in every one of the 30 runs behind them.

What happens to the labels

The dataset repeats many messages word for word, so neighbours are often exactly tied and rounding decides between them. Each number here is therefore the median and range over 30 runs, each with the embeddings nudged by one part in a million: far too little to change any meaning, enough to re-roll the ties.

MeasureMedianRange over 30 runs
Wrong labels put right85.6%83.0–87.1%
Right labels made wrong7.7%6.8–9.1%
Label accuracy, from 58.2% to89.5%87.6–90.2%

Classifier accuracy after cleaning the full dataset

The original split can place the same text on both sides, and noisy test labels can influence the cleaner.

Trained onMedianRange over 30 runs
The raw noisy labels84.1%83.7–84.1%
Consensus-corrected labels91.4%90.1–92.3%
Corrected, low-confidence rows dropped91.2%85.8–93.6%
Left: the messages in embedding space, coloured by corrected label. Right: a histogram of how sure each vote is, with a dashed line at the gate and the tallest bar starting right at it.
From a single run. On the right, how sure each vote is: the dashed line is the gate at 0.6, and the tallest bar starts right at it, which is the problem the next section explains.

Classifier accuracy on unseen message strings

One Windows run kept identical strings together and cleaned only the training labels. On the same 232 test rows, accuracy was 78.0% with raw labels, 84.1% with corrected labels, and 85.3% after the confidence gate. Variants of a synthetic message template can still appear in both folds.

Why the gate is unreliable

The gate keeps a row when at least 0.6 of the vote agrees, and 9 of 15 neighbours agreeing is exactly 0.6. With only 336 distinct messages among the 930, many rows land right on the cut-off: roughly a hundred sit within 0.005 of it, 68 to 126 on the machines tested so far. Whether each one is kept comes down to rounding, and the gate's result moves with it.

Even the processor matters. ONNX Runtime picks different low-level routines on different CPUs, so the last bits of the embeddings differ, and those bits decide ties. The medians above move by up to about a point between machines; the conclusions hold on all of them.

An earlier version of this project reported the gate as the best result, about 93%, from a run that happened to land near the top of its range. Reporting a spread instead of a single run is what caught it.

Three smaller experiments

Distillation: a small student imitating a big teacher

A teacher using the full 384-dimensional embeddings labels data with soft probabilities, and a linear student with 45 parameters learns to imitate it. The student gets within about 3 points of the teacher, 91% against 94%, at 1/42 of the size.

The textbook claim that soft labels beat hard ones did not hold. Compared fairly, each pair starting from the same weights, over 30 splits, soft labels came out 0.5 points worse on average (95% CI −0.84 to −0.14) and won only 8 of the 30.

Conformal prediction: sets of labels with a coverage guarantee

Instead of one label, return a set that contains the right one at least, say, 90% of the time. Confident messages get a single label; vague ones such as “ok” get several. The textbook rule meets its target partly by sometimes returning an empty set. A practical rule that always keeps the top class can never cover less than the classifier's own top-1 accuracy, 95% here, so it over-covers at low targets. Averaged over 200 random draws:

TargetNever-empty setsTextbook setsEmpty sets (textbook)
80%98.0%81.2%16.8%
90%98.6%90.3%8.3%
95%99.3%95.9%3.5%

A language model for the rows the vote cannot settle

llm_denoise.py sends only the ambiguous rows to Claude Opus 5.5, with each row's neighbours as context and an explicit “uncertain” option, and uses structured output so the answer is always one of the known labels. It needs Anthropic credentials; without them it prints its plan.

Run it yourself

pip install -r requirements.txt
python clarity_engine.py               # cleaning and training; writes the figures
python clarity_engine.py --grouped-holdout  # evaluate on unseen message strings
python clarity_engine.py --stability   # the medians and ranges above
python distill_demo.py                 # teacher and student
python conformal_demo.py               # conformal prediction sets

Python 3.11 to 3.14. The first run downloads the embedding model, about 90 MB, once, and runs it through ONNX Runtime, so there is no PyTorch to install.

The tests re-run every number in the README with the real model on Linux, Windows and macOS, and check this page against the README and the code, the five rows above included.

pip install -r requirements-dev.txt
pytest

Future scope