- Type
- NLP text-classification task
What does it do?
Sentiment analysis, also called opinion mining, is a natural-language-processing task that detects the emotional tone of text — typically classifying it as positive, negative, or neutral. It turns unstructured opinion text into structured, quantifiable polarity signals using one of three families of approaches: sentiment lexicons, trained machine-learning classifiers, or pretrained transformer models.
The intuition behind the method
People express opinions in free-form language, but a machine cannot read that text the way a human does. The goal of sentiment analysis is to detect the underlying opinion or polarity in a piece of text and attach a label to it. Instead of judging each sentence by intuition, the method scores wording through dictionaries of charged words, statistical patterns learned from labelled examples, or context captured by a language model, and assigns each document a sentiment label. This page covers document-level polarity, where one label is given to a whole document; sentence-level and aspect-level sentiment are related tasks that use different labelling schemes.
History
The task is closely associated with the opinion-mining survey by Pang and Lee (2008), which covers the computational treatment of opinion, sentiment, and subjectivity in text and was motivated by the growth of online review sites and personal blogs. That survey predates the pretrained transformer models described on this page, so the later shift toward those models is not documented by the cited source.
When to use it
Sentiment analysis fits when you have text whose opinion or polarity matters for your question, and a lexicon or model that matches the language and domain of that text. With no labelled data, a lexicon-based approach is the usual starting point, but its accuracy cannot be estimated without labels, so a small held-out labelled sample should still be annotated to check its output before the results are used. Supervised classifiers need a labelled training set that is large enough and balanced enough to represent the classes you care about; if you cannot build one, or the labels are unreliable, a lexicon-based method or a pretrained model is the safer choice. Do not use the method when your text is not opinion-bearing (for example, factual reports), when you need the target of the opinion or the reasons behind it rather than only its polarity, or when there is no text data at all.
Limitations and considerations
- Supervised classifiers learn only what their labelled training set shows; label quality and class balance therefore set the ceiling on what the model can learn.
- Lexicon-based scoring relies on dictionary entries and rules; words whose polarity depends on context, such as negated or domain-specific terms, are misread unless the rules explicitly handle them.
- Sarcasm, negation, and domain shift remain hard for all three approaches, so the output is an estimate that needs checking against labelled examples.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- All three approaches share the same four-stage pipeline (preprocess, represent, model, evaluate), so a lexicon baseline and a transformer can be compared on the same corpus and the same labelled sample.
- A lexicon-based approach needs no labelled training data, which makes it usable when annotation is not available.
- The output is a polarity label for each document, which can be aggregated across a corpus to track sentiment over time or across groups.
Applications
- Analysing product and service reviews to summarise customer opinion.
- Monitoring social media posts for public reaction and sentiment trends.
- Brand monitoring — tracking how an organisation or product is perceived over time.
- Exploring and classifying large opinion corpora in operations and marketing research.
How it works
How it works
The raw corpus is cleaned and standardised: text is lowercased, noise is stripped, content is normalised, and documents are segmented (tokenised) into analysable units. Consistent preprocessing is what keeps the downstream features comparable across documents.
The cleaned text is converted into features a model can consume. Depending on the approach this means sentiment-lexicon lookups, TF-IDF vectors, or contextual embeddings, each turning words into numbers that carry the signal the model will read.
A polarity label is assigned to each document. A lexicon-based approach applies dictionary rules, a machine-learning approach uses a classifier trained on labelled data, and a transformer-based approach applies a pretrained language model that captures context. The output is a sentiment label per document. The neutral label depends on how the boundary between polar and neutral text is set, for example by a score band or an annotation guideline; this boundary should be defined explicitly and reported together with the results.
Predictions are scored against labelled data using accuracy, F1, precision, and recall, and misclassifications are inspected before the model is put to use. Precision is TP / (TP + FP) and recall is TP / (TP + FN), where TP, FP and FN are the true positives, false positives and false negatives for a given class. F1 is 2 × precision × recall / (precision + recall). These measures are computed per class, and macro-averaged F1, which gives each class equal weight, is a common summary when classes are imbalanced. This step guards against over-trusting a model that performs poorly on certain classes.
Common pitfalls
- Sarcasm and figurative language can invert the apparent polarity, so a literal reading mislabels the text.
- Domain shift, meaning applying a lexicon or model built for one domain or language to another, degrades accuracy; an English lexicon should not be applied to text in another language.
- Imbalanced labels make accuracy misleading; report F1, precision, and recall per class rather than accuracy alone.
- Aggressive preprocessing can delete polarity signal: removing stop words can remove negators such as "not", and lowercasing or stripping punctuation can remove emphasis cues such as all-caps words or repeated punctuation, so check that cleaning steps leave these tokens intact.
- A supervised classifier trained on a small labelled set can fit its training data while generalising poorly; judge it on held-out labelled data, and if labelled data is scarce, use a lexicon-based method instead.
Frequently asked questions
Lexicon, machine learning, or transformer — which approach should I use?
A lexicon-based approach applies a dictionary of sentiment-bearing words and needs no labelled training data. A machine-learning classifier learns patterns from a labelled training set, so its quality depends on how well that set represents your domain. A transformer-based model uses a pretrained language model to capture context, at higher computational cost. The right choice depends on your corpus, available labels, and computing resources, and is best settled by comparing candidates on a labelled sample.
How accurate is sentiment analysis?
Accuracy varies with the approach, the corpus, and how well the model matches the text. Evaluate with accuracy, F1, precision, and recall on held-out labelled data; on imbalanced classes, accuracy alone can be misleading, so rely on F1, precision, and recall.
Can it work in languages other than English?
Yes, but the chosen language model or lexicon must match the target language of the corpus. Applying an English lexicon or model to text in another language is a common cause of poor results.
How are negation and sarcasm handled?
These remain among the hardest cases. Negation can flip polarity and sarcasm can invert the literal meaning entirely. Transformer models can capture context that simple lexicons miss, but how well any approach handles negation and sarcasm varies with the corpus, and no approach handles sarcasm reliably, so error inspection on labelled examples from your own domain matters during evaluation.
Related methods
Sources
Pang, B. & Lee, L. (2008). Opinion Mining and Sentiment Analysis. Foundations and Trends in Information Retrieval, 2(1-2), 1-135.
Open DOI reference