<commit_before>module GeneticAlgorithms
  class RouletteWheel

    def self.spin probability_distribution
      
    end
  end
end<commit_msg>Made progress on roulette wheel<commit_after>module GeneticAlgorithms
  class NaturalHash < Hash
    def map
      result = super()

      if result.is_a? Array and result.all? { |i| i.is_a? Array and i.length == 2}
        return NaturalHash[ result ]
      end

      result
    end
  end

  class RouletteWheel

    #TODO: THis works, but it is SERIOULY ugly
    def self.spin prob_dist
      prng  = rand
      total = prob_dist.values.inject(:+)

      normalized = prob_dist.map { |obj, prob| [obj, prob.to_f/total] }
      normalized = Hash[ normalized ].sort_by { |obj, prob| prob }

      accum = 0
      Hash[ normalized ].each_pair do |obj, prob| 
        return obj if prng <= prob + accum
        accum += prob
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07950727883538634, 'fraction_numerical': 0.0022396416573348264, 'mean_word_length': 2.3609022556390977, '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': '4215821', 'n_tokens_mistral': 290, 'n_tokens_neox': 276, 'n_words': 90}