<commit_before>module BooksHelper
  def facet_list(response, field)
    list = []
    response.facets.each do |facet|
      if facet.name == field
        facet.items.each do |item|
          list << { :name => item.value, :count => item.hits } if (item.hits) > 0
        end
        break
      end
    end
    list
  end

  # TODO: not updated: will be updated after finishing books search function
  def add_facet_search(params, type, field)
    tmp_params = params.clone
    unless type =="sort_type"
      if tmp_params.has_key?("_#{type}".to_sym)
        tmp_params["_#{type}".to_sym] = field + " _OR " + tmp_params["_#{type}".to_sym]
      else
        tmp_params["_#{type}".to_sym] = field
      end
       tmp_params[:sort_type] = nil
    else
      tmp_params[:sort_type] = field
    end
    tmp_params[:controller] = tmp_params[:action] = tmp_params[:page] = tmp_params[:locale] = nil
    tmp_params
  end

  def search_volumes(query, page, limit, sort_type, fquery = nil)
    response = search_facet_highlight(query, page, limit, sort_type, fquery)
    
    process_solr_volumes(response, query, page, limit, fquery)
  end

  def fill_response_array(arr)
    arr.slice 0, MAX_NAMES_PER_BOOK
  end

  def get_names_info(sci_names)
   sci_names_info = []
    sci_names.each do |sci_name|
      info = get_name_info(sci_name)
     sci_names_info <<  info unless info.nil?
    end
    sci_names_info
  end

  def name_tip (job_id, string, eol_thumb, eol_page_id)
    title_tip = ''
    if eol_thumb
      thumb = ATTACHMENTS_URL + THUMB_FOLDER + eol_thumb
      title_tip = "<div style='float:left'>
                    <img style='height:100px' src='#{thumb}'></img>
                   </div>"
    end
    title_tip += "<div style='float:left'>
                    <span >#{string}</span>
                    <ul>
                      <li><a href='../books/#{job_id}?tab=read'>
                        #{I18n.t('common.find_in_book')}
                      </a></li>"
    if eol_page_id != nil && eol_page_id > -1
      title_tip += "<li><a href='http://eol.org/pages/#{eol_page_id}'>
                          #{I18n.t('common.view_in_eol')}
                        </a></li>"
    end
    title_tip += "<li><a href='../books?_name=#{string}'>
                        #{I18n.t('common.books_with_name')}
                      </a></li>
                    </ul>
                  </div>"
    title_tip
  end

  def book_names_more_open_div(vol_jobid, sci_names_count)
    more_names = ''
    if sci_names_count > MAX_NAMES_PER_BOOK
      count =
      more_names = I18n.t('common.and_count_more', count: (sci_names_count - MAX_NAMES_PER_BOOK))
    end
    "<a href = '#' data-toggle='modal' data-target ='#morenamesdiv_#{vol_jobid}'>#{more_names}</a>".html_safe
  end

  def fill_query_array(params)
    search_params = params.select { |key, value| ["_title", "_subject", "_language", "_author", "_name", "_location", "_publisher", "_content", "_all"].include?(key) }
    query_array = { 'all' => [], 'title'=> [], 'language'=> [],
                    'location'=> [], 'author'=> [], 'name'=> [],
                    'subject'=> [], 'content' => [], 'publisher' => [] }
    query_array.each do  |key, value|
      query_array[key] = search_params["_#{key}"] ? search_params["_#{key}"].split(' _OR ') : []
    end
    query_array
  end


  def set_query_string(query_array, conjunction = nil)
    emptyQuery = is_empty_search?(query_array)
    if(emptyQuery)
      query = "*:*"
    else
      multilingual_attributes = get_multilingual_attributes(query_array)
      normal_attributes = get_normal_attributes(query_array)
      unless conjunction
        if !(query_array['all'].empty?)
          conjunction ="OR"
        else
          conjunction = "AND"
        end
      end
      query = prepare_search_query(multilingual_attributes, normal_attributes, conjunction)
    end
    query
  end

  def is_empty_search?(query_array)
    emptyQuery = true
    query_array.each do |key, value|
      unless key == "name"
        if value.count > 0
          emptyQuery = false
          break
        end
      end
    end
    emptyQuery
  end
  
  def set_fquery_string(query_array)
    sci_names = query_array['name'].empty? ? (query_array['all'].empty? ? nil : query_array['all']) : query_array['name']      
    unless sci_names.blank?
      exact_sci_names = get_exact_sci_names(sci_names)
      unless exact_sci_names.blank?
        values = exact_sci_names.join(" OR ")
        field_query = "(sci_name:#{values})"
        return "{!join from=job_id to=job_id fromIndex=names_found} #{field_query}"
      end
    end
    return "*:*"
  end

  def prepare_search_query(multilingual_attributes_and_values, normal_attributes_and_values, query_join_operator)
    attributes = []
    languages = ["en", "ge", "ar", "fr", "it", "ud"]
    multilingual_query = ''
    normal_query = ''

    unless multilingual_attributes_and_values.empty?
      multilingual_attributes_and_values.each do |key, value_arr|
        field_query = '('
        tmp_array = []
        value_arr.each do |item|
          tmp_array << "\"" + item + "\""<commit_msg>fix names search<commit_after>module BooksHelper
  def facet_list(response, field)
    list = []
    response.facets.each do |facet|
      if facet.name == field
        facet.items.each do |item|
          list << { :name => item.value, :count => item.hits } if (item.hits) > 0
        end
        break
      end
    end
    list
  end

  # TODO: not updated: will be updated after finishing books search function
  def add_facet_search(params, type, field)
    tmp_params = params.clone
    unless type =="sort_type"
      if tmp_params.has_key?("_#{type}".to_sym)
        tmp_params["_#{type}".to_sym] = field + " _OR " + tmp_params["_#{type}".to_sym]
      else
        tmp_params["_#{type}".to_sym] = field
      end
       tmp_params[:sort_type] = nil
    else
      tmp_params[:sort_type] = field
    end
    tmp_params[:controller] = tmp_params[:action] = tmp_params[:page] = tmp_params[:locale] = nil
    tmp_params
  end

  def search_volumes(query, page, limit, sort_type, fquery = nil)
    response = search_facet_highlight(query, page, limit, sort_type, fquery)
    
    process_solr_volumes(response, query, page, limit, fquery)
  end

  def fill_response_array(arr)
    arr.slice 0, MAX_NAMES_PER_BOOK
  end

  def get_names_info(sci_names)
   sci_names_info = []
    sci_names.each do |sci_name|
      info = get_name_info(sci_name)
     sci_names_info <<  info unless info.nil?
    end
    sci_names_info
  end

  def name_tip (job_id, string, eol_thumb, eol_page_id)
    title_tip = ''
    if eol_thumb
      thumb = ATTACHMENTS_URL + THUMB_FOLDER + eol_thumb
      title_tip = "<div style='float:left'>
                    <img style='height:100px' src='#{thumb}'></img>
                   </div>"
    end
    title_tip += "<div style='float:left'>
                    <span >#{string}</span>
                    <ul>
                      <li><a href='../books/#{job_id}?tab=read'>
                        #{I18n.t('common.find_in_book')}
                      </a></li>"
    if eol_page_id != nil && eol_page_id > -1
      title_tip += "<li><a href='http://eol.org/pages/#{eol_page_id}'>
                          #{I18n.t('common.view_in_eol')}
                        </a></li>"
    end
    title_tip += "<li><a href='../books?_name=#{string}'>
                        #{I18n.t('common.books_with_name')}
                      </a></li>
                    </ul>
                  </div>"
    title_tip
  end

  def book_names_more_open_div(vol_jobid, sci_names_count)
    more_names = ''
    if sci_names_count > MAX_NAMES_PER_BOOK
      count =
      more_names = I18n.t('common.and_count_more', count: (sci_names_count - MAX_NAMES_PER_BOOK))
    end
    "<a href = '#' data-toggle='modal' data-target ='#morenamesdiv_#{vol_jobid}'>#{more_names}</a>".html_safe
  end

  def fill_query_array(params)
    search_params = params.select { |key, value| ["_title", "_subject", "_language", "_author", "_name", "_location", "_publisher", "_content", "_all"].include?(key) }
    query_array = { 'all' => [], 'title'=> [], 'language'=> [],
                    'location'=> [], 'author'=> [], 'name'=> [],
                    'subject'=> [], 'content' => [], 'publisher' => [] }
    query_array.each do  |key, value|
      query_array[key] = search_params["_#{key}"] ? search_params["_#{key}"].split(' _OR ') : []
    end
    query_array
  end


  def set_query_string(query_array, conjunction = nil)
    emptyQuery = is_empty_search?(query_array)
    if(emptyQuery)
      query = "*:*"
    else
      multilingual_attributes = get_multilingual_attributes(query_array)
      normal_attributes = get_normal_attributes(query_array)
      unless conjunction
        if !(query_array['all'].empty?)
          conjunction ="OR"
        else
          conjunction = "AND"
        end
      end
      query = prepare_search_query(multilingual_attributes, normal_attributes, conjunction)
    end
    query
  end

  def is_empty_search?(query_array)
    emptyQuery = true
    query_array.each do |key, value|
      unless key == "name"
        if value.count > 0
          emptyQuery = false
          break
        end
      end
    end
    emptyQuery
  end
  
  def set_fquery_string(query_array)
    sci_names = query_array['name'].empty? ? (query_array['all'].empty? ? nil : query_array['all']) : query_array['name']      
    unless sci_names.blank?
      exact_sci_names = get_exact_sci_names(sci_names)
      if exact_sci_names.blank?
        values = sci_names.join(" OR ")
      else
        values = exact_sci_names.join(" OR ")
      end
      field_query = "(sci_name:#{values})"
      return "{!join from=job_id to=job_id fromIndex=names_found} #{field_query}"
    end
    return "*:*"
  end

  def prepare_search_query(multilingual_attributes_and_values, normal_attributes_and_values, query_join_operator)
    attributes = []
    languages = ["en", "ge", "ar", "fr", "it", "ud"]
    multilingual_query = ''
    normal_query = ''

    unless multilingual_attributes_and_values.empty?
      multilingual_attributes_and_values.each do |key, value_arr|
        field_query = '('
        tmp_array = []
        value_arr.each do |item|
          tmp_array << "\"" + item + "\""
{'fraction_non_alphanumeric': 0.13423597678916827, 'fraction_numerical': 0.0029013539651837525, 'mean_word_length': 2.512567934782609, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 81, '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': '5740165', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3383, 'n_words': 662}