Skip to content

Instantly share code, notes, and snippets.

@oleganza
Forked from kpumuk/gist:30569
Created December 1, 2008 10:13
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 oleganza/30701 to your computer and use it in GitHub Desktop.
Save oleganza/30701 to your computer and use it in GitHub Desktop.
# 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