Day 20: Named Entity Recognition (NER) - Notebook Implementation
Welcome back to our NLP journey! ??
Today is a Coding Day where we will dive into practical implementations of Natural Language Processing (NLP) using spaCy. We’ll cover how to implement code for Named Entity Recognition (NER). Let’s get coding!
Problem Statement:
Named Entity Recognition (NER) is the task of identifying and classifying key entities in text, such as people, organizations, locations, dates, and more. The goal of this notebook is to implement a NER model using the spaCy library to accurately extract and categorize named entities from given text.
Objectives:
Step 1: Install Required Libraries
First, ensure that you have the required libraries installed. You will need the spaCy library and its English language model. If you haven't installed them yet, you can do so using the following commands:
# Uncomment the following lines to install spaCy and download the English model
# !pip install spacy
# !python -m spacy download en_core_web_sm
Step 2: Import Required Libraries
Now, import the necessary libraries for NER. We will be using the spaCy library for this implementation.
import spacy
Step 3: Load the English Language Model
Load the pre-trained English language model from spaCy. This model will be used for named entity recognition.
# Load the English language model
nlp = spacy.load("en_core_web_sm")
Step 4: Define a Sample Text
Let's define a sample text that contains various named entities.
# Sample text containing various named entities
text = ("Apple Inc. reported strong earnings this quarter. "
"The company's CEO, Tim Cook, announced that iPhone sales "
"were up 20% year-over-year. The tech giant is headquartered "
"in Cupertino, California.")
Step 5: Process the Text with spaCy
Use the nlp object to process the sample text and extract named entities.
# Process the text to create a Doc object
doc = nlp(text)
# Extract and display named entities
print("Named Entities:")
for ent in doc.ents: # Iterate over the named entities
print(f"{ent.text} ({ent.label_})") # Print the entity text and its label
Output:
领英推荐
Named Entities:
Apple Inc. (ORG)
this quarter (DATE)
Tim Cook (PERSON)
iPhone (ORG)
20% (PERCENT)
Cupertino (GPE)
California (GPE)
1. Apple Inc. (ORG)
2. this quarter (DATE)
3. Tim Cook (PERSON)
4. iPhone (ORG)
5. 20% (PERCENT)
6. Cupertino (GPE)
7. California (GPE)
Step 6: Analyze User Input for NER
Allow users to input their own text for named entity recognition. This enables real-time analysis of any given text.
# Enable user input for NER analysis
print("\nEnter your own text to analyze named entities (type 'exit' to quit):")
while True:
user_input = input("Input: ")
if user_input.lower() == 'exit':
break # Exit if the user types 'exit'
# Process user input with spaCy NER model
doc = nlp(user_input)
# Extract and display named entities from user input
print("Named Entities:")
for ent in doc.ents:
print(f"{ent.text} ({ent.label_})") # Print each entity and its label
print() # Add an empty line for readability
This notebook implementation provides a straightforward way to perform Named Entity Recognition using the spaCy library. By following the steps outlined above, you can accurately extract and classify named entities from both predefined sample texts and user-provided inputs.
Key Points:
As we wrap up this series on Natural Language Processing (NLP), we hope you have gained valuable insights into various NLP techniques, including sentiment analysis and named entity recognition,
Thank you for joining us on this journey through NLP! We encourage you to continue exploring these concepts and applying them in your projects. This marks the end of our NLP series, but the learning doesn’t stop here. Keep experimenting and expanding your knowledge in the exciting field of artificial intelligence! ??
Ai Engineer | Data Scientist@PW Skills | LLM Engineer | Youtuber | Ex-Ineuron.ai | [email protected]
6 个月Very informative