<commit_before># encoding: UTF-8
require 'core_ext'
require 'shellrun'

module Metadata
  # Abstract base class providing a ShellRunner instance.
  # Define a `call` method to do the processing in children.
  class Processor
    def initialize
      @sh = ShellRunner.new
    end
  end

  class AggregatingProcessor < Processor
    attr_reader :keys
    def initialize(**keys, &block)
      keys.empty? and fail ArgumentError, "No aggregation keys given for #{self.class.name}."
      block.nil?  and fail ArgumentError, "No collector block given for #{self.class.name}."
      @keys      = keys
      @collector = block
      super()
    end

    def call(file)
      {}.tap {|hash|
        @keys.each {|metatada_key, collector_items|
          metadata_for_key = Array(collector_items).map {|collector_item|
            instance_exec(file, collector_item, &@collector)
          }.flatten.compact.uniq

          unless metadata_for_key.empty?
            hash[metatada_key] = metadata_for_key.count == 1 ? metadata_for_key.first : metadata_for_key
          end
        }
      }
    end
  end

  # YAML front matter: detect, read and strip.<commit_msg>Metadata: add #call noop to Processor<commit_after># encoding: UTF-8
require 'core_ext'
require 'shellrun'

module Metadata
  # Abstract base class providing a ShellRunner instance.
  # Define a `call` method to do the processing in children.
  class Processor
    def initialize
      @sh = ShellRunner.new
    end
    def call(file)
      nil
    end
  end

  class AggregatingProcessor < Processor
    attr_reader :keys
    def initialize(**keys, &block)
      keys.empty? and fail ArgumentError, "No aggregation keys given for #{self.class.name}."
      block.nil?  and fail ArgumentError, "No collector block given for #{self.class.name}."
      @keys      = keys
      @collector = block
      super()
    end

    def call(file)
      {}.tap {|hash|
        @keys.each {|metatada_key, collector_items|
          metadata_for_key = Array(collector_items).map {|collector_item|
            instance_exec(file, collector_item, &@collector)
          }.flatten.compact.uniq

          unless metadata_for_key.empty?
            hash[metatada_key] = metadata_for_key.count == 1 ? metadata_for_key.first : metadata_for_key
          end
        }
      }
    end
  end

  # YAML front matter: detect, read and strip.
{'fraction_non_alphanumeric': 0.08849557522123894, 'fraction_numerical': 0.001685630004214075, 'mean_word_length': 2.6299694189602447, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4905058', 'n_tokens_mistral': 734, 'n_tokens_neox': 703, 'n_words': 206}