Sentiment, Opinion and Text Mining

Sarcasm and irony detection

Sarcasm detection tries to catch when the literal words of a sentence mean the opposite of what the writer actually feels.

Read these first

On this page 5
  1. Why it exists
  2. How it works
  3. Where you have already seen it
  4. Remember this
  5. What to learn next

One lesson, three depths. Pick the one that fits you today — you can switch any time.

Beginner — No maths. Plain English.

Sarcasm detection tries to catch when the words of a sentence mean the opposite of what the writer actually feels.

Picture a friend rolling their eyes and saying "oh, brilliant" after you drop your phone screen-first onto concrete. The words say "brilliant." The eye roll says the exact opposite.

Text has no eye roll. "Oh, brilliant" typed after a bad event has to be read as sarcasm from context alone. That is the entire problem sarcasm detection tries to solve.

Why it exists

A plain sentiment model reads words at face value. "Great, another Monday meeting" contains the word "great," so a naive model calls it positive. A human reading the full sentence knows better instantly.

This is not a rare edge case. Product reviews, social media and customer feedback are full of sarcasm. Every time it is missed, it flips a sentiment score exactly backwards.

Sarcasm detection exists to catch this reversal before it corrupts anything built on top of plain sentiment scores.

How it works

"Oh great, another Monday meeting."
                |
                v
Positive words ("great") + negative context (Monday meeting, "oh")
                |
                v
Read as: sarcastic, actually negative

Sarcasm detection usually leans on a mismatch: literal word sentiment against surrounding context. Tone markers like "oh" or "wow," and real-world knowledge about what people usually feel, both help.

Where you have already seen it

  • Review platforms that flag sarcastic reviews for human review. A five-star sarcastic complaint is worse for a business than an honest one-star review.
  • Social media sentiment dashboards during a controversy. Sarcastic praise, taken at face value, can badly skew a public sentiment reading.
  • Customer support systems that route "great, another broken order" correctly, not as a compliment.
  • Content moderation tools trying to separate genuine praise from mockery. Getting this wrong sends exactly the wrong message to exactly the wrong person.

Remember this

  • Sarcasm makes a sentence's literal sentiment the opposite of its real meaning.
  • Missing it flips a sentiment score in exactly the wrong direction, not only a little off.
  • It remains one of the hardest problems in text sentiment work, for models and for people.

What to learn next

Developer — Code and libraries.

Below, a zero-shot classification model decides sarcastic or sincere, without ever being specifically trained on sarcasm examples. Zero-shot means the model was never shown labelled data for this exact task; it works from the label names alone.

Setup

bash
pip install transformers torch

The first run downloads deberta-v3-xsmall-zeroshot-v1.1-all-33, about 150 MB. Later lessons in this section reuse the same cached model.

Classifying three messages

sarcasm.py
from transformers import pipeline

classifier = pipeline(
    "zero-shot-classification",
    model="MoritzLaurer/deberta-v3-xsmall-zeroshot-v1.1-all-33",
)

texts = [
    "Oh great, another Monday meeting that could have been an email.",
    "The team shipped the fix within an hour of the bug report, genuinely impressive.",
    "Wow, my flight got delayed again, exactly what I needed today.",
]
labels = ["sincere", "sarcastic"]

for text in texts:
    result = classifier(text, candidate_labels=labels)
    print(f"{result['labels'][0]:10s} ({result['scores'][0]:.3f})  {text}")
Output
sarcastic  (0.834)  Oh great, another Monday meeting that could have been an email.
sincere    (0.995)  The team shipped the fix within an hour of the bug report, genuinely impressive.
sarcastic  (0.681)  Wow, my flight got delayed again, exactly what I needed today.

Line by line

candidate_labels=["sincere", "sarcastic"] is the entire "training" this task gets. The model was never fine-tuned on sarcasm examples specifically. It reasons about which label the text better fits, using knowledge learned from broad general-purpose training.

All three results are plausible, but confidence varies noticeably. The genuinely enthusiastic message scored 0.995 sincere, very confident. The two sarcastic messages scored 0.834 and 0.681, correct, but visibly less certain.

The third message's lower confidence is worth noting honestly. "Exactly what I needed today" is a fairly common sarcasm marker. Still, the model was less sure than on the first example. Sarcasm confidence tends to drop as the cues get more indirect.

Common mistakes

Treating zero-shot sarcasm detection as reliable enough for high-stakes decisions. It is a genuinely useful first pass, not a finished product. Sarcasm is famously hard even for trained specialist models, and harder still for a model given no task-specific examples.

Ignoring platform and cultural context. Sarcasm markers differ across languages, communities and even individual writing styles. A model trained mostly on English internet text will read sarcasm in other contexts less reliably.

Assuming sarcasm always sounds negative underneath. Sarcasm can also mask mild positivity, or land somewhere ambiguous. Do not assume "sarcastic" always means "flip to negative," and stop there.

Skipping human review on flagged content that matters. Even a well-scored sarcastic case carries real uncertainty. Treat automatic sarcasm flags as a prioritisation signal, not a final verdict, wherever the outcome matters.

Try it yourself

Try "Sure, take your time, it's not like I'm waiting or anything." Sarcasm here has no clear positive word at all, unlike "great" or "wow." See whether the model still catches it.

What to learn next

Researcher — Mathematics and papers.

Why sarcasm resists standard classification

Sarcasm is a pragmatic phenomenon. Its meaning depends on context, shared knowledge and speaker intent, not on the sentence's literal content alone. This puts it outside what token-level sentiment scoring can, in principle, fully solve. Two sentences with near-identical literal sentiment words can carry opposite real meaning. It depends entirely on context a model may or may not have access to.

Joshi, Bhattacharyya & Carman (2017) survey the field and find a consistent pattern. Purely lexical features, word presence and sentiment lexicons, perform poorly. Features capturing incongruity, a mismatch between expected and stated sentiment, perform meaningfully better.

Modelling approaches

Contextual and conversational features. Sarcasm frequently depends on context outside the sentence itself. A reply thread, a known fact about the situation, or common ground between speaker and reader, all count. Bamman & Smith (2015) show that adding author and audience context measurably improves detection accuracy over sentence-only models.

Incongruity-based architectures. Tay et al. (2018) propose the Multi-dimension Gated Attention network. It explicitly models contrast between different parts of a sentence, instead of encoding the whole thing into one vector. The aim: capture sarcasm's sentiment mismatch directly.

Zero-shot and few-shot approaches, as used in the developer block, reframe detection as natural language inference. Does the text entail "this is sincere" or "this is sarcastic," given the model's broad pretraining? This trades task-specific accuracy for zero setup cost, useful when no labelled sarcasm data exists for a domain.

Datasets and their limits

SARC (Khodak, Saveski & Tan, 2018) is a large-scale Reddit-derived sarcasm dataset. It uses the community's own /s sarcasm marker as a weak label. This is a distant supervision signal: convenient at scale, but noisy. Not all sarcasm is self-tagged, and self-tagged sarcasm may differ systematically from unmarked sarcasm in the same community.

The iSarcasm dataset (Oprea & Magdy, 2020) instead asks authors directly whether their own post was sarcastic. This produces cleaner labels at far smaller scale. It also reveals something important. Human annotators read the same text with no author context. They agree with the author's own label only about 65-70% of the time. That is a useful upper bound on how hard this task genuinely is.

Evaluation

Standard classification metrics, precision, recall, F1, apply directly. The iSarcasm result above matters for interpreting them. If human agreement on gold labels tops out around 70%, no classifier should realistically exceed that ceiling, regardless of architecture.

Key references

  • Joshi, A., Bhattacharyya, P. & Carman, M. (2017). Automatic Sarcasm Detection: A Survey. ACM Computing Surveys.
  • Bamman, D. & Smith, N. (2015). Contextualized Sarcasm Detection on Twitter. ICWSM.
  • Tay, Y. et al. (2018). Reasoning with Sarcasm by Reading In-between. arXiv:1805.02856
  • Khodak, M., Saveski, M. & Tan, C. (2018). A Large Self-Annotated Corpus for Sarcasm. arXiv:1704.05579
  • Oprea, S. & Magdy, W. (2020). iSarcasm. arXiv:1911.03123

Current state and open problems

LLMs perform noticeably better at sarcasm detection than earlier lexical or purely neural models. Broad pretraining implicitly captures more of the world knowledge sarcasm depends on. They still make systematic errors on subtle or culturally specific sarcasm.

The open problem is the ceiling itself. Even human annotators disagree with an author's own sarcastic intent roughly a third of the time. This suggests sarcasm detection may have an inherent accuracy limit, one no model can cleanly exceed. The "correct" label is itself sometimes genuinely ambiguous, to a reader without the author's private context.

What to learn next