DEV Community

Cover image for Cosine similarity
Ismaël Maurice
Ismaël Maurice

Posted on • Edited on

Cosine similarity

Similarity by cosine determines the degree of similarity between two sentences by calculating the cosine of the angle formed by the two vectors in each sentence.

Consider the following sentences:

  • Bonjour John.
  • Bonjour Doé.

Note that the two sentences are similar, but how can you tell with a computer?

Due to the difficulty of adding mathematical formulas to dev.to, you will find the article on the subject in the pdf available here: Cosine similarity

Code examples can be found here: Calculating similarity in different ways which shows various calculation methods natively with Python, SpaCy, NLTK.

Top comments (1)

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Dev does allow mathematical formulae:

c=±a2+b2 c = \pm\sqrt{a^2 + b^2}

Like this:

{% katex %}
c = \pm\sqrt{a^2 + b^2}
{% endkatex %}