Natural Language Processing deals with text data. The amount of text data generated these days is enormous. And, this data if utilized properly can bring many fruitful results. Some of the most important Natural Language Processing applications are Text Analytics, Parts of Speech Tagging, Sentiment Analysis, and Named Entity Recognition.
The vast amount of text data contains a huge amount of information. An important aspect of analyzing these text data is the identification of Named Entities.
What is a Named Entity?
A named entity is basically a real-life object which has proper identification and can be denoted with a proper name. Named Entities can be a place, person, organization, time, object, or geographic entity.
For example, named entities would be Roger Federer, Honda city, Samsung Galaxy S10. Named entities are usually instances of entity instances. For example, Roger Federer is an instance of a Tennis Player/person, Honda City is an instance of a car and Samsung Galaxy S10 is an instance of a Mobile Phone.
Named Entity Recognition:
Named Entity Recognition is the process of NLP which deals with identifying and classifying named entities. The raw and structured text is taken and named entities are classified into persons, organizations, places, money, time, etc. Basically, named entities are identified and segmented into various predefined classes.
NER systems are developed with various linguistic approaches, as well as statistical and machine learning methods. NER has many applications for project or business purposes.
NER model first identifies an entity and then categorizes the entity into the most suitable class. Some of the common types of Named Entities will be:
1. Organisations :
NASA, CERN, ISRO, etc
Mumbai, New York, Kolkata.
1 Billion Dollars, 50 Great Britain Pounds.
15th August 2020
Elon Musk, Richard Feynman, Subhas Chandra Bose.
An important thing about NER models is that their ability to understand Named Entities depends on the data they have been trained on. There are many applications of NER.
NER can be used for content classification, the various Named Entities of a text can be collected, and based on that data, the content themes can be understood. In academics and research, NER can be used to retrieve data and information faster from a wide variety of textual information. NER helps a lot in the case of information extraction from huge text datasets.
NER using Spacy:
Spacy is an open-source Natural Language Processing library that can be used for various tasks. It has built-in methods for Named Entity Recognition. Spacy has a fast statistical entity recognition system.
We can use spacy very easily for NER tasks. Though often we need to train our own data for business-specific needs, the spacy model general performs well for all types of text data.
Let us get started with the code, first we import spacy and proceed.
import spacy from spacy import displacy NER = spacy.load("en_core_web_sm")
Now, we enter our sample text which we shall be testing. The text has been taken from the Wikipedia page of ISRO.
raw_text="The Indian Space Research Organisation or is the national space agency of India, headquartered in Bengaluru. It operates under Department of Space which is directly overseen by the Prime Minister of India while Chairman of ISRO acts as executive of DOS as well."
Now, we print the data on the NEs found in this text sample.
for word in text1.ents: print(word.text,word.label_)
The Indian Space Research Organisation ORG the national space agency ORG India GPE Bengaluru GPE Department of Space ORG India GPE ISRO ORG DOS ORG
So, now we can see that all the Named Entities in this particular text are extracted. If, we are facing any problem regarding what type a particular NE is, we can use the following method.
Output: ‘Companies, agencies, institutions, etc.’
Output: ‘Countries, cities, states’
Now, we try an interesting visual, which shows the NEs directly in the text.
I will leave the Kaggle Link in the end, so that the readers can try out the code themselves. Coming to the visual, the Named Entities are properly mentioned in the text, with contrasting colors, which make data visualization quite easy and simple. There is another type of visual, which explores the full dataset as a whole. Please refer to the Kaggle link in the end.
Let us try the same tasks with some tests containing more Named Entities.
raw_text2=”The Mars Orbiter Mission (MOM), informally known as Mangalyaan, was launched into Earth orbit on 5 November 2013 by the Indian Space Research Organisation (ISRO) and has entered Mars orbit on 24 September 2014. India thus became the first country to enter Mars orbit on its first attempt. It was completed at a record low cost of $74 million.”
for word in text2.ents: print(word.text,word.label_)
The Mars Orbiter Mission PRODUCT MOM ORG Mangalyaan GPE Earth LOC 5 November 2013 DATE the Indian Space Research Organisation ORG ISRO ORG Mars LOC 24 September 2014 DATE India GPE first ORDINAL Mars LOC first ORDINAL $74 million MONEY
Here, we get more types of named entities. Let us identify what type they are.
Output: ‘Objects, vehicles, foods, etc. (not services)’
Output: ‘Non-GPE locations, mountain ranges, bodies of water’
Output: ‘Absolute or relative dates or periods’
Output: ‘ “first”, “second”, etc.’
Output: ‘Monetary values, including unit’
Now, we analyze the text as a whole in the form of a visual.
Here, we the various Named Entities in contrasting colors, so we understand the overall nature of the text.
NER of a News Article
We shall web scrape data from a news article and do NER on the text data gathered from there.
We shall use Beautiful Soup for web scraping purposes.
from bs4 import BeautifulSoup import requests import re
Now, we will use the URL of the news article.
html_content = requests.get(URL).text
soup = BeautifulSoup(html_content, "lxml")
Now, we get the body content.
Now, we use regex to clean the text.
body= body.replace('n', ' ') body= body.replace('t', ' ') body= body.replace('r', ' ') body= body.replace('xa0', ' ') body=re.sub(r'[^ws]', '', body)
Let us now have a look at the text.
' View in App Bitcoin was down by 6 and was trading at Rs 2728815 after hitting days high of Rs 2900208 Source Reuters Reported By ZeeBiz WebTeam Written By Ravi Kant Kumar Updated Sat Jun 12 20210646 pm Patna ZeeBiz WebDesk RELATED NEWS Cryptocurrency Latest News Today June 14 Bitcoin leads crypto rally up over 12 after ELON MUSK TWEET Check Ethereum Polka Dot Dogecoin Shiba Inu and other top coins INR price World India updates Bitcoin law is only'
Now, let us proceed with Named Entity Recognition.
Well, the visual formed is very large, but there are some interesting parts which I want to cover.
Now, coming to a few observations.
Bitcoin is supposedly a geographic location. Patna is an Organisation. Leaving aside a few cases, most of the text has been classified properly into their respective named entities. Thus we can understand that the entity recognition has been done properly.
To view the entire code, check this link on Kaggle.
So, we can conclude that NER is an important application of NLP and has widespread uses.
Data Science and Analytics | Digital Marketing Specialist | SEO | Content Creation
Connect with me on Linkedin.
The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.You can also read this article on our Mobile APP