python 15 lines · 1 tab

Natural language processing with spaCy pipelines and custom rules

1 tab
import spacy
from spacy.matcher import Matcher

nlp = spacy.load('en_core_web_sm')
matcher = Matcher(nlp.vocab)
matcher.add('INCIDENT_ID', [[{'TEXT': {'REGEX': '^INC-[0-9]{6}$'}}]])

text = 'Customer referenced INC-102301 and requested refund after a payment failure.'
doc = nlp(text)

entities = [(ent.text, ent.label_) for ent in doc.ents]
matches = [doc[start:end].text for _, start, end in matcher(doc)]

print(entities)
print(matches)
1 file · python Explain with highlit

I like spaCy for production NLP because it balances performance, ergonomics, and deployability. It is especially good for entity extraction, rule-based matching, and clean token-level processing. I often pair learned models with explicit match patterns when the domain has stable language conventions.

Share this code

Here's the card — post it anywhere.

Natural language processing with spaCy pipelines and custom rules — share card
Link copied