Articles → SPACY → Spacy Pattern Matcher
Spacy Pattern Matcher
Purpose
Example
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
# Pattern:
# Hard-coded word "CPU" followed by a number (one token)
pattern = [
{"LOWER": "cpu"}, # hard-coded string
{"IS_DIGIT": True} # specifier (a number)
]
matcher.add("CPU_WITH_NUMBER", [pattern])
doc = nlp("The server has CPU 4 and CPU 16 cores.")
matches = matcher(doc)
for match_id, start, end in matches:
print("Matched:", doc[start:end].text)
Output
Using Regular Expression
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
# Pattern using REGEX:
# Match the hard-coded string CPU (case-insensitive)
# Followed by a number (regex for digits)
pattern = [
{"TEXT": {"REGEX": "(?i)cpu"}}, # hard-coded string using regex (case-insensitive)
{"TEXT": {"REGEX": "^[0-9]+$"}} # number using regex
]
matcher.add("CPU_WITH_NUMBER", [pattern])
doc = nlp("The server has CPU 4 and CPU 16 cores.")
matches = matcher(doc)
for match_id, start, end in matches:
print("Matched:", doc[start:end].text)
| Posted By - | Karan Gupta |
| |
| Posted On - | Sunday, November 30, 2025 |