<commit_before>require 'i18n/tasks/key_pattern_matching'
require 'i18n/tasks/data/tree/node'

module I18n::Tasks
  module Data::Router
    # Keep the path, or infer from base locale
    class ConservativeRouter
      def initialize(adapter, config)
        @adapter       = adapter
        @base_locale   = config[:base_locale]
        @routes_config = config[:write]
      end

      def route(locale, forest, &block)
        return to_enum(:route, locale, forest) unless block
        out = {}
        forest.keys(root: false) do |key, node|
          path = node.data[:path]

          # infer from base
          unless path
            path = base_tree["#{base_locale}.#{key}"].try(:data).try(:[], :path)
            path = path.try :sub, /(?<=[\/.])#{base_locale}(?=\.)/, locale
          end

          if path
            (out[path] ||= Set.new) << "#{locale}.#{key}"
          else
            raise "could not find path for #{locale}.#{key}"
          end
        end
        out.each do |dest, keys|
          block.yield dest, forest.select_keys { |key, _| keys.include?(key) }
        end
      end

      protected

      def base_tree
        adapter[base_locale]
      end

      attr_reader :adapter, :base_locale
    end
  end
end
<commit_msg>conservativer router: use original path data, as the tree being set might not have it<commit_after>require 'i18n/tasks/key_pattern_matching'
require 'i18n/tasks/data/tree/node'

module I18n::Tasks
  module Data::Router
    # Keep the path, or infer from base locale
    class ConservativeRouter
      def initialize(adapter, config)
        @adapter       = adapter
        @base_locale   = config[:base_locale]
        @routes_config = config[:write]
      end

      def route(locale, forest, &block)
        return to_enum(:route, locale, forest) unless block
        out = {}
        forest.keys(root: false) do |key, node|
          locale_key = "#{locale}.#{key}"
          path = adapter[locale][locale_key].data[:path]

          # infer from base
          unless path
            path = base_tree["#{base_locale}.#{key}"].try(:data).try(:[], :path)
            path = path.try :sub, /(?<=[\/.])#{base_locale}(?=\.)/, locale
          end

          if path
            (out[path] ||= Set.new) << locale_key
          else
            raise "could not find path for #{locale_key}"
          end
        end
        out.each do |dest, keys|
          block.yield dest, forest.select_keys { |key, _| keys.include?(key) }
        end
      end

      protected

      def base_tree
        adapter[base_locale]
      end

      attr_reader :adapter, :base_locale
    end
  end
end

{'fraction_non_alphanumeric': 0.12122356495468278, 'fraction_numerical': 0.004531722054380665, 'mean_word_length': 2.0205245153933866, 'pattern_counts': {'":': 0, '<': 9, '<?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': '6338943', 'n_tokens_mistral': 794, 'n_tokens_neox': 785, 'n_words': 210}