Skip to content
SDB
Natural Language Processing

Chapter 06 · intermediate · 22 min

Discourse & Pragmatics

Coreference resolution, discourse structure, speech acts, sarcasm, and pragmatic interpretation

Subhendu Datta BhowmikAI Tutorials

Discourse: Meaning Beyond a Single Sentence

Discourse analysis studies how sequences of sentences form coherent text or dialogue. A sentence's meaning often cannot be determined in isolation:

"The trophy didn't fit in the suitcase because it was too big."

What does "it" refer to? The trophy or the suitcase? Resolving this requires understanding the entire discourse, not just the sentence.

Discourse Coherence

A coherent discourse has sentences that are logically connected. Coherence relations describe how adjacent sentences relate:

RelationExample
Cause-Effect"She studied hard. She passed the exam."
Contrast"He's rich. However, he's unhappy."
Elaboration"He owns three cars. Two are Ferraris."
Temporal"First he woke up. Then he made coffee."
Purpose"She went to the store to buy milk."

RST (Rhetorical Structure Theory) is the formal framework for modeling discourse coherence — it builds a tree of nucleus-satellite relations over the text.

Discourse Markers

Words and phrases that signal coherence relations explicitly:

  • Contrast: "however", "but", "although", "on the other hand"
  • Cause: "because", "therefore", "consequently", "as a result"
  • Addition: "furthermore", "in addition", "moreover"
  • Temporal: "first", "then", "finally", "subsequently"

NLP models often use discourse markers as features for discourse relation classification.

Coreference Resolution

Types of Anaphoric Relations

TypeExampleResolution
Pronominal anaphora"Alice left. She forgot her keys."She = Alice
Nominal anaphora"A cat walked in. The animal sat down."the animal = the cat
Zero anaphora"Alice left. [She] forgot her keys." (some languages)implicit subject
Cataphora"Before he left, John locked the door."he = John (forward)
Bridging"I bought a car. The engine is noisy."Inferred: engine of the car

Coreference Resolution Pipeline

Text
  ↓  Mention detection (find all candidate mentions)
Candidate mentions (NPs, pronouns, proper names)
  ↓  Mention ranking (score pairs for coreference likelihood)
Coreference links
  ↓  Clustering (group coreferent mentions)
Coreference clusters (one cluster per entity)

Approaches

Rule-based (Hobbs Algorithm): traverse the syntax tree to find the nearest compatible antecedent. Simple but handles only simple pronouns.

Statistical (mention-pair model): Score all pairs (mi,mj)(m_i, m_j) with features (gender, number, animacy, distance, syntax).

Neural (end-to-end): SpanBERT-based models jointly detect mentions and score coreference — state-of-the-art on OntoNotes benchmark (>80% F1).

The Winograd Schema Challenge

Winograd schemas are sentence pairs that differ by one word and require world knowledge to resolve:

  • "The trophy didn't fit in the suitcase because it was too big." → it = trophy
  • "The trophy didn't fit in the suitcase because it was too small." → it = suitcase

These require commonsense reasoning, not just linguistic patterns — a benchmark for AI understanding.

Pragmatics: Meaning in Context

Pragmatics studies how context and speaker intent shape the meaning of an utterance beyond its literal content.

Speech Acts (Austin & Searle)

Every utterance performs a speech act — an action accomplished through language:

Speech Act TypeExampleActual Function
Assertion"It's raining."States a fact
Question"Is it raining?"Requests information
Directive"Close the window."Requests action
Commissive"I'll call you tomorrow."Commits to action
Expressive"Congratulations!"Expresses attitude
Declaration"You're fired."Changes reality

Indirect speech acts: "Can you pass the salt?" is literally a yes/no question about ability, but pragmatically a polite directive (request).

Gricean Maxims

Grice's cooperative principle: speakers contribute what is required by the accepted purpose of the conversation. It has four maxims:

  1. Quantity: Be as informative as required (no more, no less)
  2. Quality: Be truthful; don't say what you believe to be false
  3. Relation: Be relevant
  4. Manner: Be clear, brief, orderly; avoid ambiguity

Implicature arises when a maxim is apparently violated, forcing the listener to infer the intended meaning:

  • "Some students passed the exam." → implicates not all (violates quantity if all passed)
  • "Can you reach the salt?" → implicates a request (relevance)

Sarcasm and Irony in NLP

Sarcasm says the opposite of what is meant for rhetorical effect:

  • "Oh great, another Monday." (speaker dislikes Mondays)
  • "Fantastic weather!" (said during a storm)

Challenges for NLP:

  • Contradicts sentiment lexicon signals (positive words, negative sentiment)
  • Often lacks explicit markers; requires world knowledge and context
  • Performs poorly with lexicon-based sentiment analysis

Detection approaches:

  • Rule-based: quotation marks, hyperbolic words, "yeah right", "#sarcasm"
  • ML: train on labeled datasets (SemEval sarcasm task)
  • Multimodal: tone of voice, facial expression help in spoken sarcasm

Metaphor

Conceptual metaphor: understanding one concept through another:

  • "Argument is war": "He attacked every weak point. She defended her position."
  • "Time is money": "I spent an hour on this. Don't waste my time."

Metaphor violates compositionality — literal meaning is incorrect. NLP models (especially pre-2015) struggled; transformer models handle common metaphors well but fail on novel ones.

Coreference Resolution and Discourse Analysispython
# ─── 1. Coreference with spaCy + neuralcoref ────────────────
# pip install spacy neuralcoref
import spacy
import neuralcoref

nlp = spacy.load("en_core_web_sm")
neuralcoref.add_to_pipe(nlp)

text = """Barack Obama was born in Hawaii. He served as the 44th President of the
United States. Obama's administration focused on healthcare reform. The president
signed the Affordable Care Act into law."""

doc = nlp(text)
print("Coreference Clusters:")
for cluster in doc._.coref_clusters:
    print(f"  Main mention: '{cluster.main}'")
    print(f"  All mentions: {[str(m) for m in cluster.mentions]}")
    print()

# Resolve coreferences (replace pronouns with canonical mention)
print("Resolved text:")
print(doc._.coref_resolved)

# ─── 2. Coreference with AllenNLP (more accurate) ────────────
from allennlp.predictors.predictor import Predictor

coref_predictor = Predictor.from_path(
    "https://storage.googleapis.com/allennlp-public-models/coref-spanbert-large-2021.03.10.tar.gz"
)

result = coref_predictor.predict(document=text)
print("\nAllenNLP Coreference clusters:")
words = result["document"]
for cluster in result["clusters"]:
    mentions = [" ".join(words[start:end+1]) for start, end in cluster]
    print(f"  Cluster: {mentions}")

# ─── 3. Discourse Marker Detection ──────────────────────────
import re

DISCOURSE_MARKERS = {
    "contrast": ["however", "but", "although", "nevertheless", "on the other hand", "yet"],
    "cause": ["because", "therefore", "consequently", "as a result", "thus", "hence"],
    "addition": ["furthermore", "moreover", "in addition", "also", "additionally"],
    "temporal": ["first", "then", "finally", "subsequently", "meanwhile", "after"],
    "concession": ["although", "even though", "despite", "while", "whereas"],
}

def detect_discourse_markers(text):
    found = {}
    text_lower = text.lower()
    for relation, markers in DISCOURSE_MARKERS.items():
        found_markers = [m for m in markers if re.search(r'\b' + m + r'\b', text_lower)]
        if found_markers:
            found[relation] = found_markers
    return found

sample = """The economy grew significantly last year. However, inflation also rose.
Furthermore, unemployment remained low. As a result, consumer confidence increased.
Although wages grew, purchasing power decreased because prices rose faster."""

markers = detect_discourse_markers(sample)
print("\nDiscourse markers found:")
for relation, found in markers.items():
    print(f"  [{relation}]: {found}")

# ─── 4. Basic Sarcasm Detection ──────────────────────────────
from transformers import pipeline

# Fine-tuned sarcasm classifier
sarcasm_clf = pipeline("text-classification",
                       model="cardiffnlp/twitter-roberta-base-irony")

test_sentences = [
    "Oh great, my flight got cancelled. Just what I needed.",
    "I had a wonderful day at the beach!",
    "Yeah right, because that always works.",
    "The conference was genuinely inspiring and well-organized.",
]

print("\nSarcasm Detection:")
for sent in test_sentences:
    result = sarcasm_clf(sent)[0]
    print(f"  [{result['label']:<8} {result['score']:.2f}] {sent[:60]}")

Knowledge check

Someone says "Nice weather we're having" during a thunderstorm. Which Gricean maxim is being flouted, and what is the implicature?

Summary

  • Discourse coherence connects sentences through relations (cause, contrast, temporal); RST models discourse structure as trees
  • Coreference resolution identifies all mentions of the same entity; SpanBERT-based models achieve >80% F1
  • Winograd schemas require world knowledge for coreference — a key AI benchmark
  • Speech acts describe the communicative function of utterances (assertive, directive, commissive, etc.)
  • Gricean maxims (quantity, quality, relation, manner) explain how implicature arises from cooperative communication
  • Sarcasm violates the Quality maxim — NLP must go beyond literal meaning using context and pragmatic inference

Next: Text Representation & Embeddings — converting text into numerical vectors for ML models.

Natural Language Processing