Named Entity Recognition with Hugging Face Transformers: A Beginner’s Guide

Ganesh Lokare
7 min readFeb 7, 2023

--

What We Will Cover in This Blog

  1. Review of what Named Entity Recognition is and where we use it.
  2. How to recognize named entities with Hugging face Transformers (just a few lines of code)

The main focus of this blog, using a very high level interface for transformers which is the Hugging face pipeline. Using this interface you will see that we can recognize named entities from given text with just 1 or 2 lines of code.

What is Named Entity Recognition

Named Entity Recognition (NER) is a subtask of Natural Language Processing (NLP) that identifies and classifies named entities in a text into predefined categories such as person names, organizations and locations. The goal of NER is to extract structured information from unstructured text data and turn it into a machine-readable format.

How named entities recognized using Transformers

Named Entity Recognition (NER) in transformers is typically achieved using a multi-stage process that involves the following steps:

  1. Text pre-processing: Clean and pre-process the text to remove noise, such as HTML tags, special characters, and punctuation marks.
  2. Tokenization: Break the text into smaller units, called tokens, such as words, phrases, or sentences.
  3. Embedding: Represent each token as a dense vector, also known as an embedding, using pre-trained transformer-based models such as BERT or RoBERTa. These embeddings capture the context and semantic information of each token in the text.
  4. Named entity recognition: Use the information captured in the token embeddings to identify named entities and categorize them into predefined categories. This is typically done by training a classifier, such as a linear layer or a CRF layer, on top of the token embeddings.
  5. Validation and correction: Validate and correct the named entity recognition results using additional techniques, such as entity linking and coreference resolution, to ensure high accuracy and reliability.

During the named entity recognition stage, the token embeddings are passed through the classifier, which uses them to make predictions about the named entities in the text. The classifier can be trained on annotated training data, which includes labeled named entities, to learn to recognize named entities based on the token embeddings and the context they provide.

Transformers-based NER models have shown to achieve state-of-the-art performance on a variety of NER benchmarks and have been widely adopted in many NLP applications due to their ability to capture the context and relationships between tokens in a text effectively.

Use of Named Entity Recognition

Named Entity Recognition (NER) has a wide range of applications in various fields and industries, including:

  1. Information extraction: NER can be used to extract structured information from unstructured text data, such as news articles, scientific papers, or social media posts, to create machine-readable knowledge graphs or databases.
  2. Question answering: NER can be used in question answering systems to identify named entities in a question and use them to retrieve relevant information from a database or a knowledge base.
  3. Sentiment analysis: NER can be used in sentiment analysis to identify named entities, such as products or brands, in a text and determine the sentiment expressed towards them.
  4. Event extraction: NER can be used to extract information about events, such as date, time, location, and participants, from a text.
  5. Financial analysis: NER can be used in financial analysis to extract information about companies, stock prices, monetary values, and financial ratios from news articles, financial reports, and other sources.
  6. Chatbots and customer service: NER can be used in chatbots and customer service to extract information about customer requests and provide relevant information or assistance.

These are just a few examples of the many applications of NER. The ability to extract structured information from unstructured text data can provide valuable insights and support decision-making in many industries and fields.

Why use Transformers for Named Entity Recognition

Transformers are used for Named Entity Recognition (NER) due to the following reasons:

  1. Contextual information: Transformers are capable of capturing the context and relationships between words in a sentence, which is essential for accurate NER. By considering the context, transformers can disambiguate words that have multiple meanings and identify named entities more accurately.
  2. Pre-training: Transformers are pre-trained on large corpus of text data, which provides them with a vast knowledge of the language and the ability to generate high-quality token representations that can be fine-tuned for specific NER tasks.
  3. Transfer learning: Transformers can be fine-tuned for NER using a small annotated dataset, which reduces the amount of labeled data required to train NER models and makes it possible to adapt to new domains and languages easily.
  4. High accuracy: Transformers have shown to achieve state-of-the-art performance on various NER benchmarks and have been widely adopted in many NLP applications.
  5. Multi-lingual support: Transformers can be trained on multilingual text data, which makes them well-suited for NER tasks that require multilingual support, such as cross-lingual NER or multilingual NER.

In summary, transformers provide a powerful and flexible framework for NER, making it possible to extract structured information from unstructured text data effectively and efficiently.

Enough theory!!! Let’s code…

# install transformers
!pip install transformers

The command pip install transformers is used to install the transformers package, which provides access to state-of-the-art Transformer-based models for NLP tasks, including NER.

Once the transformers package is installed, you can import and use the Transformer-based models in your own projects.

from transformers import pipeline

# create pipeline for NER
ner = pipeline('ner', aggregation_strategy = 'simple')

The code above is using the pipeline function from the transformers library to create a Named Entity Recognition (NER) pipeline. The pipeline is a pre-configured, ready-to-use model for performing NER on text data.

The pipeline function takes two arguments:

  1. 'ner': The type of task for the pipeline, in this case Named Entity Recognition.
  2. aggregation_strategy='simple': The argument to specify the aggregation strategy for the pipeline. The aggregation strategy defines how the pipeline will combine the results of multiple models when multiple models are used in the pipeline. The 'simple' strategy is a simple aggregation method that concatenates the output of all models in the pipeline.

The ner variable in the code will store the NER pipeline, which can be used to perform NER on text data by calling the predict method on the pipeline and passing it the text to process.

The pipeline function uses the latest state-of-the-art models and provides a convenient and efficient way to perform NER without having to write complex code. It is especially useful for quickly testing and evaluating NER models and for using NER in production applications.

ner("Hi, my name is Ganesh Lokare. I am from Pune. I want to work with Google.")

The code above is using the ner pipeline created earlier to perform Named Entity Recognition (NER) on a sample sentence.

The ner function takes a string argument, which is the text to process, in this case "Hi, my name is Ganesh Lokare. I am from Pune. I want to work with Google.".

The NER pipeline will analyze the text and identify named entities in the sentence, such as person names, location names, and organization names. The output will be a list of dictionaries, where each dictionary represents a named entity and contains information such as the entity text, entity type, and entity start and end position in the text.

For example, the output for the sample sentence look like this:

This information can be used for various purposes, such as information extraction, sentiment analysis, or event extraction. The NER pipeline provides a convenient and efficient way to perform NER on text data and is especially useful for quickly testing and evaluating NER models and for using NER in production applications.

Next you can use it directly for your dataset.

In this blog , we covered the basics of Named Entity Recognition (NER), its importance in various applications and how to use pre-trained NER model from Hugging Face library. In next part, we will delve deeper into NER and explore more advanced topics.

First, we will discuss how to fine-tune NER models on custom datasets. This is crucial for those who have specific requirements that cannot be fulfilled by existing pre-trained models. We will go through the process of how to prepare your own dataset, fine-tune existing models, and evaluate their performance.

Next, we will move on to building NER models from scratch. We will discuss the various algorithms and models that can be used to perform NER and how to implement them. You will learn how to train, validate, and test NER models and how to optimize their performance.

If you enjoyed this post, be sure to check out some of my other blog posts for more insights and information. I’m constantly exploring new topics and writing about my findings, so there’s always something new and interesting to discover.

--

--