Advanced NLP with spaCy
Ines Montani
spaCy core developer
nlp
doc.ents
doc
, modifies it and returns itnlp.add_pipe
methoddef custom_component(doc): # Do something to the doc here return doc
nlp.add_pipe(custom_component)
def custom_component(doc):
# Do something to the doc here
return doc
nlp.add_pipe(custom_component)
Argument | Description | Example |
---|---|---|
last |
If True , add last |
nlp.add_pipe(component, last=True) |
first |
If True , add first |
nlp.add_pipe(component, first=True) |
before |
Add before component | nlp.add_pipe(component, before='ner') |
after |
Add after component | nlp.add_pipe(component, after='tagger') |
# Create the nlp object nlp = spacy.load('en_core_web_sm')
# Define a custom component def custom_component(doc):
# Print the doc's length print('Doc length:' len(doc))
# Return the doc object return doc
# Add the component first in the pipeline nlp.add_pipe(custom_component, first=True)
# Print the pipeline component names print('Pipeline:', nlp.pipe_names)
Pipeline: ['custom_component', 'tagger', 'parser', 'ner']
# Create the nlp object nlp = spacy.load('en_core_web_sm') # Define a custom component def custom_component(doc): # Print the doc's length print('Doc length:' len(doc)) # Return the doc object return doc # Add the component first in the pipeline nlp.add_pipe(custom_component, first=True)
# Process a text doc = nlp("Hello world!")
Doc length: 3
Advanced NLP with spaCy