spaCy - Token.similarity Method



This method is used to compute a semantic similarity estimate. The default is cosine over vectors.

Argument

The table below explains its argument −

NAME TYPE DESCRIPTION
other - It is the object with which the comparison will be done. By default, it will accept Doc, Span, Token, and Lexeme objects.

Example 1

An example of Token.similarity method is given below −

import spacy
nlp_model = spacy.load("en_core_web_sm")
apples, _, bananas = nlp_model("apples and bananas")
apples_bananas = apples.similarity(bananas)
bananas_apples = bananas.similarity(apples)
apples_bananas

Output

0.5698063

Example 2

An another example of Token.similarity method is given below −

bananas_apples

Output

The output is mentioned below −

0.5698063
spacy_container_token_class.htm
Advertisements