Articles → NLP → Create A Custom Pipeline To Detect Email In Text Using Spacy

Create A Custom Pipeline To Detect Email In Text Using Spacy






Example


import spacy
from spacy.language import Language
import re

# Step 1: Create a blank English model
nlp = spacy.blank("en")  # blank pipeline

# Step 2: Create a custom component to detect emails
@Language.component("email_component")
def email_component(doc):
    # Regex pattern for emails
    email_pattern = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
    
    entities = []
    for match in re.finditer(email_pattern, doc.text):
        start_char, end_char = match.span()
        span = doc.char_span(start_char, end_char, label="EMAIL")
        if span is not None:
            entities.append(span)
    
    # Set detected emails as entities
    doc.ents = entities
    return doc

# Step 3: Add the custom component to the pipeline
nlp.add_pipe("email_component")

# Step 4: Test the pipeline
text = "Contact us at support@example.com or sales@example.org for more info."
doc = nlp(text)

# Print detected emails
print("\nEmails detected:")
for ent in doc.ents:
    print(ent.text, ent.label_)



Output


Picture showing the output of creating custom pipeline to detect email in text using spacy





Posted By  -  Karan Gupta
 
Posted On  -  Monday, January 12, 2026

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250