<commit_before>module ActsAsFaceted

  module ClassMethods
  end

  module ActsMethods
    # Example:
    #
    #acts_as_faceted :fields => {
    #  :f_type => {:label => _('Type'), :proc => proc{|klass| f_type_proc(klass)}},
    #  :f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"), 
    #    '[NOW-1YEARS TO NOW/DAY]' => _("Last year"), '[NOW-1MONTHS TO NOW/DAY]' => _("Last month"), '[NOW-7DAYS TO NOW/DAY]' => _("Last week"), '[NOW-1DAYS TO NOW/DAY]' => _("Last day")}},
    #  :f_profile_type => {:label => _('Author'), :proc => proc{|klass| f_profile_type_proc(klass)}},
    #  :f_category => {:label => _('Categories')}},
    #  :order => [:f_type, :f_published_at, :f_profile_type, :f_category]
    #
    #acts_as_searchable :additional_fields => [ {:name => {:type => :string, :as => :name_sort, :boost => 5.0}} ] + facets_fields_for_solr,
    #  :exclude_fields => [:setting],
    #  :include => [:profile],
    #  :facets => facets_option_for_solr,
    #  :if => proc{|a| ! ['RssFeed'].include?(a.class.name)}
    def acts_as_faceted(options)
      extend ClassMethods
      extend ActsAsSolr::CommonMethods

      cattr_accessor :facets
      cattr_accessor :facets_order
      cattr_accessor :to_solr_fields_names
      cattr_accessor :facets_results_containers
      cattr_accessor :solr_fields_names
      cattr_accessor :facets_option_for_solr
      cattr_accessor :facets_fields_for_solr
      cattr_accessor :facet_category_query

      self.facets = options[:fields]
      self.facets_order = options[:order] || self.facets.keys
      self.facets_results_containers = {:fields => 'facet_fields', :queries => 'facet_queries', :ranges => 'facet_ranges'}
      self.facets_option_for_solr = Hash[facets.select{ |id,data| ! data.has_key?(:queries) }].keys
      self.facets_fields_for_solr = facets.map{ |id,data| {id => data[:type] || :facet} } 
      self.solr_fields_names = facets.map{ |id,data| id.to_s + '_' + get_solr_field_type(data[:type] || :facet) }
      self.facet_category_query = options[:category_query]

      # A hash to retrieve the field key for the solr facet string returned
      # :field_name => "field_name_facet"
      self.to_solr_fields_names = Hash[facets.keys.zip(solr_fields_names)]

      def facet_by_id(id)
        {:id => id}.merge(facets[id]) if facets[id]
      end

      def map_facets_for(environment)
        facets_order.map do |id|
          facet = facet_by_id(id)
          next if facet[:type_if] and !facet[:type_if].call(self.new)

          if facet[:multi]
            facet[:label].call(environment).map do |label_id, label|
              facet.merge({:id => facet[:id].to_s+'_'+label_id.to_s, :solr_field => facet[:id], :label_id => label_id, :label => label})
            end
          else
            facet.merge(:id => facet[:id].to_s, :solr_field => facet[:id])
          end
        end.compact.flatten
      end

      def map_facet_results(facet, facet_params, facets_data, unfiltered_facets_data = {}, options = {})
        raise 'Use map_facets_for before this method' if facet[:solr_field].nil?
        facets_data = {} if facets_data.blank? # could be empty array
        solr_facet = to_solr_fields_names[facet[:solr_field]]

        if facet[:queries]
          container = facets_data[facets_results_containers[:queries]]
          facet_data = (container.nil? or container.empty?) ? [] : container.select{ |k,v| k.starts_with? solr_facet }
          container = unfiltered_facets_data[facets_results_containers[:queries]]
          unfiltered_facet_data = (container.nil? or container.empty?) ? [] : container.select{ |k,v| k.starts_with? solr_facet }<commit_msg>Fix crash putting default variable value<commit_after>module ActsAsFaceted

  module ClassMethods
  end

  module ActsMethods
    # Example:
    #
    #acts_as_faceted :fields => {
    #  :f_type => {:label => _('Type'), :proc => proc{|klass| f_type_proc(klass)}},
    #  :f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"), 
    #    '[NOW-1YEARS TO NOW/DAY]' => _("Last year"), '[NOW-1MONTHS TO NOW/DAY]' => _("Last month"), '[NOW-7DAYS TO NOW/DAY]' => _("Last week"), '[NOW-1DAYS TO NOW/DAY]' => _("Last day")}},
    #  :f_profile_type => {:label => _('Author'), :proc => proc{|klass| f_profile_type_proc(klass)}},
    #  :f_category => {:label => _('Categories')}},
    #  :order => [:f_type, :f_published_at, :f_profile_type, :f_category]
    #
    #acts_as_searchable :additional_fields => [ {:name => {:type => :string, :as => :name_sort, :boost => 5.0}} ] + facets_fields_for_solr,
    #  :exclude_fields => [:setting],
    #  :include => [:profile],
    #  :facets => facets_option_for_solr,
    #  :if => proc{|a| ! ['RssFeed'].include?(a.class.name)}
    def acts_as_faceted(options)
      extend ClassMethods
      extend ActsAsSolr::CommonMethods

      cattr_accessor :facets
      cattr_accessor :facets_order
      cattr_accessor :to_solr_fields_names
      cattr_accessor :facets_results_containers
      cattr_accessor :solr_fields_names
      cattr_accessor :facets_option_for_solr
      cattr_accessor :facets_fields_for_solr
      cattr_accessor :facet_category_query

      self.facets = options[:fields]
      self.facets_order = options[:order] || self.facets.keys
      self.facets_results_containers = {:fields => 'facet_fields', :queries => 'facet_queries', :ranges => 'facet_ranges'}
      self.facets_option_for_solr = Hash[facets.select{ |id,data| ! data.has_key?(:queries) }].keys
      self.facets_fields_for_solr = facets.map{ |id,data| {id => data[:type] || :facet} } 
      self.solr_fields_names = facets.map{ |id,data| id.to_s + '_' + get_solr_field_type(data[:type] || :facet) }
      self.facet_category_query = options[:category_query]

      # A hash to retrieve the field key for the solr facet string returned
      # :field_name => "field_name_facet"
      self.to_solr_fields_names = Hash[facets.keys.zip(solr_fields_names)]

      def facet_by_id(id)
        {:id => id}.merge(facets[id]) if facets[id]
      end

      def map_facets_for(environment)
        facets_order.map do |id|
          facet = facet_by_id(id)
          next if facet[:type_if] and !facet[:type_if].call(self.new)

          if facet[:multi]
            facet[:label].call(environment).map do |label_id, label|
              facet.merge({:id => facet[:id].to_s+'_'+label_id.to_s, :solr_field => facet[:id], :label_id => label_id, :label => label})
            end
          else
            facet.merge(:id => facet[:id].to_s, :solr_field => facet[:id])
          end
        end.compact.flatten
      end

      def map_facet_results(facet, facet_params, facets_data, unfiltered_facets_data = {}, options = {})
        raise 'Use map_facets_for before this method' if facet[:solr_field].nil?
        facets_data = {} if facets_data.blank? # could be empty array
        solr_facet = to_solr_fields_names[facet[:solr_field]]
        unfiltered_facets_data ||= {}

        if facet[:queries]
          container = facets_data[facets_results_containers[:queries]]
          facet_data = (container.nil? or container.empty?) ? [] : container.select{ |k,v| k.starts_with? solr_facet }
          container = unfiltered_facets_data[facets_results_containers[:queries]]
          unfiltered_facet_data = (container.nil? or container.empty?) ? [] : container.select{ |k,v| k.starts_with? solr_facet }
{'fraction_non_alphanumeric': 0.14745379603776093, 'fraction_numerical': 0.0018614545938040155, 'mean_word_length': 3.4747174301011303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 83, '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': '1421958', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2611, 'n_words': 484}