ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Mastering Part-of-Speech Tagging with spaCy in Python

author
Generated by
ProCodebase AI

22/11/2024

python

Sign in to read full article

Introduction to Part-of-Speech Tagging

Part-of-speech (POS) tagging is a fundamental task in natural language processing that involves labeling each word in a text with its appropriate grammatical category. These categories, such as nouns, verbs, adjectives, and adverbs, provide crucial information about the role and meaning of words within a sentence.

SpaCy, a popular Python library for NLP, offers robust and efficient POS tagging capabilities. Let's dive into how we can leverage spaCy to perform POS tagging in our Python projects.

Setting Up spaCy

Before we begin, make sure you have spaCy installed and a language model downloaded. You can install spaCy and download the English model using pip:

pip install spacy python -m spacy download en_core_web_sm

Now, let's import spaCy and load the English language model:

import spacy nlp = spacy.load("en_core_web_sm")

Basic POS Tagging with spaCy

To perform POS tagging on a piece of text, we simply need to process it with our spaCy model:

text = "The quick brown fox jumps over the lazy dog." doc = nlp(text) for token in doc: print(f"{token.text}: {token.pos_}")

This will output:

The: DET
quick: ADJ
brown: ADJ
fox: NOUN
jumps: VERB
over: ADP
the: DET
lazy: ADJ
dog: NOUN
.: PUNCT

SpaCy uses the Universal Dependencies tagset, which provides a consistent set of tags across different languages. Some common tags include:

  • NOUN: Nouns
  • VERB: Verbs
  • ADJ: Adjectives
  • ADV: Adverbs
  • DET: Determiners
  • ADP: Adpositions (prepositions and postpositions)
  • PUNCT: Punctuation

Fine-grained POS Tags

In addition to the coarse-grained POS tags, spaCy also provides fine-grained tags that offer more detailed information:

for token in doc: print(f"{token.text}: {token.pos_} ({token.tag_})")

Output:

The: DET (DT)
quick: ADJ (JJ)
brown: ADJ (JJ)
fox: NOUN (NN)
jumps: VERB (VBZ)
over: ADP (IN)
the: DET (DT)
lazy: ADJ (JJ)
dog: NOUN (NN)
.: PUNCT (.)

These fine-grained tags (e.g., NN for singular noun, VBZ for 3rd person singular present verb) provide more specific grammatical information.

Practical Applications of POS Tagging

Let's explore some practical applications of POS tagging:

1. Extracting all nouns from a text:

text = "The majestic mountains rise above the serene lake, creating a breathtaking landscape." doc = nlp(text) nouns = [token.text for token in doc if token.pos_ == "NOUN"] print("Nouns:", nouns)

Output:

Nouns: ['mountains', 'lake', 'landscape']

2. Finding adjective-noun pairs:

adj_noun_pairs = [(token.text, token.head.text) for token in doc if token.pos_ == "ADJ" and token.head.pos_ == "NOUN"] print("Adjective-Noun Pairs:", adj_noun_pairs)

Output:

Adjective-Noun Pairs: [('majestic', 'mountains'), ('serene', 'lake'), ('breathtaking', 'landscape')]

3. Analyzing verb usage:

verbs = [token.lemma_ for token in doc if token.pos_ == "VERB"] print("Verbs (lemmatized):", verbs)

Output:

Verbs (lemmatized): ['rise', 'create']

Customizing POS Tagging

SpaCy allows you to customize the POS tagging process. For instance, you can add custom rules to handle domain-specific terminology:

from spacy.pipeline import EntityRuler ruler = nlp.add_pipe("entity_ruler", before="ner") patterns = [{"label": "ORG", "pattern": "spaCy"}] ruler.add_patterns(patterns) text = "I love using spaCy for NLP tasks." doc = nlp(text) for token in doc: print(f"{token.text}: {token.pos_} ({token.ent_type_ if token.ent_type_ else 'N/A'})")

Output:

I: PRON (N/A)
love: VERB (N/A)
using: VERB (N/A)
spaCy: PROPN (ORG)
for: ADP (N/A)
NLP: PROPN (N/A)
tasks: NOUN (N/A)
.: PUNCT (N/A)

In this example, we've added a custom rule to recognize "spaCy" as an organization (ORG), which affects its POS tag.

Conclusion

POS tagging with spaCy is a powerful tool in your NLP toolkit. It provides valuable grammatical information that can be used in various applications, from text analysis to machine learning feature engineering. By understanding and utilizing spaCy's POS tagging capabilities, you'll be well-equipped to tackle complex NLP tasks in Python.

Popular tags

pythonspacynlp

Share now!

Like & bookmark

Related collections

  • Python with Redis Cache

    08/11/2024 · Python

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 · Python

  • FastAPI Mastery: From Zero to Hero

    15/10/2024 · Python

  • Mastering Pandas: From Foundations to Advanced Data Engineering

    25/09/2024 · Python

  • Mastering Computer Vision with OpenCV

    06/12/2024 · Python

Related articles

  • Elevating Data Visualization

    05/10/2024 · Python

  • Mastering Prompt Engineering with LlamaIndex for Python Developers

    05/11/2024 · Python

  • Optimizing LangGraph Code for Python

    17/11/2024 · Python

  • Unlocking the Power of Dependency Parsing with spaCy in Python

    22/11/2024 · Python

  • Mastering Pie Charts and Donut Plots with Matplotlib

    05/10/2024 · Python

  • Streamlining Data Ingestion

    05/11/2024 · Python

  • Mastering the Art of Debugging LangGraph Applications in Python

    17/11/2024 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design