<commit_before>module Multivariate
  class Experiment
    attr_accessor :name
    attr_accessor :alternatives

    def initialize(name, *alternatives)
      @name = name.to_s
      @alternatives = alternatives
    end

    def alternatives
      @alternatives.map {|a| Multivariate::Alternative.find_or_create(a, name)}
    end

    def random_alternative
      alternatives[rand(alternatives.size)]
    end

    def save
      Multivariate.redis.sadd(:experiments, name)
      @alternatives.each {|a| Multivariate.redis.sadd(name, a) }
    end

    def self.find(name)
      if Multivariate.redis.exists(name)
        self.new(name, *Multivariate.redis.smembers(name))
      else
        raise 'Experiment not found'
      end
    end

    def self.find_or_create(name, *alternatives)
      if Multivariate.redis.exists(name)
        return self.new(name, *Multivariate.redis.smembers(name))
      else
        experiment = self.new(name, *alternatives)
        experiment.save
        return experiment
      end
    end
  end
end<commit_msg>method to load all experiments<commit_after>module Multivariate
  class Experiment
    attr_accessor :name
    attr_accessor :alternatives

    def initialize(name, *alternatives)
      @name = name.to_s
      @alternatives = alternatives
    end

    def alternatives
      @alternatives.map {|a| Multivariate::Alternative.find_or_create(a, name)}
    end

    def random_alternative
      alternatives[rand(alternatives.size)]
    end

    def save
      Multivariate.redis.sadd(:experiments, name)
      @alternatives.each {|a| Multivariate.redis.sadd(name, a) }
    end

    def self.all
      Array(Multivariate.redis.smembers(:experiments)).map {|e| find(e)}
    end

    def self.find(name)
      if Multivariate.redis.exists(name)
        self.new(name, *Multivariate.redis.smembers(name))
      else
        raise 'Experiment not found'
      end
    end

    def self.find_or_create(name, *alternatives)
      if Multivariate.redis.exists(name)
        return self.new(name, *Multivariate.redis.smembers(name))
      else
        experiment = self.new(name, *alternatives)
        experiment.save
        return experiment
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08938294010889292, 'fraction_numerical': 0.0, 'mean_word_length': 2.73728813559322, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2988933', 'n_tokens_mistral': 723, 'n_tokens_neox': 677, 'n_words': 145}