Named Entity Recognition with Spacy and the Mighty roBERTa | by Zoumana Keita | Towards Data Science

When reading a text, a human being can naturally identify named entities like dates, currencies, locations, person names, medical codes, brands, etc. This step can then be relevant for further information extraction from large text data in order to better answer questions such as:
what is the name of the covid19 variant trending on the news?
what tools are mentioned in a given job description and at what level of proficiency?
etc.
The goal of this article is to use spaCy and roBERTa from spaCy transformers in order to automatically identify and extract entities as defined early in the introduction.
We will be performing the entity extraction of two different text data, a short text and a much longer one respectively (from CNN and Etoro) and finally, compare the performance of traditional spaCy and roBERTa.
A shorter text: “Amy Schneider, an engineering manager from Oakland, California, became the first woman and the fourth person on “Jeopardy!” to earn more than $1 million in winnings on Friday’s episode”.
Longer text: “Good news for consumers, undoubtedly, and good news also for investors. Apple’s recent results, covering the three months to December 31 2016, saw the company’s chief financial officer Luca Maestri announce: ‘We returned nearly $15 billion to investors through share re-purchases and dividends during the quarter.’ The quarterly dividend itself was 57 cents a share, identical to the dividend for the previous three quarters and up on the 52 cents paid for each of the four quarters before that.Business is brisk at Apple. On January 31, Tim Cook, Apple’s chief executive, said of the last three months of 2016: ‘We’re thrilled to report that our holiday quarter results generated Apple’s highest quarterly revenue ever, and broke multiple records along the way. We sold more iPhones than ever before and set all-time revenue records for iPhone, Services, Mac and Apple Watch”
This is about installing all the required libraries for the analysis.
The first function print_entities is used to perform the named entity extraction from a given text and pipeline (traditional spaCy or spaCy transformer in our case). It works as follow:
pipeline is the language object we get from thespacy.loadinstruction. It contains all the components and data required to process the text
pipeline(text)runs pipeline and returns processed document
named entities are obtained by iterating over the document.entsobject.
then at each iteration, we can access the entity text from.textand the entity label from .label_
The second function visualize_entities executesdisplacy.render function to produce a nice visualization of all the entities.
Now that we have installed all the libraries and defined our named entity extraction function, we can proceed with the analysis.
An opensource library for industrial-strength NLP, and has been trained on the OntoNotes 5.0 corpus.
Analysis of the short text
print_entities(nlp_sm, short_text)
visualize_entities(nlp_sm, short_text)
Traditional spaCy successfully identified CNN as an Organisation (ORG), Amy Schneider as a PERSON, Oakland, and California as Geo-Political Entity (GEP), etc. You can find the exhaustive list of the definition of the entity codes from the Entity Names Annotation section of this file.
Analysis of the long text
print_entities(nlp_sm, long_text)
visualize_entities(nlp_sm, short_text)
Observation: For the short text, the result looks perfect, because all the entities have been correctly identified. However, for the second text, the model made the following mistakes by
misidentifying Apple Watch to be an ORG (Organization) instead of a Product.
missing to identify iPhone and Mac.
Let’s see how the roBERTa model behaves on the same texts.
This is from spacy-transformerslibrary introduced by spaCy in 2019. It aims to power spacy pipelines by connecting spaCy to HuggingFace’s transformer models.
Analysis of the short text
print_entities(roberta_nlp, short_text)
visualize_entities(roberta_nlp, short_text)
So far, so good, nothing changes compared to the result of the traditional spaCy on the short text.
Analysis of the long text
print_entities(roberta_nlp, long_text)
visualize_entities(roberta_nlp, long_text)
The following summary table contains the result of both traditional spacy and roBERTa on the long text. We can clearly observe that roBERTa outperformed traditional spacy. roBERTa failed only once by not identifying Mac. On the other hand, traditional spacy fails to identify iPhone and Mac but also misidentified Apple Watch.
I hope this article was useful. Try it yourself by running the code from my Github from the link below. Happy Monday!
Github
Spacy for English
Spacy transformers
Bye for now 🏃🏾