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 spaCy Matcher Patterns

author
Generated by
ProCodebase AI

22/11/2024

spaCy

Sign in to read full article

Introduction to spaCy Matcher

If you're working with natural language processing in Python, you've probably heard of spaCy. It's a powerful library that makes text processing a breeze. One of its most useful features is the Matcher, which allows you to search for specific patterns in text. Let's dive into how you can use spaCy Matcher patterns to supercharge your NLP projects!

Setting Up

First things first, make sure you have spaCy installed:

pip install spacy python -m spacy download en_core_web_sm

Now, let's import the necessary modules and load a language model:

import spacy from spacy.matcher import Matcher nlp = spacy.load("en_core_web_sm") matcher = Matcher(nlp.vocab)

Creating Simple Patterns

The basic structure of a spaCy Matcher pattern is a list of dictionaries, where each dictionary represents a token. Let's start with a simple example:

pattern = [{"LOWER": "hello"}, {"LOWER": "world"}] matcher.add("GREETING", [pattern]) doc = nlp("Hello World! Welcome to spaCy matching.") matches = matcher(doc) for match_id, start, end in matches: print(doc[start:end])

This will match "Hello World" in the text, ignoring case. The output will be:

Hello World

Using Token Attributes

spaCy offers a wide range of token attributes for pattern matching. Here are some common ones:

  • LOWER: Lowercase form of the token
  • TEXT: Exact text of the token
  • LEMMA: Base form of the token
  • POS: Part-of-speech tag
  • TAG: Fine-grained POS tag
  • DEP: Syntactic dependency relation
  • SHAPE: Word shape (capitalization, punctuation, digits)

Let's create a pattern to match adjectives followed by nouns:

pattern = [{"POS": "ADJ"}, {"POS": "NOUN"}] matcher.add("ADJ_NOUN", [pattern]) doc = nlp("The big dog chased the small cat.") matches = matcher(doc) for match_id, start, end in matches: print(doc[start:end])

Output:

big dog
small cat

Combining Multiple Attributes

You can use multiple attributes in a single token pattern for more precise matching:

pattern = [ {"LOWER": "python", "POS": "PROPN"}, {"LOWER": "developer", "POS": "NOUN"} ] matcher.add("PYTHON_DEV", [pattern]) doc = nlp("We're looking for a Python developer with 5 years of experience.") matches = matcher(doc) for match_id, start, end in matches: print(doc[start:end])

This will match "Python developer" only when "Python" is recognized as a proper noun.

Using Operators

spaCy Matcher supports several operators to make your patterns more flexible:

  • "OP": "?" (optional, 0 or 1)
  • "OP": "+" (1 or more)
  • "OP": "*" (0 or more)
  • "OP": "!" (negation)

Here's an example using the "+" operator to match one or more adjectives followed by a noun:

pattern = [{"POS": "ADJ", "OP": "+"}, {"POS": "NOUN"}] matcher.add("ADJ_NOUN_PHRASE", [pattern]) doc = nlp("The big red shiny apple fell from the old tree.") matches = matcher(doc) for match_id, start, end in matches: print(doc[start:end])

Output:

big red shiny apple
old tree

Advanced Pattern Matching

For more complex scenarios, you can use custom token attributes or even functions to define matching criteria:

def is_fruit(token): fruits = ["apple", "banana", "orange", "pear"] return token.text.lower() in fruits pattern = [ {"POS": "ADJ", "OP": "*"}, {"POS": "NOUN", "TEXT": {"IN": ["apple", "banana", "orange", "pear"]}} ] matcher.add("FRUIT_PHRASE", [pattern]) doc = nlp("I love eating juicy red apples and ripe yellow bananas.") matches = matcher(doc) for match_id, start, end in matches: print(doc[start:end])

Output:

juicy red apples
ripe yellow bananas

Conclusion

spaCy's Matcher patterns are a powerful tool for extracting information from text. By combining token attributes, operators, and custom functions, you can create sophisticated patterns to match almost any textual structure. As you continue to work with spaCy, you'll discover even more ways to leverage this fantastic feature in your NLP projects.

Popular tags

spaCyNLPPython

Share now!

Like & bookmark

Related collections

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 · Python

  • Mastering LangGraph: Stateful, Orchestration Framework

    17/11/2024 · Python

  • Python Basics: Comprehensive Guide

    21/09/2024 · Python

  • Matplotlib Mastery: From Plots to Pro Visualizations

    05/10/2024 · Python

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 · Python

Related articles

  • Understanding Basic Operators and Expressions in Python

    21/09/2024 · Python

  • Object Tracking with Python

    06/12/2024 · Python

  • Camera Calibration in Python

    06/12/2024 · Python

  • Crafting Custom Named Entity Recognizers in spaCy

    22/11/2024 · Python

  • Working with Python's C Extensions

    13/01/2025 · Python

  • Mastering Text Splitting and Chunking in Python with LlamaIndex

    05/11/2024 · Python

  • Advanced Exception Handling Techniques in Python

    13/01/2025 · Python

Popular category

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