<commit_before>module Economy
  class Base
    DEFAULT_INTERVAL = { from: Date.new(0), to: Date.today }.freeze

    # Implement a #sum method in the subclass if the hashes in the as_array
    #   doesn't have :days and :amount
    def sum
      as_array.sum { |x| x[:days] * x[:amount] }
    end

    # Implement a #as_formula method in the subclass if the hashes in the as_array
    #   doesn't have :days and :amount
    def as_formula
      as_array.map do |x|
        next if x.value? 0

        "#{x[:days]}*#{x[:amount]}"
      end.compact.join('+')
    end

    protected

    def as_array(*_args)
      raise NotImplementedError, "Implement #{__method__} method in your #{self.class.name} subclass"
    end

    def number_of_days(from, to)
      days = (to.to_date - from.to_date).to_i + 1
      return 0 if !days.is_a?(Integer) || days.negative?

      days
    end

    def earliest_date(*dates)
      dates.compact.map(&:to_date).min
    end

    def latest_date(*dates)
      dates.compact.map(&:to_date).max
    end

    # Returns the number of months as a float
    #   calculated from the total days in each month
    def number_of_months(interval)
      (interval[:from]..interval[:to]).sum do |date|
        1.to_f / (date.end_of_month - date.beginning_of_month + 1)
      end
    end

    # Returns the minimum section of the cost interval and the report interval
    def date_interval(period_start, period_end, report_interval)
      {
        from: latest_date(period_start, report_interval[:from]),
        to: earliest_date(period_end, report_interval[:to])
      }
    end

    def remove_overlaps_in_date_intervals(intervals)
      intervals.map! do |interval|
        (intervals - [interval]).each do |other_interval|
          if (other_interval[:from]..other_interval[:to]).cover? interval[:from]
            interval[:from] = other_interval[:to] + 1.day
          end
        end
        next nil if interval[:from] > interval[:to]

        interval
      end.compact
    end
  end
end<commit_msg>Comment<commit_after>module Economy
  class Base
    DEFAULT_INTERVAL = { from: Date.new(0), to: Date.today }.freeze

    # Implement a #sum method in the subclass if the hashes in the as_array
    #   doesn't have :days and :amount
    def sum
      as_array.sum { |x| x[:days] * x[:amount] }
    end

    # Implement a #as_formula method in the subclass if the hashes in the as_array
    #   doesn't have :days and :amount
    def as_formula
      as_array.map do |x|
        next if x.value? 0

        "#{x[:days]}*#{x[:amount]}"
      end.compact.join('+')
    end

    protected

    def as_array(*_args)
      raise NotImplementedError, "Implement #{__method__} method in your #{self.class.name} subclass"
    end

    def number_of_days(from, to)
      days = (to.to_date - from.to_date).to_i + 1
      return 0 if !days.is_a?(Integer) || days.negative?

      days
    end

    def earliest_date(*dates)
      dates.compact.map(&:to_date).min
    end

    def latest_date(*dates)
      dates.compact.map(&:to_date).max
    end

    # Returns the number of months as a float
    #   calculated from the total days in each month
    def number_of_months(interval)
      (interval[:from]..interval[:to]).sum do |date|
        1.to_f / (date.end_of_month - date.beginning_of_month + 1)
      end
    end

    # Returns the minimum section of the cost interval and the report interval
    def date_interval(period_start, period_end, report_interval)
      {
        from: latest_date(period_start, report_interval[:from]),
        to: earliest_date(period_end, report_interval[:to])
      }
    end

    # Removes overlaps in date intervals
    # Takes an array of date intervals of the form { from: Date, to: Date }
    # Returns an array of hashes in the same format
    def remove_overlaps_in_date_intervals(intervals)
      intervals.map! do |interval|
        (intervals - [interval]).each do |other_interval|
          if (other_interval[:from]..other_interval[:to]).cover? interval[:from]
            interval[:from] = other_interval[:to] + 1.day
          end
        end
        next nil if interval[:from] > interval[:to]

        interval
      end.compact
    end
  end
end
{'fraction_non_alphanumeric': 0.10249110320284698, 'fraction_numerical': 0.0033214709371293, 'mean_word_length': 2.5942028985507246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1322447', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1314, 'n_words': 380}