How To Implement Named Entity Recognition In Python With SpaCy, BERT, NLTK & Flair

by | Dec 6, 2022 | Machine Learning, Natural Language Processing

What is Named Entity Recognition (NER)?

Named entity recognition (NER) is a part of natural language processing (NLP) that involves finding and classifying named entities in text. Named entities are words or phrases that refer to specific real-world objects, such as people, organisations, locations, etc.

For example, in the sentence “Barack Obama was the 44th president of the United States,” the named entities would be “Barack Obama” and “United States.” Named entity recognition can help to extract meaningful information from the text and organise it in a structured format for further analysis.

United States is an entity that can be extracted with a NER

“Barack Obama was the 44th president of the United States”, has 2 entities that can be extracted with NER

Top 8 entity types most commonly extracted by Named Entity Recognition (NER)

Several different types of named entities can be identified through named entity recognition. These include:

1. People

People are named entities that refer to specific people, such as “Barack Obama” or “Neri Van Otten.”

2. Locations

These entities refer to specific locations, such as “The United States” or “Paris, France.”

3. Organisations

Organisational entities refer to specific organisations, such as “Google” or “United Nations.”

4. Events

Event-named entities refer to specific events, such as “World War II” or “Olympic Games.”

5. Products

Products are named entities that refer to specific products, such as “the iPhone” or “Ford Mustang.”

6. Artefacts

These refer to objects such as the “Eiffel Tower” or the “Mona Lisa.”

7. Dates

Dates are entities that can sometimes be easily recognised as “02/04/2022” or “5th January 2022” but could also be textual entities like “new year” or “Easter”.

8. Monetary Values

These entities refer to a monetary value, these could be in pounds, euros or any other currency. An example would be “£2.10” or “5.50“.

Named Entity Recognition NER can extract monetory values in financial reports

Extracting monetary values is useful when automating the parsing of financial reports.

These are the most common entities, and many libraries will have implementations to detect these entities. There may also be other types of named entities that can be identified, depending on the specific application. For example, named entity recognition can be used to identify medical conditions in medical text or financial entities in financial documents. Training your own NER is often the only option if you need a specific entity extraction for a specific use case.

When implementing your own NER, knowing the different approaches you can take is useful. The different approaches are discussed in the next section.

What are the different approaches to Named Entity Recognition (NER)?

There are several approaches to NER, including rule-based systems, dictionary-based systems, and supervised-, unsupervised- or neural network-based approaches. Each of these different systems has its advantages and disadvantages. We discuss the various methods below.

1. Rule-based NER

NER develops rules to identify entities in texts written in natural language. Utilising predefined tags like “organisation,” “product name”, and “date”, these rules can be used to categorise and label content found in documents, articles, and websites. You could, for instance, establish a rule that whenever Apple or Windows appear, the NER labels them as “technology corporations” and “operating systems.” This has the benefit of being easy to implement because all you have to do is create some rules using the entities you’re interested in and then put them into action. However, this method’s obvious drawback is that it ignores context, which means that even the fruit “Apple” would be tagged as a “technology corporation”.

2. Dictionary-based NER

Dictionary-based methods take words from texts that are part of dictionaries, ontologies, and vocabularies. The approach applies to both small datasets and large corpora of texts that fall under a specific entity class (such as persons or locations). Dictionary-based methods can also be used to implement synonym substitution, which involves interpreting the document’s words interchangeably within their respective categories based on a database of accepted terms.

3. A supervised machine learning approach

Supervised learning uses labelled training data, and algorithms such as the maximum entropy algorithm and conditional random fields (CRF) are trained. These techniques support the automated learning of patterns from data that can be applied to new datasets to anticipate associated labels by labelling new samples using previously learned features.

4. An unsupervised machine learning approach

Unsupervised approaches aid in classifying names into distinct classes based on linguistic characteristics such as capitalisation, part-of-speech tags, and domain terminology found in comparable but imperfect classifications in the corpus; as a result, it is a technique that may ultimately increase labelling precision.

5. Neural network-based approaches

Neural network-based approaches combine artificial neural networks, language models, and embedding techniques to produce an entire NER framework. Three options to think about are the

  • BiLSTM (Bidirectional Long Short-Term Memory)
  • BERT (Bidirectional Encoder Representations for Transformers) architectures.

This general strategy can be effective in many sequence labelling tasks, including POS tagging and improving NER accuracy.

These architectures can create deep learning algorithms that improve machine-enabled prediction power while minimising manual interference.

What are the different Python tools that implement Named Entity Recognition (NER)?

1. SpaCy Named Entity Recognition (NER)

To use the named entity recognition (NER) functionality in the spaCy library, you must have spaCy installed on your machine. You can install spaCy using pip, the Python package manager, with the following command:

pip install spacy

Once spaCy is installed, you can load a pre-trained NER model, such as the English language model, and use it to identify named entities in text. Here is an example of how to do this:

import spacy # Load the English language model 

nlp = spacy.load("en_core_web_sm") 

# Define a sample text 
text = "Barack Obama was the 44th president of the United States." 

# Parse the text with the loaded NER model 
doc = nlp(text)

# Iterate over the entities in the document and print their labels 
for ent in doc.ents: 
  print(ent.label_)

In this example, the NER model will identify the named entities “Barack Obama” and “United States” in the sample text and will print their labels (i.e., “PERSON” and “GPE”, respectively). You can then use the identified entities and their labels for further analysis or processing.

2. BERT Named Entity Recognition (NER)

To use the named entity recognition (NER) functionality of BERT, you must have a BERT model and the BERT library installed on your machine. The BERT library implements several popular deep-learning frameworks, including TensorFlow and PyTorch. To install the BERT library for TensorFlow, you can use pip, the Python package manager, with the following command:

pip install bert-tensorflow 

Once the BERT library is installed, you can perform NER on text. Here is an example of how to do this using the TensorFlow implementation of BERT:

import tensorflow as tf 
import tensorflow_hub as hub 

# Load a BERT model from TensorFlow Hub 
model = hub.load("https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1") 

# Define a sample text 
text = "Barack Obama was the 44th president of the United States." 

# Tokenise and input the text to the BERT model 
tokens = model.tokenization.tokenize_string(text) 
inputs = model.tokenization.convert_tokens_to_ids(tokens) 

# Use the BERT model to predict named entities 
ner_predictions = model.predict(inputs) 

# Iterate over the predicted named entities and print their labels 
for ner_prediction in ner_predictions: 
  print(ner_prediction["ner"]) 

In this example, the BERT model will identify the named entities “Barack Obama” and “United States” in the sample text and will print their labels (i.e., “PERSON” and “LOCATION”, respectively). You can then use the identified entities and their labels for further analysis or processing.

Note that this example uses a pre-trained BERT model from TensorFlow Hub, but you can also use a custom BERT model that you have trained yourself.

3. NLTK Named Entity Recognition (NER)

To use the named entity recognition (NER) functionality in the NLTK library, you will need to have NLTK installed on your machine. You can install NLTK using pip, the Python package manager, with the following command:

pip install nltk 

Once NLTK is installed, you can use the nltk.ne_chunk() function to identify named entities in text. This function uses the default NLTK NER classifier based on the Stanford NER model. Here is an example of how to use the nltk.ne_chunk() function to identify named entities in text:

import nltk 

# Define a sample text 
text = "Barack Obama was the 44th president of the United States." 

# Tokenize the text 
tokens = nltk.word_tokenize(text) 

# Part-of-speech tag the tokens 
tagged = nltk.pos_tag(tokens) 

# Use the NLTK NER classifier to identify named entities 
named_entities = nltk.ne_chunk(tagged) 

# Iterate over the named entities and print their labels 
for entity in named_entities: 
  if hasattr(entity, "label"): 
    print(entity.label())

In this example, the NLTK NER classifier will identify the named entities “Barack Obama” and “United States” in the sample text and will print their labels (i.e., “PERSON” and “GPE”, respectively). You can then use the identified entities and their labels for further analysis or processing.

Note that the nltk.ne_chunk() function requires the input text to be tokenized and part-of-speech tagged, which are included in the example above.

4. Flair Named Entity Recognition (NER)

To use the named entity recognition (NER) functionality of Flair, you will need to have the Flair library installed on your machine. You can install Flair using pip, the Python package manager, with the following command:

pip install flair 

Once Flair is installed, you can perform NER on text. Here is an example of how to do this:

import flair 

# Load a pre-trained Flair NER model 
ner = flair.models.SequenceTagger.load("ner-fast") 

# Define a sample text 
text = "Barack Obama was the 44th president of the United States." 

# Use the Flair NER model to predict named entities in the text 
ner_predictions = ner.predict(text) 

# Iterate over the predicted named entities and print their labels 
for ner_prediction in ner_predictions: 
  print(ner_prediction) 

 

In this example, the Flair NER model will identify the named entities “Barack Obama” and “United States” in the sample text and will print their labels (i.e., “PERSON” and “GPE”, respectively). You can then use the identified entities and their labels for further analysis or processing.

Note that this example uses a pre-trained Flair NER model, but you can also use a custom Flair model that you have trained yourself.

Key Takeaways

  • NER can help extract important entities from text.
  • The most common entities extracted are people, organisations, locations, events, products, artefacts, dates and monetary values. If you need other entities extracted, you will probably need to train your own model.
  • There are many different ways of implementing named entity recognition. The simplest is a rule-based system. Slightly more complicated is the dictionary approach, and the more complicated systems use machine learning. Supervised and unsupervised learning can both be used to do entity extraction.
  • The most complicated NER technique is based on neural networks. Common options are BiLSTM, ELMO, and BERT architectures.
  • Python has several really good NER implementations to choose from. SpaCy, NLTK, BERT and Flair all have solid implementations you can use out of the box or train your model with.

At Spot Intelligence, we use the out-of-the-box approach and build and train our own for specific use cases. What NER implementations do you use and why? Let us know in the comments.

About the Author

Neri Van Otten

Neri Van Otten

Neri Van Otten is the founder of Spot Intelligence, a machine learning engineer with over 12 years of experience specialising in Natural Language Processing (NLP) and deep learning innovation. Dedicated to making your projects succeed.

Recent Articles

cloud vs edge computing

NLP And Edge Computing: How It Works & Top 7 Technologies for Offline Computing

In the age of digital transformation, Natural Language Processing (NLP) has emerged as a cornerstone of intelligent applications. From chatbots and voice assistants to...

elastic net vs l1 and l2 regularization

Elastic Net Made Simple & How To Tutorial In Python

What is Elastic Net Regression? Elastic Net regression is a statistical and machine learning technique that combines the strengths of Ridge (L2) and Lasso (L1)...

how recursive feature engineering works

Recursive Feature Elimination (RFE) Made Simple: How To Tutorial

What is Recursive Feature Elimination? In machine learning, data often holds the key to unlocking powerful insights. However, not all data is created equal. Some...

high dimensional dat challenges

How To Handle High-Dimensional Data In Machine Learning [Complete Guide]

What is High-Dimensional Data? High-dimensional data refers to datasets that contain a large number of features or variables relative to the number of observations or...

in-distribution vs out-of-distribution example

Out-of-Distribution In Machine Learning Made Simple & How To Detect It

What is Out-of-Distribution Detection? Out-of-Distribution (OOD) detection refers to identifying data that differs significantly from the distribution on which a...

types of anomalies in LLMs

Anomaly Detection In LLM Responses [How To Monitor & Mitigate]

What is Anomaly Detection in LLMs? Anomaly detection in the context of Large Language Models (LLMs) involves identifying outputs, patterns, or behaviours that deviate...

types of text annotation

Text Annotation Made Simple And 7 Popular Tools

What is Text Annotation? Text annotation is the process of labelling or tagging text data with specific information, making it more understandable and usable for...

average rating by sentiment

How To Process Text In Python With Pandas Made Simple

Introduction Text data is everywhere—from social media posts and customer reviews to emails and product descriptions. For data scientists and analysts, working with...

causes of missing data

Handling Missing Data In Machine Learning: Top 8 Techniques & How To Tutorial In Python

What is Missing Data in Machine Learning? In machine learning, the quality and completeness of data are often just as important as the algorithms and models we choose....

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

nlp trends

2024 NLP Expert Trend Predictions

Get a FREE PDF with expert predictions for 2024. How will natural language processing (NLP) impact businesses? What can we expect from the state-of-the-art models?

Find out this and more by subscribing* to our NLP newsletter.

You have Successfully Subscribed!