<commit_before>module Genome
  module Groupers
    class DrugGrouper
      @alt_to_pubchem = Hash.new() {|hash, key| hash[key] = []}
      @alt_to_other = Hash.new() {|hash, key| hash[key] = []}
      @alt_to_pubchem_cid = Hash.new() {|hash, key| hash[key] = []}

      def self.run
        ActiveRecord::Base.transaction do
          puts 'preload'
          preload
          puts 'create groups'
          create_groups
          puts 'add members'
          add_members
        end
      end

      def self.preload
        DataModel::DrugClaimAlias.includes(drug_claim: [:drugs, :source]).all.each do |dca|
          drug_claim_alias = dca.alias
          if drug_claim_alias =~ /^\d+$/
            if dca.nomenclature =~ /pubchem.*(compound)|(cid)/i
              @alt_to_pubchem_cid[drug_claim_alias] << dca
            else
              next
            end
          elsif drug_claim_alias.length == 1
            next
          elsif dca.nomenclature == 'pubchem_primary_name'
            @alt_to_pubchem[drug_claim_alias] << dca
          else
            @alt_to_other[drug_claim_alias] << dca
          end
        end
      end

      def self.create_groups
        @alt_to_pubchem.each_key do |key|
          drug_claims = @alt_to_pubchem[key].map(&:drug_claim)
          drug = DataModel::Drug.where(name: key).first
          if drug 
            drug_claims.each do |drug_claim|
              drug_claim.drugs << drug unless drug_claim.drugs.include?(drug)
              drug_claim.save
            end
          else<commit_msg>add reset method<commit_after>module Genome
  module Groupers
    class DrugGrouper
      @alt_to_pubchem = Hash.new() {|hash, key| hash[key] = []}
      @alt_to_other = Hash.new() {|hash, key| hash[key] = []}
      @alt_to_pubchem_cid = Hash.new() {|hash, key| hash[key] = []}

      def self.run
        ActiveRecord::Base.transaction do
          puts 'preload'
          preload
          puts 'create groups'
          create_groups
          puts 'add members'
          add_members
        end
      end

      def self.reset
        DataModel::Drug.destroy_all
      end

      def self.preload
        DataModel::DrugClaimAlias.includes(drug_claim: [:drugs, :source]).all.each do |dca|
          drug_claim_alias = dca.alias
          if drug_claim_alias =~ /^\d+$/
            if dca.nomenclature =~ /pubchem.*(compound)|(cid)/i
              @alt_to_pubchem_cid[drug_claim_alias] << dca
            else
              next
            end
          elsif drug_claim_alias.length == 1
            next
          elsif dca.nomenclature == 'pubchem_primary_name'
            @alt_to_pubchem[drug_claim_alias] << dca
          else
            @alt_to_other[drug_claim_alias] << dca
          end
        end
      end

      def self.create_groups
        @alt_to_pubchem.each_key do |key|
          drug_claims = @alt_to_pubchem[key].map(&:drug_claim)
          drug = DataModel::Drug.where(name: key).first
          if drug 
            drug_claims.each do |drug_claim|
              drug_claim.drugs << drug unless drug_claim.drugs.include?(drug)
              drug_claim.save
            end
          else
{'fraction_non_alphanumeric': 0.09788443321755605, 'fraction_numerical': 0.0006315124723713293, 'mean_word_length': 1.7887323943661972, 'pattern_counts': {'":': 0, '<': 19, '<?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': '2537795', 'n_tokens_mistral': 1020, 'n_tokens_neox': 992, 'n_words': 195}