AD - NLP : A Benchmark for Anomaly Detection in Natural Language Processing (2023)

Abstract: Deep learning models have reignited the interest in Anomaly Detection research in recent years. Methods for Anomaly Detection in text have shown strong empirical results on ad-hoc anomaly setups that are usually made by downsampling some classes of a labeled dataset. This can lead to reproducibility issues and models that are biased toward detecting particular anomalies while failing to recognize them in more sophisticated scenarios. In the present work, we provide a unified benchmark for detecting various types of anomalies, focusing on problems that can be naturally formulated as Anomaly Detection in text, ranging from syntax to stylistics. In this way, we are hoping to facilitate research in Text Anomaly Detection. We also evaluate and analyze two strong shallow baselines, as well as two of the current state-of-the-art neural approaches, providing insights into the knowledge the neural models are learning when performing the anomaly detection task. We provide the code for evaluation, downloading, and preprocessing the dataset at https://github.com/mateibejan1/ad-nlp/.

## introduction an anomaly, sometimes referred to as an outlier, discordant, or novelty, can be intuitively described as an observation that appears to be inconsistent with the remainder of that set of data @xcite to the degree that it arouses suspicion @xcite . such an observation can be described as being atypical, irregular, erroneous, or simply strange @xcite . anomalies are relative to the data distribution at hand and can be perceived as having characteristics that are not definitory to the vast majority of the population. researchers have been interested in the anomaly detection (ad) problem for several decades @xcite @xcite , some of the first formal mentions of "discordant observations" going back to the 19th century @xcite . both general anomaly detectors @xcite @xcite and narrowscoped ones @xcite @xcite have shown promising results in various domains, such as network monitoring @xcite @xcite , time series @xcite , computer vision @xcite , and natural language processing @xcite . the advent of deep learning methods for detecting anomalies amplified the interest in the field, producing a great variety of models such as ones based on generative networks @xcite , self-supervised learning @xcite , or one-class classification @xcite . although the field has seen a sprout of activity, most of the introduced datasets for anomaly detection are being specifically crafted for computer vision, intrusion detection systems @xcite or network traffic @xcite . approaches in nlp are usually benchmarked on ad-hoc setups, typically by making use of an annotated dataset and downsampling some classes to produce outliers @xcite @xcite . this does not represent an impediment in computer vision, due to the large number and variety of annotated datasets. in natural language processing, however, deciding what an anomaly is and what it is not is a delicate matter. this problem is rooted in the intrinsic complexity of the text: the form the language of a text takes is influenced not only by the style and lexicon of the author but also by the literary or historical era it was written in and its purpose (e.g., newspaper article, novel, letter, satire, etc.). thus, anomalies can be defined through a multitude of factors, ranging from the concrete syntax, style, and grammar, to the more abstract semantics, metaphorical meaning, and contextual topic. a notable aspect of certain linguistic phenomena that can be effectively analyzed with anomaly detection (ad) is the challenge posed by the lack of clear negative examples. for instance, while there are countless texts that were not written by shakespeare, determining which ones serve as a good representation of non-shakespearean texts can be tricky. in a similar vein, identifying a typical non-scientific text can also be challenging. we introduce ad-nlp: an anomaly detection setup for natural language processing which can be used to benchmark ad systems on syntactic anomalies, semantic anomalies, pragmatic anomalies and stylistic anomalies, by aggregating different tasks and treating them as a general anomaly detection task, such as: sentence acceptability detection, topic detection, metaphor detection and autorship detection. furthermore, we experiment with two strong classical baselines and two recent deep methods for detecting anomalies in text and make observations on both the quantitative and qualitative results of the models. in the following, we will use the terms "anomaly" and "outlier" interchangeably to refer to the same concept of divergent observation with respect to the overall data distribution. the paper is organized as follows: section 2 covers related work. in section 3, we outline our task definition, data collection approach, and datasets. sections 4 and 5 delve into the models we used and our experimental assessment. finally, we wrap up with our conclusions in section 6. ## conclusions we introduce ad-nlp, a benchmark for anomaly detection in text over an extensive assortment of outlier scenarios, covering syntactic, semantic, pragmatic, and stylistic language anomalies. additionally, we introduce three new datasets as part of ad-nlp: song genres, gutenberg categories, and gutenberg authors. song genres provides a complex setting in which part of information about the data has been obscured, enforcing a distinct focus on the subtle differences between texts, while the two datasets derived from the gutenberg data are meant to provide variety on multiple levels: syntax, style, genre, and literary movement. we find that anomalies that depend solely on semantic or stylistic aspects of the text are easier to recognize, whereas those that only partially depend on the text, like song lyrics, are harder to detect and separate. we have also disclosed our results on various models and found out that the neural models react well to domain-specific words, author idiosyncrasies and punctuation as being anomalous or not. we hope that the proposed benchmark and tools will facilitate research in text anomaly detection.