-
-
Save oleganza/30701 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
# Given a set of documents this method returns a list of tags associated with | |
# those documents ordered by the ones occuring on the most documents. Tags th | |
# only appear on one doc are excluded from the list. | |
# If the user supplies a specific tag to exclude it will not be included | |
# in the list. | |
def related_tags(docs, exclude_tag = nil) | |
# DM triggers single SQL query for all docs' tags when doc.tags is called. | |
docs[0,20].inject({}) do |hash, doc| | |
doc.tags.inject(hash) do |hsh, tag| | |
hsh[tag] ||= 0 | |
hsh[tag] -= 1 unless exclude_tag == tag | |
hsh | |
end | |
end.to_a.sort_by{|(tag, c)| c }[0, 24].map{|(tag,c)| tag } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment