<commit_before>class Boostificator
  MILLIS_IN_YEAR = 1000 * 60 * 60 * 24 * 365
  PRECISION = 6

  attr_accessor :search, :field

  attr_accessor :m, :x, :a, :b

  attr_accessor :now

  def initialize(options)
    options.reverse_merge!(:a => 1.0, :b => 1.0, :m => 1.0 / MILLIS_IN_YEAR, :now => 1.hour.since.change(:min => 0))
    options.each do |field, value|
      self.send("#{field}=", value)
    end
  end

  def recip_min
    sprintf("%f", (recip * 10**PRECISION).floor/10.0**PRECISION)
  end

  def recip_max
    sprintf("%f", (recip * 10**PRECISION).ceil/10.0**PRECISION)
  end

  def recip
    a / (m * now_ms + b)
  end

  def now_ms
    now.to_i * 1000
  end

  def now_s
    now.utc.to_datetime.to_time.iso8601
  end

  def adjust_solr_params
    search.adjust_solr_params do |params|
      params[:q] = "{!boost b=map(recip(ms(#{now_s},#{field}),#{m},#{a},#{b}),#{recip_min},#{recip_max},1) defType=dismax}#{params[:q]}" if params[:q]
    end
  end
end<commit_msg>date can be set in the future, so use abs for datedistance<commit_after>class Boostificator
  MILLIS_IN_YEAR = 1000 * 60 * 60 * 24 * 365
  PRECISION = 6

  attr_accessor :search, :field

  attr_accessor :m, :x, :a, :b

  def initialize(options={})
    options.reverse_merge!(:a => 1.0, :b => 1.0, :m => 1.0 / MILLIS_IN_YEAR)
    options.each do |field, value|
      self.send("#{field}=", value)
    end
  end


  def adjust_solr_params
    search.adjust_solr_params do |params|
      params[:q] = "{!boost b=map(recip(abs(ms(#{now_s},#{field})),#{m},#{a},#{b}),#{recip_min},#{recip_max},1) defType=dismax}#{params[:q]}" if params[:q]
    end
  end

  private
    def now
      @now ||= DateTime.now.change(:min => 0)
    end

    def recip
      @recip ||= a / (m * now_ms + b)
    end

    def now_ms
      @now_ms ||= now.to_i * 1000
    end

    def now_s
      @now_s ||= now.utc.to_datetime.to_time.iso8601
    end

    def recip_min
      @recip_min ||= sprintf("%f", (recip * 10**PRECISION).floor/10.0**PRECISION)
    end

    def recip_max
      @recip_max ||= sprintf("%f", (recip * 10**PRECISION).ceil/10.0**PRECISION)
    end

end
{'fraction_non_alphanumeric': 0.16886792452830188, 'fraction_numerical': 0.03820754716981132, 'mean_word_length': 3.1184466019417476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '1760872', 'n_tokens_mistral': 941, 'n_tokens_neox': 853, 'n_words': 181}