Created
February 1, 2010 17:39
-
-
Save bru/291846 to your computer and use it in GitHub Desktop.
Weighted Tanimoto algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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