Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Ruby gem to calculate the similarity between texts using tf*idf

Ruby Vector Space Model (VSM) with tf*idf weights

Gem Version Build Status Dependency Status Coverage Status Code Climate

Calculates the similarity between texts using a bag-of-words Vector Space Model with Term Frequency-Inverse Document Frequency (tf*idf) weights. If your use case demands performance, use Lucene (see below).


require 'matrix'
require 'tf-idf-similarity'

Create a set of documents:

document1 ="Lorem ipsum dolor sit amet...")
document2 ="Pellentesque sed ipsum dui...")
document3 ="Nam scelerisque dui sed leo...")
corpus = [document1, document2, document3]

Create a document-term matrix using Term Frequency-Inverse Document Frequency function:

model =

Or, create a document-term matrix using the Okapi BM25 ranking function:

model =

Create a similarity matrix:

matrix = model.similarity_matrix

Find the similarity of two documents in the matrix:

matrix[model.document_index(document1), model.document_index(document2)]

Print the tf*idf values for terms in a document:

tfidf_by_term = {}
document1.terms.each do |term|
  tfidf_by_term[term] = model.tfidf(document1, term)
puts tfidf_by_term.sort_by{|_,tfidf| -tfidf}

Tokenize a document yourself, for example by excluding stop words:

require 'unicode_utils'
text = "Lorem ipsum dolor sit amet..."
tokens = UnicodeUtils.each_word(text).to_a - ['and', 'the', 'to']
document1 =, :tokens => tokens)

Read the documentation at


Instead of using the Ruby Standard Library's Matrix class, you can use one of the GNU Scientific Library (GSL), NArray or NMatrix (0.0.9 or greater) gems for faster matrix operations. For example:

require 'narray'
model =, :library => :narray)

NArray seems to have the best performance of the three libraries.

The NMatrix gem gives access to Automatically Tuned Linear Algebra Software (ATLAS), which you may know of through Linear Algebra PACKage (LAPACK) or Basic Linear Algebra Subprograms (BLAS). Follow these instructions to install the NMatrix gem.


You can access more term frequency, document frequency, and normalization formulas with:

require 'tf-idf-similarity/extras/document'
require 'tf-idf-similarity/extras/tf_idf_model'

The default tf*idf formula follows the Lucene Conceptual Scoring Formula.


At the time of writing, no other Ruby gem implemented the tf*idf formula used by Lucene, Sphinx and Ferret.

Term frequencies

  • The vss gem does not normalize the frequency of a term in a document; this occurs frequently in the academic literature, but only to demonstrate why normalization is important.
  • The tf_idf and similarity gems normalize the frequency of a term in a document to the number of terms in that document, which never occurs in the literature.
  • The tf-idf gem normalizes the frequency of a term in a document to the number of unique terms in that document, which never occurs in the literature.

Document frequencies

  • The vss gem does not normalize the inverse document frequency.
  • The treat, tf_idf, tf-idf and similarity gems use variants of the typical inverse document frequency formula.


  • The treat, tf_idf, tf-idf, rsemantic and vss gems have no normalization component.

Additional adapters

Adapters for the following projects were also considered:

  • Ruby-LAPACK is a very thin wrapper around LAPACK, which has an opaque Fortran-style naming scheme.
  • Linalg and RNum give access to LAPACK from Ruby but are old and unavailable as gems.


Further Reading

Lucene implements many more similarity functions, such as:

Lucene can even combine similarity measures.

Copyright (c) 2012 James McKinney, released under the MIT license

Something went wrong with that request. Please try again.