Skip to content

Instantly share code, notes, and snippets.

@bru
Created February 1, 2010 17:39
Show Gist options
  • Save bru/291846 to your computer and use it in GitHub Desktop.
Save bru/291846 to your computer and use it in GitHub Desktop.
Weighted Tanimoto algorithm
# weighted tanimoto for taggables
# works with acts_as_taggable_on_steroids,
# assuming you have a weight attribute in your Tagging model
def tanimoto(a,b)
a_size = a.taggings.map { |t| t.weight}.sum
b_size = b.taggings.map { |t| t.weight}.sum
return 0 if (a_size == 0 || b_size == 0)
c_size = a.taggings.map { |t| b.tag_list.include?(t.name) ? [t.weight, b.taggings.find_by_tag_id(t.tag_id).weight].min : 0 }.sum
coefficient = (c_size.to_f/(a_size + b_size - c_size))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment