<commit_before><commit_msg>Rake task for copying policy topics to editions<commit_after>namespace :topics do
  desc "Map topics assigned via policies directly onto editions"
  task from_policies_to_editions: :environment do

    class TopicTransfer < Struct.new(:logger, :edition)
      def copy_topics_from_policies
        existing_topics = edition.directly_associated_topics.pluck(:id)
        new_topics = policy_topics.select do |topic|
          ! existing_topics.include?(topic.id)
        end
        logger.info "Copying #{new_topics.size} topics to #{edition.slug}"
        edition.directly_associated_topics << new_topics
      end

      def policy_topics
        edition.related_policies.map(&:topics).flatten.uniq
      end
    end

    logger = Logger.new($stderr)

    [Announcement, Publicationesque].each do |edition_type|
      logger.info "-- Associating #{edition_type.name.downcase.pluralize} directly with policies' topics"
      edition_type.includes(:policy_topics).find_each do |announcement|
        TopicTransfer.new(logger, announcement).copy_topics_from_policies
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08125, 'fraction_numerical': 0.0, 'mean_word_length': 3.5942622950819674, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2059034', 'n_tokens_mistral': 341, 'n_tokens_neox': 337, 'n_words': 73}