Skip to content
Ruby gem to calculate the similarity between texts using tf*idf
Branch: master
Clone or download
James McKinney
James McKinney Remove Gemnasium
Latest commit 00777f8 May 23, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Bump version Mar 7, 2017
spec Revert "Add option to allow tokens to not be processed" Jan 17, 2016
.rspec add .rspec Apr 16, 2015
.travis.yml Drop support for Ruby 1.9.3 Mar 7, 2017
LICENSE Remove Gemnasium May 23, 2018
Rakefile first commit Sep 9, 2012

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

Gem Version Build 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)

Provide, by yourself, the number of times each term appears and the number of tokens in the document:

require 'unicode_utils'
text = "Lorem ipsum dolor sit amet..."
tokens = UnicodeUtils.each_word(text).to_a - ['and', 'the', 'to']
term_counts =
size = 0
tokens.each do |token|
  # Unless the token is numeric.
  unless token[/\A\d+\z/]
    # Remove all punctuation from tokens.
    term_counts[token.gsub(/\p{Punct}/, '')] += 1
    size += 1
document1 =, :term_counts => term_counts, :size => size)

Read the documentation at


NoMethodError: undefined method `[]' for Matrix:Module

The matrix gem conflicts with Ruby's internal Matrix module. Don't use the matrix gem.


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

You can’t perform that action at this time.