<commit_before>module BaseballStats
  module Calculators
    class TripleCrownEligible
      include Calculators

      def calculate
        al_winner = find_winner(AMERICAN_LEAGUE)
        nl_winner = find_winner(NATIONAL_LEAGUE)
        { 'American League' => al_winner[PLAYER_ID],
          'National League' => nl_winner[PLAYER_ID] }
      end

      private
      def find_winner(league)
        players = select_eligible_players(league)
        max_RBI = get_max_stat(players, RBI)
        max_HR  = get_max_stat(players, HOMERUNS)

        rbi_matches = players.select { |p| p[RBI] == max_RBI }
        hr_matches  = rbi_matches.select { |p| p[HOMERUNS] == max_HR }
        hr_matches.max_by { |p| batting_average(p) } || { PLAYER_ID => 0 }
      end

      def batting_average(stats)
        ImprovedBattingAverage.formula(stats)
      end

      def get_max_stat(collection, stat)
        collection.max_by { |p| p[stat] }[stat] rescue 0
      end

      def select_eligible_players(league)
        eligible_players.select { |p| p[LEAGUE] == league }
      end

      def eligible_players
        select_from_csv do |player|
          player[AT_BATS] > 399 && player[YEAR_ID] == year
        end
      end
    end
  end
end<commit_msg>Make triple crown eligible winner / no winner logic readable<commit_after>module BaseballStats
  module Calculators
    class TripleCrownEligible
      include Calculators

      def calculate
        al_winner = find_winner(AMERICAN_LEAGUE)
        nl_winner = find_winner(NATIONAL_LEAGUE)
        { 'American League' => al_winner[PLAYER_ID],
          'National League' => nl_winner[PLAYER_ID] }
      end

      private
      def find_winner(league)
        players = select_eligible_players(league)
        max_RBI = get_max_stat(players, RBI)
        max_HR  = get_max_stat(players, HOMERUNS)

        rbi_matches = players.select { |p| p[RBI] == max_RBI }
        hr_matches  = rbi_matches.select { |p| p[HOMERUNS] == max_HR }
        winner      = hr_matches.max_by { |p| batting_average(p) }

        winner || no_winner
      end

      def no_winner
        { PLAYER_ID => 0 }
      end

      def batting_average(stats)
        ImprovedBattingAverage.formula(stats)
      end

      def get_max_stat(collection, stat)
        collection.max_by { |p| p[stat] }[stat] rescue 0
      end

      def select_eligible_players(league)
        eligible_players.select { |p| p[LEAGUE] == league }
      end

      def eligible_players
        select_from_csv do |player|
          player[AT_BATS] > 399 && player[YEAR_ID] == year
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08409785932721713, 'fraction_numerical': 0.00382262996941896, 'mean_word_length': 2.4298820445609435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3431559', 'n_tokens_mistral': 929, 'n_tokens_neox': 873, 'n_words': 179}