Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a custom pipeline component function that takes a doc and returns it.
NLP
def custom_component(doc): # Process the doc here return [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of the doc object.
Returning the text string instead of the doc object.
✗ Incorrect
The custom pipeline component must return the processed doc object to continue the pipeline.
2fill in blank
mediumComplete the code to add the custom component to the pipeline named 'custom_component'.
NLP
nlp.add_pipe([1], name='custom_component', last=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of the function name.
Using a wrong function name that is not defined.
✗ Incorrect
You add the custom component function by passing its name to add_pipe.
3fill in blank
hardFix the error in the custom component that tries to add an attribute 'is_positive' to each token.
NLP
def custom_component(doc): for token in doc: token.[1] = token.text.startswith('good') return doc
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to set token.is_positive directly without registering the attribute.
Using a wrong attribute name or syntax.
✗ Incorrect
Custom token attributes must be set using the underscore accessor '_.attribute_name'.
4fill in blank
hardFill both blanks to register a custom token attribute 'is_positive' with default False.
NLP
from spacy.tokens import Token Token.set_extension('[1]', default=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using True as default when you want False.
Using a different attribute name than used in the component.
✗ Incorrect
You register the attribute name 'is_positive' with default value False.
5fill in blank
hardFill all three blanks to create a custom pipeline component that sets 'is_positive' on tokens and returns the doc.
NLP
def [1](doc): for token in doc: token._.[2] = token.text.lower() in [3] return doc
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than registered.
Not using a set for the positive words.
Forgetting to return the doc.
✗ Incorrect
The function name is 'custom_component', attribute is 'is_positive', and the set of positive words is {'good', 'great', 'happy'}.