Quick-start with Name Entity Extraction in NLP using Python 3

Name Entity Extraction in NLP using python | Innovate Yourself
29
0

Introduction

Welcome, Python enthusiasts! Today, we embark on an exciting journey into the realm of Natural Language Processing (NLP) to unravel the mysteries of Name Entity Extraction using Python 3. Whether you’re a budding coder or an experienced developer, this blog post is your gateway to mastering NLP and becoming a pro in the world of Python.

Understanding the Basics of Name Entity Extraction

Before we dive into the code, let’s grasp the fundamental concept of Name Entity Extraction. In NLP, Name Entity Extraction involves identifying and classifying entities (such as names of people, organizations, locations, dates, and more) within a given text. It’s like teaching your computer to recognize the ‘who,’ ‘what,’ and ‘where’ in a sea of words.

Setting the Stage: Python

Python 3 is our weapon of choice for this journey. Its simplicity and powerful libraries make it the go-to language for NLP tasks. If you haven’t already installed Python 3, hop onto the Python wagon by visiting python.org and follow the installation instructions.

Essential Libraries

We’ll be leveraging the prowess of two essential libraries for our NER adventure: spaCy and Matplotlib.

  1. spaCy: A robust library for NLP that simplifies complex tasks like tokenization, part-of-speech tagging, and of course, NER.
  2. Matplotlib: The go-to library for data visualization in Python. We’ll use it to create insightful plots that make understanding our results a breeze.

Installing Dependencies

Let’s kick things off by installing spaCy and Matplotlib. Open your command prompt or terminal and type the following commands:

pip install spacy
pip install matplotlib

Loading the SpaCy Model

Once our tools are in place, let’s load the spaCy model for Name Entity Extraction. SpaCy provides pre-trained models that save us from the hassle of training our own from scratch.

import spacy

# Load spaCy NER model
nlp = spacy.load("en_core_web_sm")

The Name Entity Extraction Magic: Code Breakdown

Now comes the fun part – the code! We’ll demonstrate Name Entity Extraction using a sample text. Suppose we have the following sentence:

text = "Innovate Yourself was founded by Ashish Saini in India in 2014."

Let’s break down the code step by step:

# Process the text with spaCy
doc = nlp(text)

# Extract entities
entities = [(ent.text, ent.label_) for ent in doc.ents]

# Print the results
print("Entities:", entities)

In this example, the entities list will contain tuples with the identified entities and their corresponding labels. For our sentence, the output would look like this:

Entities: [('Innovate Yourself', 'ORG'), ('Ashish Saini', 'PERSON'), ('India', 'GPE'), ('2014', 'DATE')]

Visualizing the Results

Now that we have our entities, let’s visualize them using Matplotlib. We’ll create a simple bar chart to show the frequency of each entity type.

import matplotlib.pyplot as plt

# Count the occurrences of each entity type
entity_types = [ent[1] for ent in entities]
entity_counts = {entity: entity_types.count(entity) for entity in set(entity_types)}

# Plotting
plt.bar(entity_counts.keys(), entity_counts.values())
plt.title("Entity Type Distribution")
plt.xlabel("Entity Type")
plt.ylabel("Count")
plt.show()
Name Entity Extraction in NLP using python | Innovate Yourself

This code will generate a bar chart that vividly displays the distribution of entity types in our text.

Putting It All Together: Complete Code

For your convenience, here’s the full code snippet that combines everything we’ve covered:

import spacy
import matplotlib.pyplot as plt

# Load spaCy NER model
nlp = spacy.load("en_core_web_sm")

# Sample text
text = "Innovate Yourself was founded by Ashish Saini in India in 2014."

# Process the text with spaCy
doc = nlp(text)

# Extract entities
entities = [(ent.text, ent.label_) for ent in doc.ents]

# Print the results
print("Entities:", entities)

# Count the occurrences of each entity type
entity_types = [ent[1] for ent in entities]
entity_counts = {entity: entity_types.count(entity) for entity in set(entity_types)}

# Plotting
plt.bar(entity_counts.keys(), entity_counts.values())
plt.title("Entity Type Distribution")
plt.xlabel("Entity Type")
plt.ylabel("Count")
plt.show()

Conclusion

Congratulations! You’ve just scratched the surface of Name Entity Extraction in NLP using Python 3. This journey is just the beginning – there’s a vast world of possibilities waiting for you to explore.

Remember, practice makes perfect. Experiment with different texts, tweak the code, and see how Name Entity Extraction performs in various scenarios. The more you play with it, the more confident you’ll become in wielding the power of Python for NLP.

Also, check out our other playlist Rasa ChatbotInternet of thingsDockerPython ProgrammingMachine LearningMQTTTech NewsESP-IDF etc.
Become a member of our social family on youtube here.
Stay tuned and Happy Learning. ✌🏻😃
Happy coding, and may your NLP endeavors be both enlightening and rewarding! ❤️🔥

Leave a Reply