- spaCy Tutorial
- spaCy - Home
- spaCy - Introduction
- spaCy - Getting Started
- spaCy - Models and Languages
- spaCy - Architecture
- spaCy - Command Line Helpers
- spaCy - Top-level Functions
- spaCy - Visualization Function
- spaCy - Utility Functions
- spaCy - Compatibility Functions
- spaCy - Containers
- Doc Class ContextManager and Property
- spaCy - Container Token Class
- spaCy - Token Properties
- spaCy - Container Span Class
- spaCy - Span Class Properties
- spaCy - Container Lexeme Class
- Training Neural Network Model
- Updating Neural Network Model
- spaCy Useful Resources
- spaCy - Quick Guide
- spaCy - Useful Resources
- spaCy - Discussion
spaCy - Token.set_extension Classmethod
This class method was introduced in version 2.0. It defines a custom attribute on the Token. Once done, that attribute will become available via Token._.
Arguments
The table below explains its arguments −
NAME | TYPE | DESCRIPTION |
---|---|---|
name | Unicode | This argument represents the name of the attribute to set by the extension. For example, ‘his_attr’ will be available as doc._.his_attr. |
default | - | It is the optional default value of the attribute for the case when no getter or method is defined. |
method | callable | It is used to set a custom method on the object. For example, token._.compare(other_token). |
getter | callable | This attribute represents the getter function that will takes the object and will return an attribute value. It is mainly called when the user accesses the ._ attribute. |
setter | callable | This attribute represents the Setter function that will take the Doc & a value and will modify the object. It is mainly called when the user writes to the Token._ attribute. |
Force | bool | It will forcefully overwrite an existing attribute. |
Example 1
An example of Token.set_extension class method is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") from spacy.tokens import Token fruit_getter = lambda token: token.text in ("apple", "pear", "banana") Token.set_extension("is_fruit", getter=fruit_getter, force=True) doc = nlp_model("I have an pear") doc[3]._.is_fruit
Output
True
Example 2
An another example of Token.set_extension class method is as follows −
import spacy nlp_model = spacy.load("en_core_web_sm") from spacy.tokens import Token fruit_getter = lambda token: token.text in ("apple", "pear", "banana") Token.set_extension("is_fruit", getter=fruit_getter, force=True) doc = nlp_model("I have a car") doc[3]._.is_fruit
Output
The output is mentioned below −
False
spacy_container_token_class.htm
Advertisements