
Python Natural Language Processing Cookbook
By :

When we work on problems of extracting entities and relations from text (see the Extracting entities and relations recipe), we are faced with real text, and many of our entities might end up being extracted as pronouns, such as she or him. In order to tackle this issue, we need to perform anaphora resolution, or the process of substituting the pronouns with their referents.
For this task, we will be using a spaCy
extension written by Hugging Face called neuralcoref
(see https://github.com/huggingface/neuralcoref). As the name suggests, it uses neural networks to resolve pronouns. To install the package, use the following command:
pip install neuralcoref
Your steps should be formatted like so:
spacy
and neuralcoref
:import spacy import neuralcoref
spaCy
engine and add neuralcoref
to its pipeline:nlp = spacy.load('en_core_web_sm') neuralcoref.add_to_pipe(nlp)
text = "Earlier this year, Olga appeared on a new song. She was featured on one of the tracks. The singer is assuring that her next album will be worth the wait."
neuralcoref
is part of the pipeline, we just process the text using spaCy
and then output the result:doc = nlp(text) print(doc._.coref_resolved)
The output will be as follows:
Earlier this year, Olga appeared on a new song. Olga was featured on one of the tracks. Olga is assuring that Olga next album will be worth the wait.
In step 1, we import the necessary packages. In step 2, we load the spacy
engine and then add neuralcoref
to its pipeline. In step 3, we initialize the text
variable with the short text we will be using.
In step 4, we use the spacy
engine to process the text and then print out the text with the pronouns resolved. You can see that the pronouns she and her, and even the phrase The
singer
, were all correctly substituted with the name Olga
.
The neuralcoref
package uses custom spacy
attributes that are set by using an underscore and the attribute name. The coref_resolved
variable is a custom attribute that is set on a Doc
object. To learn more about spaCy
custom attributes, see https://spacy.io/usage/processing-pipelines#custom-components-attributes.
The neuralcoref
package did a good job of recognizing different references to Olga
in the previous section. However, if we use an unusual name, it might not work correctly. Here, we are using an example from the Hugging Face GitHub:
text = "Deepika has a dog. She loves him. The movie star has always been fond of animals."
Deepika has a dog. Deepika loves Deepika. Deepika has always been fond of animals.
neuralcoref
to the spacy
pipe, as follows:neuralcoref.add_to_pipe(nlp, conv_dict={'Deepika': ['woman']})
doc = nlp(text) print(doc._.coref_resolved)
The output will be as follows:
Deepika has a dog. Deepika loves a dog. Deepika has always been fond of animals.
Once we give the coreference resolution module more information, it gives the correct output.