diff --git a/app/logical/related_tag_calculator.rb b/app/logical/related_tag_calculator.rb index d702dad37..595e343fc 100644 --- a/app/logical/related_tag_calculator.rb +++ b/app/logical/related_tag_calculator.rb @@ -9,6 +9,29 @@ class RelatedTagCalculator convert_hash_to_array(calculate_from_sample(tags, Danbooru.config.post_sample_size, category_constraint)) end + def self.calculate_from_post_set_to_array(post_set, category_constraint = nil) + convert_hash_to_array(calculate_from_post_set(post_set, category_constraint)) + end + + def self.calculate_from_post_set(post_set, category_constraint = nil) + counts = Hash.new {|h, k| h[k] = 0} + + post_set.posts.each do |post| + post.tag_array.each do |tag| + category = Tag.category_for(tag) + if category_constraint + if category == category_constraint + counts[tag] += 1 + end + else + counts[tag] += 1 + end + end + end + + counts + end + def self.calculate_from_sample(tags, limit, category_constraint = nil) counts = Hash.new {|h, k| h[k] = 0} diff --git a/app/presenters/post_set_presenters/post.rb b/app/presenters/post_set_presenters/post.rb index f360e7254..290f77c43 100644 --- a/app/presenters/post_set_presenters/post.rb +++ b/app/presenters/post_set_presenters/post.rb @@ -40,10 +40,14 @@ module PostSetPresenters if tag tag.related_tag_array.map(&:first) else - [] + calculate_related_tags_from_post_set end end + def calculate_related_tags_from_post_set + RelatedTagCalculator.calculate_from_post_set_to_array(post_set).map(&:first) + end + def tag_list_html(template) tag_set_presenter.tag_list_html(template) end diff --git a/test/unit/related_tag_calculator_test.rb b/test/unit/related_tag_calculator_test.rb index 8d1ba32d7..471b12d7f 100644 --- a/test/unit/related_tag_calculator_test.rb +++ b/test/unit/related_tag_calculator_test.rb @@ -14,6 +14,19 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase end context "A related tag calculator" do + context "for a post set" do + setup do + FactoryGirl.create(:post, :tag_string => "aaa bbb ccc ddd") + FactoryGirl.create(:post, :tag_string => "aaa bbb ccc") + FactoryGirl.create(:post, :tag_string => "aaa bbb") + @post_set = PostSets::Post.new("aaa") + end + + should "calculate the related tags" do + assert_equal({"aaa"=>3, "bbb"=>3, "ccc"=>2, "ddd"=>1}, RelatedTagCalculator.calculate_from_post_set(@post_set)) + end + end + should "calculate related tags for a tag" do posts = [] posts << FactoryGirl.create(:post, :tag_string => "aaa bbb ccc ddd")