Coreference resolution is the task of identifying expressions that refer to the same entity. Getting this right improves machine understanding of text for summarization, question answering and information extraction.
My sister has a dog, named Billy. She loves him.
Here him refers back to a dog, named Billy, so the two expressions corefer.
2 Subtasks
| Subtask | Question it answers |
|---|---|
| Mention Detection | which spans in the text could refer to an entity |
| Mention Clustering | which of those mentions refer to the same entity |
Detection runs first and is deliberately generous, then clustering decides what actually groups together.
1. Mention Detection
Find all the candidate spans referring to some entity. There are 3 sources of candidates:
- Pronouns: words that substitute for nouns, such as I, you, we, they, he, she, it
- Named entities: spans found by Named Entity Recognition (NER) and classified into categories such as names, organizations, locations and products
- Noun phrases: a group of words headed by a noun and including its modifiers, such as
a dog, named Billy
2. Mention Clustering
- once the mentions are held, decide which ones refer to the same entity
- merge those mentions into the cluster that corresponds to that entity
- a sentence can produce several clusters, one per entity
Code
The library used is fastcoref, which runs on top of a spaCy pipeline.
from fastcoref import FCoref
import spacy
nlp = spacy.load("en_core_web_lg")
model = FCoref(nlp=nlp)
texts = [
"My sister has a dog, named Billy. She loves him.",
"Peter likes the company because it provides great benefits. He earned a lot of money.",
"Alice goes down the rabbit hole. Where she would discover a new reality beyond her expectations."
]
result = model.predict(texts=texts)
for res in result:
print(f'Text: {res.text}\nClusters: {res.get_clusters()}\n')Text: My sister has a dog, named Billy. She loves him.
Clusters: [['a dog, named Billy', 'him']]
Text: Peter likes the company because it provides great benefits. He earned a lot of money.
Clusters: [['the company', 'it'], ['Peter', 'He']]
Text: Alice goes down the rabbit hole. Where she would discover a new reality beyond her expectations.
Clusters: [['Alice', 'she', 'her']]
Reading the output
get_clusters()returns a list of clusters, and each cluster is a list of the surface strings that refer to 1 entity. The second text has 2 clusters because it names 2 entities. A mention that corefers with nothing is not returned.
- the model loads
en_core_web_lg, the large spaCy model, not the small one - clusters hold the raw text of each mention, so the noun phrase
a dog, named Billyis kept whole
Limitations
These apply to coreference resolution and to semantic role labeling alike.
| Limitation | Why it is hard |
|---|---|
| Ambiguous pronouns and noun phrases | pronouns such as "he", "she" or "it" can refer to several entities, and noun phrases shift meaning with context |
| Long-distance dependencies | linking mentions that sit far apart in the text is difficult |
| Scalability | computationally intensive, which makes it hard to apply to large datasets |
| Need for world knowledge | many coreference links are never stated, so they need background knowledge or common sense to infer |