NER — How To Implement Different Types With Code Examples

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

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 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“.

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 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.

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”.

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.

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.

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.

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 NER?

SpaCy 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.

BERT 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.

NLTK 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.

Flair 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.

Related Articles

Understanding Elman RNN — Uniqueness & How To Implement

by | Feb 1, 2023 | artificial intelligence,Machine Learning,Natural Language Processing | 0 Comments

What is the Elman neural network? Elman Neural Network is a recurrent neural network (RNN) designed to capture and store contextual information in a hidden layer. Jeff...

Self-attention Made Easy And How To Implement It

by | Jan 31, 2023 | Machine Learning,Natural Language Processing | 0 Comments

What is self-attention in deep learning? Self-attention is a type of attention mechanism used in deep learning models, also known as the self-attention mechanism. It...

Gated Recurrent Unit Explained & How They Compare [LSTM, RNN, CNN]

by | Jan 30, 2023 | artificial intelligence,Machine Learning,Natural Language Processing | 0 Comments

What is a Gated Recurrent Unit? A Gated Recurrent Unit (GRU) is a Recurrent Neural Network (RNN) architecture type. It is similar to a Long Short-Term Memory (LSTM)...

How To Use The Top 9 Most Useful Text Normalization Techniques (NLP)

by | Jan 25, 2023 | Data Science,Natural Language Processing | 0 Comments

Text normalization is a key step in natural language processing (NLP). It involves cleaning and preprocessing text data to make it consistent and usable for different...

How To Implement POS Tagging In NLP Using Python

by | Jan 24, 2023 | Data Science,Natural Language Processing | 0 Comments

Part-of-speech (POS) tagging is fundamental in natural language processing (NLP) and can be carried out in Python. It involves labelling words in a sentence with their...

How To Start Using Transformers In Natural Language Processing

by | Jan 23, 2023 | Machine Learning,Natural Language Processing | 0 Comments

Transformers Implementations in TensorFlow, PyTorch, Hugging Face and OpenAI's GPT-3 What are transformers in natural language processing? Natural language processing...

How To Implement Different Question-Answering Systems In NLP

by | Jan 20, 2023 | artificial intelligence,Data Science,Natural Language Processing | 0 Comments

Question answering (QA) is a field of natural language processing (NLP) and artificial intelligence (AI) that aims to develop systems that can understand and answer...

The Curse Of Variability And How To Overcome It

by | Jan 20, 2023 | Data Science,Machine Learning,Natural Language Processing | 0 Comments

What is the curse of variability? The curse of variability refers to the idea that as the variability of a dataset increases, the difficulty of finding a good model...

How To Implement A Siamese Network In NLP — Made Easy

by | Jan 19, 2023 | Machine Learning,Natural Language Processing | 0 Comments

What is a Siamese network? It is also commonly known as one or a few-shot learning. They are popular because less labelled data is required to train them. Siamese...

Top 6 Most Popular Text Clustering Algorithms And How They Work

by | Jan 17, 2023 | Data Science,Machine Learning,Natural Language Processing | 0 Comments

What exactly is text clustering? The process of grouping a collection of texts into clusters based on how similar their content is is known as text clustering. Text...

Opinion Mining — More Powerful Than Just Sentiment Analysis

by | Jan 17, 2023 | Data Science,Natural Language Processing | 0 Comments

Opinion mining is a field that is growing quickly. It uses natural language processing and text analysis to gather subjective information from sources. The main goal of...

How To Implement Document Clustering In Python

by | Jan 16, 2023 | Data Science,Machine Learning,Natural Language Processing | 0 Comments

Introduction to document clustering and its importance Grouping similar documents together in Python based on their content is called document clustering, also known as...

Local Sensitive Hashing — When And How To Get Started

by | Jan 16, 2023 | Machine Learning,Natural Language Processing | 0 Comments

What is local sensitive hashing? A technique for performing a rough nearest neighbour search in high-dimensional spaces is called local sensitive hashing (LSH). It...

How To Get Started With One Hot Encoding

by | Jan 12, 2023 | Data Science,Machine Learning,Natural Language Processing | 0 Comments

Categorical variables are variables that can take on one of a limited number of values. These variables are commonly found in datasets and can't be used directly in...

Different Attention Mechanism In NLP Made Easy

by | Jan 12, 2023 | artificial intelligence,Machine Learning,Natural Language Processing | 0 Comments

Numerous tasks in natural language processing (NLP) depend heavily on an attention mechanism. When the data is being processed, they allow the model to focus on only...

0 Comments

Submit a Comment

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