Enable Dark Mode!
what-is-spacy-its-key-features.jpg
By: Anagha S

What is spaCy & Its Key Features

Functional Odoo 18 Odoo Enterprises

Nowadays, a lot of applications, including chatbots and sentiment analysis, depend on natural language processing, or NLP. Out of the many tools available for NLP, SpaCy shines as a powerful and efficient Python library. When dealing with large volumes of text, you'll likely want deeper insights into its content. In this blog post, we'll introduce Spacy and explore its key features and capabilities.

What is Spacy?

Spacy is one of the most widely used Python libraries for Natural Language Processing (NLP). 

Because of its quick and user-friendly design, developers and data scientists working on natural language processing projects highly prefer it. It provides tools for text processing, tokenization, entity recognition, and more, making it popular in NLP applications.

The key features of Spacy:

1. Tokenization: SpaCy's tokenizer identifies words, punctuation marks, and other elements in the text. It entails segmenting a text into smaller sections.

For example, in the sentence "Hello, world!", the tokens would be "Hello", ",", "world", and "!".

2. Part-of-speech (POS) Tagging: It labels each word's grammatical role in a sentence, like nouns, verbs, conjunctions, adjectives, etc.

For example, in the sentence “The brown fox jumps over the lazy dog”

"The" (Determiner), "brown" (Adjective), "fox" (Noun), "jumps" (Verb), "over" (Preposition),  "the" (Determiner), "lazy" (Adjective), "dog" (Noun).

3. Named Entity Recognition (NER): SpaCy recognizes and categorizes named entities in text into predefined groups, including people, organizations, places, dates, and more.

4. Sentence Segmentation: It divides text into sentences with precision.

5. Text Classification: SpaCy can help build models for tasks such as sentiment analysis.

6. Rule-based Matching: It offers a flexible system for finding specific phrases or patterns in text.

7. Dependency Parsing: It can identify the connections between words and examine a sentence's grammatical structure.

8. Word Vectors:  Word Vectors are numerical representations of words in a continuous vector space. They capture semantic meanings and relationships between words based on their usage in context. Word vectors are widely used in various NLP applications, including text classification, clustering, and recommendation systems, as they provide a way to quantify the meaning of words and their relationships.

Let's Get Started with Spacy

To start using Spacy, you first need to install it. You can do this using pip:

pip install spacy

After installation, you'll need to download a language model. For English, you can use:

python -m spacy download en_core_web_sm

Now, let’s explore spaCy with code examples.

Example 1: Basic Tokenization

Tokenization is the process of splitting text into words, punctuation, or other meaningful units (tokens). spaCy makes this effortless.

import spacy
# Load the English model
nlp = spacy.load("en_core_web_sm")
# Process a sentence
text = "spaCy is an amazing tool for NLP!"
doc = nlp(text)
# Print tokens
for token in doc:
    print(token.text)
#output
spaCy 
Is
an 
Amazing
Tool
For
NLP 
!

Each word and punctuation mark is separated into a token. 

Example 2: Part-of-Speech Tagging

For every word—that is, noun, verb, adjective—spaCy can determine its grammatical function. Let's try it:

# Same setup as above
text = "The quick brown fox jumps over the lazy dog."
doc = nlp(text)
# Print tokens with POS tags
for token in doc:
    print(f"{token.text}: {token.pos_}")
#Output
The: DET
quick: ADJ
brown: ADJ
fox: NOUN
jumps: VERB
over: ADP
the: DET
lazy: ADJ
dog: NOUN
.: PUNCT

The part-of-speech tag is provided by the 'pos_' attribute. This is helpful for filtering particular word types or comprehending sentence structure.

Example 3: Named Entity Recognition (NER)

In text, NER recognises real-world entities such as individuals, groups, or places. Here's an example:

text = "Apple is planning to open a new store in New York next year."
doc = nlp(text)
# Print entities
for ent in doc.ents:
    print(f"{ent.text}: {ent.label_}")
#Output
Apple: ORG 
New York: 
GPE next 
year: DATE

SpaCy accurately recognizes "next year" as a date, "Apple" as an organization, and "New York" as a geopolitical entity (GPE). When it comes to extracting structured data from unstructured text, this is extremely helpful.

Example 4: Dependency Parsing

Dependency parsing shows how words relate to one another grammatically.

from spacy import displacy
text = "She loves to code in Python."
doc = nlp(text)
# Print dependencies
for token in doc:
    print(f"{token.text} --> {token.dep_} --> {token.head.text}")
# Visualize (run this in a Jupyter notebook or save to HTML)
displacy.render(doc, style="dep", jupyter=True)
# Output
She --> nsubj --> loves
loves --> ROOT --> loves
to --> aux --> code
code --> xcomp --> loves
in --> prep --> code
Python --> pobj --> in
. --> punct --> loves

A tree-like structure linking words according to their syntactic roles is depicted in the notebook visualization. This is excellent for learning how sentences are put together.

Example 5: Custom Text Processing

Let’s combine multiple features to analyze a longer text:

text = """ Elon Musk founded SpaceX in 2002. The company is based in California and aims to colonize Mars. """
doc = nlp(text)
# Extract entities and their types
print("Named Entities:") 
for ent in doc.ents:
   print(f"{ent.text}: {ent.label_}")
 # Count nouns 
nouns = [token.text for token in doc if token.pos_ == "NOUN"] print("\nNouns found:", len(nouns), nouns)
#Output
Named Entities:
Elon Musk: PERSON
SpaceX: ORG
2002: DATE
California: GPE
Mars: LOC
Nouns found: 4 ['company', 'California', 'aims', 'Mars']

This sample demonstrates how spaCy can handle multi-sentence text by extracting entities and counting nouns.

Tips for Efficient Use of spaCy

* Choose the Right Model: Use en_core_web_sm for speed, or en_core_web_lg for better accuracy.

* Batch Processing: For large datasets, process texts in batches with nlp.pipe() to optimize performance.

* Customization: Add your own rules or train custom models if the defaults don’t meet your needs.

* Memory Management: Disable unused components (e.g., nlp = spacy.load("en_core_web_sm", disable=["ner"])).

ascore: 0.5em;">) to save memory.

spaCy is a powerhouse for NLP, offering a blend of speed, accuracy, and usability. Whether you’re tokenizing text, tagging parts of speech, extracting entities, or parsing dependencies, spaCy has you covered. The examples above are just the beginning—experiment with your own text and explore advanced features like word vectors or custom pipelines.

To read more about An Overview of Pandas AI in 2024, refer to our blog An Overview of Pandas AI in 2024.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message