Skip to content

Instantly share code, notes, and snippets.

@tomtaylor
Created August 5, 2008 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomtaylor/4047 to your computer and use it in GitHub Desktop.
Save tomtaylor/4047 to your computer and use it in GitHub Desktop.
def similar(n = 10)
other_tags = Tag.find(:all, :conditions => ['tags.id != ?', self.id], :include => :bookmarks)
other_tags.map do |b|
[b, tanimoto(b.bookmarks, self.bookmarks)] # compare tag listings
end.select do |b|
b.last > 0 # select only those which have a tanimoto greater than 0, ie. some shared tags
end.sort do |x,y|
y.last <=> x.last # sort on tanimoto
end[0..n].map(&:first) # return only 0..10 bookmarks
end
def tanimoto(a,b)
c = a.select { |v| b.include?(v) }
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