Cosine Similarity: Measuring the Similarity of Content
Cosine similarity measures how similar two pieces of content are – via the angle between their vectors. The measure is a core building block of semantic SEO analyses.
What you will learn
- What cosine similarity is and what it measures
- Why it builds on vectors (embeddings) and what the angle means
- How search engines and SEO use the concept
- Where you calculate cosine similarity in practice – from Python to Google Sheets
- Which advantages and limits the measure has
What is cosine similarity?
Cosine similarity is a mathematical measure that determines the similarity between two vectors via the angle between them. A smaller angle means higher similarity, a larger angle lower. The clever part: the length (magnitude) of the vectors plays no role – only their direction counts. This is exactly why the measure is excellently suited to measuring the content closeness of texts, regardless of their length.
The concept is present in many areas: in text analysis, in recommendation systems and in search engine technology, everywhere the relevance of documents or the similarity of content is assessed.
The foundation: embeddings
Before you can calculate cosine similarity, you need embeddings. These are numerical representations of words, sentences or entire documents in the form of vectors – that is, lists of numbers. They map the semantic meaning of a text in a multidimensional space: similar content lies close together in it, different content further apart.
Imagine each text as a point in a complex coordinate system. Models like Google’s BERT are trained to generate such meaningful vectors from texts. These numerical representations are the basis on which cosine similarity can be calculated in the first place. How embeddings arise and function in detail is explored in depth in the article Vector Embeddings.
Cosine similarity in detail
At its core, cosine similarity measures the cosine of the angle between two vectors. The values are easy to read:
- Value 1: The vectors point in the same direction (angle 0°) – maximum similarity.
- Value 0: The vectors are perpendicular to each other (90°) – no similarity.
- Value -1: The vectors point in opposite directions (180°) – maximum dissimilarity. With typical text embeddings, which are often non-negative, this rarely occurs.
Simply put: concepts with high cosine similarity are more strongly related. Thus "search engine optimization" and "online marketing" have a higher cosine similarity than "shark" and "online marketing".
Cosine similarity measures the angle between two vectors – a small angle means high similarity, a large one low.
The scale ranges from 1 (identical) through 0 (independent) to -1 (opposite).
The formula
The cosine similarity of two vectors A and B is calculated as:
Here, A · B is the dot product of the vectors, and ||A|| or ||B|| are their Euclidean lengths. The division by the lengths normalizes the vectors to unit length – so only the direction counts, not the magnitude.
Value | Interpretation |
|---|---|
1 | Perfect match (same direction) |
close to 1 | High similarity |
close to 0 | Low or no similarity (orthogonal) |
close to -1 | High dissimilarity (rare with text embeddings) |
Areas of application in SEO
Search engines and relevance
Search engines like Google use concepts similar to cosine similarity to assess the relevance of pages for a search query. The query is converted into a vector, as is the content of the indexed pages. Via the similarity between query and document vectors, the most fitting pages are identified and ranked. This semantic understanding is a central building block of modern, AI-supported search – see SEO in the AI Era.
Content and on-page strategy
For content work, understanding cosine similarity helps to better align content with search intent and search algorithms:
- Relevant terminology: use core terms and related terminology prominently.
- Clear content structure: place important content higher up and understandably.
- Sharpen the focus: remove sections of text that contribute nothing to the main topic.
- Meaningful headings: they should clearly reflect the main topics.
In e-commerce this can be used concretely: consistent product names with relevant terms, descriptive SEO texts on category pages, a well-thought-out internal linking between thematically related categories and relevant product reviews strengthen the thematic relevance of a page.
Further fields of application
Beyond SEO, cosine similarity is present in many systems: in recommendation systems (which content fits the user profile?), in plagiarism detection, in document clustering and in k-nearest neighbor algorithms (k-NN), where it serves as a distance measure. In SEO, clustering is particularly useful, for example for keyword clustering.
Calculating cosine similarity in practice
This is how you proceed in practice:
- Generate embeddings: Convert the texts to be compared into vectors – with pre-trained models like BERT, Word2Vec or GloVe or via embedding APIs from providers like OpenAI.
- Apply the formula: Libraries in Python like scikit-learn or SciPy offer ready-made functions for the calculation – you don’t have to implement the mathematics yourself.
For smaller amounts of data it even works in Google Sheets: via custom functions with Google Apps Script – a JavaScript-based language – cosine similarity and k-NN can be calculated directly in the spreadsheet. The embeddings, which are present as text, are first converted into number arrays.
For large amounts of data, however, Sheets is too slow. In that case, Python solutions or cloud platforms like BigQuery with integrated embedding and distance functions are the more efficient choice.
Advantages and limits
The strengths of cosine similarity:
- Good with high-dimensional data: it works reliably with the dense vectors from text embeddings.
- Independent of length: Since only the angle counts, documents of different lengths do not distort the result. A short and a long text can be considered very similar if they match thematically.
- Captures meaning: With good embedding models it measures semantic similarity accurately.
You should know the limits:
- Loss of magnitude: Sometimes the length (e.g. word frequency) is important information that is lost.
- Dependent on preprocessing: The quality of the embeddings – and thus of the result – depends strongly on the chosen model and the data preparation.
- Zero vectors: For the zero vector, cosine similarity is not defined.
Conclusion
Cosine similarity bridges qualitative content and quantitative analysis: as soon as texts are available as vectors, their similarity can be measured precisely – regardless of length. For SEO, the concept is the basis of semantic analyses: from relevance assessment through content clustering to recommendations. As AI and language models continue to develop, the importance of such similarity measures will keep increasing. Anyone who works with text data, search or machine learning should have understood the principle.
FAQ
Frequently asked questions
It measures the cosine of the angle between two vectors. A value close to 1 means high similarity (same direction), a value close to 0 no similarity (perpendicular), a value close to -1 opposition. The length of the vectors is not factored in.
Because this way a short and a long text can be considered similar if they cover the same topic. The assessment depends on the content orientation, not on the amount of text – ideal for comparing pages thematically.
For small amounts of data, Google Sheets with a custom function via Google Apps Script suffices. For larger analyses, Python (scikit-learn, SciPy) or cloud platforms like BigQuery are the better choice.
Embeddings are the numerical representation of content as vectors. Cosine similarity is the measure with which you calculate the closeness of two such vectors. First come the embeddings, then the similarity measurement.
Google does not disclose its algorithms, but demonstrably uses embeddings and similar vector techniques to capture meaning and relevance. Cosine similarity is a common measure in this kind of semantic processing.
Quiz
Test your knowledge
Five questions on cosine similarity.
Question 1 of 5
What does cosine similarity between two vectors measure?