spaCy is an open-source software library for advanced Natural Language Processing (NLP) in Python. It's designed to be fast, efficient, and easy to use, making it a popular choice for industrial-strength NLP tasks. Whether you're working with text classification, named entity recognition (NER), part-of-speech tagging, or text parsing, spaCy provides all the essential tools you need to get started.
In this article, we'll briefly explore what spaCy is and show a small Python example to help you get started.
Installing spaCy
To use spaCy, you first need to install it. If you don't have it installed, you can do so using pip:
pip install spacy
Next, you'll need to download a pre-trained model. For English, you can use the en_core_web_sm model:
python -m spacy download en_core_web_sm
Python Example: Tokenization and Named Entity Recognition (NER)
In this small example, we’ll load a spaCy model, perform tokenization, and extract named entities from a text. Here's how you can do it:
**# Importing spaCy**
import spacy
# Load the pre-trained spaCy model
nlp = spacy.load("en_core_web_sm")
# Sample text
text = "Apple is looking to buy a startup in the UK for $1 billion."
# Process the text using spaCy
doc = nlp(text)
# Tokenization: Print each token in the text
print("Tokens:")
for token in doc:
print(token.text)
# Named Entity Recognition (NER): Print named entities in the text
print("\nNamed Entities:")
for ent in doc.ents:
print(f"{ent.text} ({ent.label_})")
Output
Tokens:
Apple
is
looking
to
buy
a
startup
in
the
UK
for
$1
billion
.
Named Entities:
Apple (ORG)
UK (GPE)
$1 billion (MONEY)
What We Learned
Tokenization: We split the text into individual tokens, such as words ("Apple", "is", "looking") and punctuation (".").
Named Entity Recognition (NER): We identified "Apple" as an organization (ORG), "UK" as a geopolitical entity (GPE), and "$1 billion" as a monetary value (MONEY).
Top comments (0)