<commit_before>require "csv"
require "httparty"
require "json"

module Debug
  class RankEval
    def initialize(datafile, ab_tests)
      @data = load_from_csv(datafile)
      @search_params = ab_tests.nil? ? {} : { "ab_tests" => [ab_tests] }
      @search_config = SearchConfig.parse_parameters(@search_params).search_config
    end

    def load_from_csv(datafile)
      data = {}
      last_query = ""
      CSV.foreach(datafile, headers: true) do |row|
        query = (row["query"] || last_query).strip
        score = row["score"]
        link = row["link"]

        raise "missing query for row '#{row}'" if query.nil?
        raise "missing score for row '#{row}'" if score.nil?
        raise "missing link for row '#{row}" if link.nil?

        data[query] = data.fetch(query, [])
        data[query] << { score: score.to_i, link: link }

        last_query = query
      end

      ignore_extra_judgements(data)
    end

    def evaluate
      requests = queries.each_with_object([]) do |(query, data), acc|
        acc << {
          id: query,
          request: {
            query: data[:es_query][:query],
            post_filter: data[:es_query][:post_filter],
          },
          ratings: data[:judgements].map do |judgement|
            {
              _index: index_for_link(judgement[:link]),
              _id: judgement[:link],
              rating: judgement[:score],
            }
          end,
        }
      end

      result = rank_eval(requests)

      {
        score: result["metric_score"],
        # rubocop:disable Style/HashTransformValues
        query_scores: result["details"].each_with_object({}) do |(query, detail), acc|
          acc[query] = detail["metric_score"]
        end,
        # rubocop:enable Style/HashTransformValues
      }
    end

    def queries
      @queries ||= @data.each_with_object({}) do |(query, judgements), queries|
        queries[query] = {
          es_query: SearchConfig.generate_query(@search_params.merge("q" => [query])),
          judgements: judgements,
        }
      end
    end<commit_msg>Fix unnecessary disabling of Style/HashTransformValues<commit_after>require "csv"
require "httparty"
require "json"

module Debug
  class RankEval
    def initialize(datafile, ab_tests)
      @data = load_from_csv(datafile)
      @search_params = ab_tests.nil? ? {} : { "ab_tests" => [ab_tests] }
      @search_config = SearchConfig.parse_parameters(@search_params).search_config
    end

    def load_from_csv(datafile)
      data = {}
      last_query = ""
      CSV.foreach(datafile, headers: true) do |row|
        query = (row["query"] || last_query).strip
        score = row["score"]
        link = row["link"]

        raise "missing query for row '#{row}'" if query.nil?
        raise "missing score for row '#{row}'" if score.nil?
        raise "missing link for row '#{row}" if link.nil?

        data[query] = data.fetch(query, [])
        data[query] << { score: score.to_i, link: link }

        last_query = query
      end

      ignore_extra_judgements(data)
    end

    def evaluate
      requests = queries.each_with_object([]) do |(query, data), acc|
        acc << {
          id: query,
          request: {
            query: data[:es_query][:query],
            post_filter: data[:es_query][:post_filter],
          },
          ratings: data[:judgements].map do |judgement|
            {
              _index: index_for_link(judgement[:link]),
              _id: judgement[:link],
              rating: judgement[:score],
            }
          end,
        }
      end

      result = rank_eval(requests)

      {
        score: result["metric_score"],
        query_scores: result["details"].each_with_object({}) do |(query, detail), acc|
          acc[query] = detail["metric_score"]
        end,
      }
    end

    def queries
      @queries ||= @data.each_with_object({}) do |(query, judgements), queries|
        queries[query] = {
          es_query: SearchConfig.generate_query(@search_params.merge("q" => [query])),
          judgements: judgements,
        }
      end
    end
{'fraction_non_alphanumeric': 0.12878787878787878, 'fraction_numerical': 0.0, 'mean_word_length': 2.2279179810725553, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '6849955', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1216, 'n_words': 271}