<commit_before>module ReadmeScore
  class Document
    class Score

      SCORE_METRICS = [
        {
          metric: :number_of_code_blocks,
          value_per: 5,
          max: 40
        },
        {
          metric: :number_of_non_code_sections,
          value_per: 5,
          max: 30
        },
        {
          metric: :has_lists?,
          value: 10
        },
        {
          metric: :number_of_images,
          value_per: 5,
          max: 15
        },
        {
          metric: :number_of_gifs,
          value_per: 5,
          max: 15
        },
        {
          metric: :cumulative_code_block_length,
          value_per: 0.0009475244447271192,
          max: 10
        },
        {
          metric_name: :low_code_block_penalty,
          metric: :number_of_code_blocks,
          if_less_than: 3,
          value: -10
        }
      ]

      attr_accessor :metrics

      def initialize(metrics)
        @metrics = metrics
      end

      def score_breakdown
        breakdown = {}
        SCORE_METRICS.each { |h|
          metric_option = OpenStruct.new(h)
          metric_name = metric_option.metric_name || metric_option.metric
          metric_score_value = 0
          # points for each occurance
          if metric_option.value_per
            metric_score_value = [metrics.send(metric_option.metric) * metric_option.value_per, metric_option.max].min
          elsif metric_option.if_less_than
            if metrics.send(metric_option.metric) < metric_option.if_less_than
              metric_score_value = metric_option.value
            end
          else
            metric_score_value = metrics.send(metric_option.metric) ? metric_option.value : 0
          end
          breakdown[metric_name] = metric_score_value
        }
        breakdown
      end
      alias_method :breakdown, :score_breakdown

      def total_score
        score = 0
        score_breakdown.each {|metric, points|
          score += points.to_i
        }
        [score, 100].min
      end
      alias_method :to_i, :total_score

      def to_f
        to_i.to_f
      end

      def inspect
        "#<#{self.class} - #{total_score}>"<commit_msg>force a range >= 0<commit_after>module ReadmeScore
  class Document
    class Score

      SCORE_METRICS = [
        {
          metric: :number_of_code_blocks,
          value_per: 5,
          max: 40
        },
        {
          metric: :number_of_non_code_sections,
          value_per: 5,
          max: 30
        },
        {
          metric: :has_lists?,
          value: 10
        },
        {
          metric: :number_of_images,
          value_per: 5,
          max: 15
        },
        {
          metric: :number_of_gifs,
          value_per: 5,
          max: 15
        },
        {
          metric: :cumulative_code_block_length,
          value_per: 0.0009475244447271192,
          max: 10
        },
        {
          metric_name: :low_code_block_penalty,
          metric: :number_of_code_blocks,
          if_less_than: 3,
          value: -10
        }
      ]

      attr_accessor :metrics

      def initialize(metrics)
        @metrics = metrics
      end

      def score_breakdown
        breakdown = {}
        SCORE_METRICS.each { |h|
          metric_option = OpenStruct.new(h)
          metric_name = metric_option.metric_name || metric_option.metric
          metric_score_value = 0
          # points for each occurance
          if metric_option.value_per
            metric_score_value = [metrics.send(metric_option.metric) * metric_option.value_per, metric_option.max].min
          elsif metric_option.if_less_than
            if metrics.send(metric_option.metric) < metric_option.if_less_than
              metric_score_value = metric_option.value
            end
          else
            metric_score_value = metrics.send(metric_option.metric) ? metric_option.value : 0
          end
          breakdown[metric_name] = metric_score_value
        }
        breakdown
      end
      alias_method :breakdown, :score_breakdown

      def total_score
        score = 0
        score_breakdown.each {|metric, points|
          score += points.to_i
        }
        [[score, 100].min, 0].max
      end
      alias_method :to_i, :total_score

      def to_f
        to_i.to_f
      end

      def inspect
        "#<#{self.class} - #{total_score}>"
{'fraction_non_alphanumeric': 0.07593778591033852, 'fraction_numerical': 0.021043000914913082, 'mean_word_length': 1.5468841001747233, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '4902361', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1296, 'n_words': 247}