Created
August 5, 2008 09:21
-
-
Save tomtaylor/4047 to your computer and use it in GitHub Desktop.
This file contains 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
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