Document Similarity Measures and Techniques

Natural Language Processing (NLP) is a field of study that focuses on the interaction between computers and human language. One of the important tasks in NLP is measuring the similarity between documents. Document similarity can be useful in various applications such as text classification, plagiarism detection, and recommendation systems. In this article, we will explore different measures and techniques used to compute document similarity using Python.

Bag-of-Words Approach

One of the simplest and most commonly used techniques for measuring document similarity is the Bag-of-Words (BoW) approach. In this approach, we represent each document as a collection of words, ignoring grammar and word order. The basic steps involved in the BoW approach are as follows:

  1. Tokenization: Splitting the documents into words or tokens.
  2. Text cleaning: Removing stopwords, punctuation, and other irrelevant characters.
  3. Feature extraction: Creating a unique set of words from all the documents, known as the vocabulary.
  4. Document representation: Creating a numerical vector representation for each document using word frequencies or presence/absence indicators.

Once we have the numerical representation for each document, we can compute the similarity between them using measures such as Cosine Similarity or Jaccard Similarity.

Cosine Similarity

Cosine Similarity is a commonly used measure to determine the similarity between two documents represented as vectors. It calculates the cosine of the angle between the two vectors, which ranges from -1 to 1. A higher cosine similarity indicates a higher degree of similarity between the documents. The cosine similarity formula is as follows:

Cosine Similarity Formula

Here, A and B are the document vectors.

To compute the cosine similarity between two documents using Python, we can utilize libraries such as scikit-learn or gensim. These libraries provide efficient implementations of the cosine similarity measure, along with various other NLP functionalities.

Jaccard Similarity

Jaccard Similarity is another measure commonly used for measuring document similarity, especially in cases where we are interested in set-like documents. It is defined as the size of the intersection divided by the size of the union of the document sets. The Jaccard Similarity score ranges from 0 to 1, with higher values indicating greater similarity between the documents.

The Jaccard Similarity formula is as follows:

Jaccard Similarity Formula

Here, A and B are the sets representing the documents.

Python provides several libraries, such as scikit-learn and nltk, which offer functions to compute Jaccard Similarity efficiently.

Word Embeddings

Word embeddings have gained significant popularity in recent years for measuring document similarity. Word embeddings capture the semantic relationships between words by representing them as dense vectors in a high-dimensional space. These vectors are learned from large text corpora using techniques like Word2Vec, GloVe, or FastText.

To compute document similarity using word embeddings, we can utilize techniques such as Word Movers Distance (WMD) or Word Centroid Distance. WMD measures the dissimilarity between two documents as the minimum distance that the embedded words of one document need to "travel" to reach the embedded words of another document. Word Centroid Distance, on the other hand, represents each document as the centroid of its word vectors and calculates the distance between the centroids.

Python libraries like gensim and spaCy provide efficient implementations of WMD and Word Centroid Distance for computing document similarity using word embeddings.


Measuring document similarity is a crucial task in NLP with applications ranging from text classification to recommendation systems. In this article, we explored various techniques and measures for computing document similarity using Python. The Bag-of-Words approach, along with cosine similarity and Jaccard similarity, provides a simpler solution. However, word embeddings, such as Word Movers Distance and Word Centroid Distance, offer more advanced and contextually aware methods. Depending on the specific application and requirements, one can choose the most suitable technique to measure document similarity effectively.

noob to master © copyleft