spacy-semantic-similarity

05-10-2022 || 02:26
Tags: #nlp #spacy

spacy-semantic-similarity

Spacy can compare two object similary. For that spacy uses word vectores. There are 3 methods

  1. Doc.similarity()
  2. Span.similarity()
  3. Token.similarity()

Similarity scores are from 0 to 1, 1 means full similar and 0 means no similarity

Not all the pipeline has word vectors included
✅ en_core_web_md
✅ en_core_web_lg
❌ en_core_web_sm

import spacy
nlp = spacy.load('en_core_web_md')

doc1 = nlp('i like you')
doc2 = nlp('i love you')

print(doc1.simiarity(doc2))

span = doc1[1:]
print(span.similarity(doc1))

token = doc1[1]
print(token.similarity(doc2))

# print token vectors
print(doc[1].vector)

References