TL;DR
Traditional text anomaly detection operates at the document level, flagging an entire text as "anomalous" without explaining why or where. This paper introduces a novel token-level anomaly detection paradigm and proposes TokenCore, a unified framework that utilizes a memory bank of normal token embeddings to pinpoint specific anomalous words across diverse error types, significantly improving interpretability and localization.
What Problem Does This Paper Address?
Existing text anomaly detection methods typically operate at two extremes. On one end, coarse-grained document-level detectors can flag a piece of text as spam or fake news but fail to locate the specific problematic phrases. On the other end, highly specialized token-level tools (like spell checkers or grammatical error correctors) only target predefined, narrow anomaly types and fail to generalize.
In the real world, anomalies are messy and diverse—ranging from character corruption and typos to subtle semantic contradictions. The authors address this gap by formalizing a unified token-level text anomaly detection task. This comes with unique challenges:
- Extreme Class Imbalance: Anomalous tokens often make up less than 1% of a document.
- Representation Smoothing: Pre-trained language models (PLMs) are optimized for semantic similarity, meaning they tend to "smooth out" surface-level anomalies like typos.
- Aggregation Dilemmas: Combining token-level scores into a document-level score without amplifying false positives is non-trivial.
Methods
The authors introduce TokenCore, a framework adapted from the computer vision anomaly detection model PatchCore.
The pipeline consists of three main steps:
- Embedding Extraction with Max Pooling: Input text is processed using a pre-trained BERT model. Because tokenizers often split words into subwords, TokenCore uses max pooling across subword embeddings to capture and preserve the strongest anomaly signal for each word.
- Token-Level Scoring via Memory Bank: During training, the model extracts word-level embeddings from a corpus of entirely normal text to build a "memory bank." At test time, the anomaly score of a given token is determined by its Euclidean distance to its nearest neighbor in the normal memory bank.
- Document-Level Aggregation: To generate an overall document score, the model computes the mean of all token-level anomaly scores, balancing interpretability with global detection.
Datasets
To evaluate the framework, the authors curated and annotated three benchmark datasets with both token- and document-level labels:
- SMS Spam Dataset: Focuses on character-level anomalies by injecting random gibberish strings (e.g., 'jasdhnjsdb') into normal messages to simulate encoding errors or obfuscation. (Anomaly rate: 8.7%)
- Restaurant Review Dataset: Focuses on semantic anomalies. Sourced from Google Maps, it treats positive/neutral reviews as normal and flags explicit negative sentiment words/phrases as anomalous tokens. (Anomaly rate: 4.5%)
- Grammar Correction Dataset: Focuses on syntactic violations, utilizing paired grammatically correct and incorrect sentences from Kaggle. (Anomaly rate: 10%)
Key Insights and Ideas
- The PLM Semantic Bias: A fascinating finding is that PLMs like BERT are inherently biased toward semantic content over surface form. Consequently, TokenCore excels at detecting semantic anomalies (like negative reviews in a positive corpus) but can struggle with pure character corruption, where traditional statistical methods like ECOD or iForest perform better.
- Max Pooling Over Average Pooling: When handling subwords, standard practices use average pooling or take the first subword. The authors realized this dilutes anomaly signals. Max pooling ensures that if even a single subword looks highly unusual, the anomaly signal is preserved.
- Unified vs. Specialized: Instead of training separate models for grammar, spelling, and spam, a simple nearest-neighbor lookup against a "normal" memory bank provides a surprisingly robust, unified baseline for all three.
Key Results
TokenCore was evaluated against 6 traditional and deep learning anomaly detection baselines (including LOF, iForest, ECOD, Deep SVDD, AutoEncoder, and LUNAR).
As shown in Table 1, TokenCore achieved the highest average AUROC at both the token level (0.7145) and document level (0.7335). It particularly dominated the Review and Grammar datasets at the token level, though statistical methods like ECOD held an edge on the character-corrupted SMS Spam dataset.
Limitations and Future Work
Despite its strong performance, TokenCore faces a few notable hurdles:
- Computational Complexity: Searching a token-level memory bank scales quadratically ($O(nkm)$) with corpus size and document length. For large-scale deployments, integrating approximate nearest neighbor search (like FAISS) will be essential, though it introduces a speed-accuracy trade-off.
- Subword Ordering: Max pooling discards the structural order of subwords, which might contain critical diagnostic information for complex morphological errors.
- Aggregation Nuance: Mean pooling for document scores can dilute isolated, highly anomalous tokens. Finding a more principled multi-granularity aggregation strategy remains an open challenge.
Summary updated