0
0
NLPml~10 mins

Custom pipeline components in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Atext
Btokens
Cdoc
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of the doc object.
Returning the text string instead of the doc object.
2fill in blank
medium

Complete 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'
Apipeline
Bprocess_doc
Ccomponent
Dcustom_component
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of the function name.
Using a wrong function name that is not defined.
3fill in blank
hard

Fix 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'
Apos
B_.is_positive
Cis_positive
Dset_attr
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to set token.is_positive directly without registering the attribute.
Using a wrong attribute name or syntax.
4fill in blank
hard

Fill 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'
Ais_positive
BTrue
CFalse
Dsentiment
Attempts:
3 left
💡 Hint
Common Mistakes
Using True as default when you want False.
Using a different attribute name than used in the component.
5fill in blank
hard

Fill 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'
Acustom_component
Bis_positive
C{'good', 'great', 'happy'}
Dpositive_tokens
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.