<commit_before>class StopSearcher
  include ActiveModel::SerializerSupport
  DEFAULT_PER_PAGE = 20

  attr_reader :per_page, :page

  def initialize(params)
    @params = params
    @per_page = params.fetch(:per_page, DEFAULT_PER_PAGE).to_i
    @page = params.fetch(:page, 1).to_i
  end

  def results
    @results ||= paginated_results
  end

  def total_results
    filtered_results.count
  end

  def total_pages
    total_results / per_page
  end

  def offset
    per_page * (page - 1)
  end

  def valid?
    [
      @params[:query].present?,
      @params[:latitude].present? && @params[:longitude].present?,
    ].any?
  end

  private

  def paginated_results
    @paginated_results ||= filtered_results.offset(offset).limit(per_page)
  end

  def filtered_results
    scope = Stop.includes(:routes)

    if @params[:query]
      scope = scope.search(substituted_query)
    end

    if @params[:longitude] && @params[:latitude]
      scope = scope.by_distance(origin: [@params[:latitude], @params[:longitude]])
    end

    scope
  end

  def substituted_query
    @params[:query].gsub(/(#{QUERY_SUBSTITUTIONS.keys.join("|")})/, QUERY_SUBSTITUTIONS)
  end

  QUERY_SUBSTITUTIONS = {
    "and" => "&",
    "first" => "1st",
    "second" => "2nd",
    "thrid" => "3rd",
    "fourth" => "4th",
    "fifth" => "5th",
    "sixth" => "6th",
    "seventh" => "7th",
    "eighth" => "8th",
    "ninth" => "9th",
    "tenth" => "10th",
    "eleventh" => "11th",
    "twelfth" => "12th",
  }
end<commit_msg>Add query substitutions for st, ave, ln etc.<commit_after>class StopSearcher
  include ActiveModel::SerializerSupport
  DEFAULT_PER_PAGE = 20

  attr_reader :per_page, :page

  def initialize(params)
    @params = params
    @per_page = params.fetch(:per_page, DEFAULT_PER_PAGE).to_i
    @page = params.fetch(:page, 1).to_i
  end

  def results
    @results ||= paginated_results
  end

  def total_results
    filtered_results.count
  end

  def total_pages
    total_results / per_page
  end

  def offset
    per_page * (page - 1)
  end

  def valid?
    [
      @params[:query].present?,
      @params[:latitude].present? && @params[:longitude].present?,
    ].any?
  end

  private

  def paginated_results
    @paginated_results ||= filtered_results.offset(offset).limit(per_page)
  end

  def filtered_results
    scope = Stop.includes(:routes)

    if @params[:query]
      scope = scope.search(substituted_query)
    end

    if @params[:longitude] && @params[:latitude]
      scope = scope.by_distance(origin: [@params[:latitude], @params[:longitude]])
    end

    scope
  end

  def substituted_query
    @params[:query].gsub(/(#{QUERY_SUBSTITUTIONS.keys.join("|")})/, QUERY_SUBSTITUTIONS)
  end

  QUERY_SUBSTITUTIONS = {
    "and" => "&",
    "street" => "st",
    "road" => "rd",
    "lane" => "ln",
    "avenue" => "ave",
    "boulevard" => "blvd",
    "first" => "1st",
    "second" => "2nd",
    "thrid" => "3rd",
    "fourth" => "4th",
    "fifth" => "5th",
    "sixth" => "6th",
    "seventh" => "7th",
    "eighth" => "8th",
    "ninth" => "9th",
    "tenth" => "10th",
    "eleventh" => "11th",
    "twelfth" => "12th",
  }
end
{'fraction_non_alphanumeric': 0.1591125198098257, 'fraction_numerical': 0.012044374009508717, 'mean_word_length': 3.0987012987012985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, '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': '785798', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1134, 'n_words': 224}