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 spacyfrom spacy.language import Languageimport re# Step 1: Create a blank English modelnlp = 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 pipelinenlp.add_pipe("email_component")# Step 4: Test the pipelinetext = "Contact us at support@example.com or sales@example.org for more info."doc = nlp(text)# Print detected emailsprint("\nEmails detected:")for ent in doc.ents: print(ent.text, ent.label_)
Output
| Posted By - | Karan Gupta |
| |
| Posted On - | Monday, January 12, 2026 |