<commit_before>class SearchCriteria < ActiveRecord::Base
  validates :name, presence: true, 
      length: { minimum: 1 }, 
      uniqueness: true

  has_many :criteria, dependent: :destroy
  belongs_to :scope, class_name: 'Group'
  belongs_to :user

  def formula
    composition
  end
end
<commit_msg>Humanizing formula on searches<commit_after>class SearchCriteria < ActiveRecord::Base
  validates :name, presence: true, 
      length: { minimum: 1 }, 
      uniqueness: true

  has_many :criteria, dependent: :destroy
  belongs_to :scope, class_name: 'Group'
  belongs_to :user

  def formula
    process_search_criteria composition
  end

  private
    def process_search_criteria element
      elements = split_closure element
      if elements.one?
        criterium = elements.first
        criterium = criterium[1..-2].split ":"
        if criterium.one? #first case
          " (#{SearchCriteria.find(criterium.first.to_i).name})"
        else #second case
          process_criterium criterium
        end
      else # elements.second == "AND" || elements.second == "OR" #third case
        " (" + process_search_criteria(elements.first) + " " + elements.second + " " +  process_search_criteria(elements.last) + ")"
      end
    end

    # split into smaller elements, delimited by "(" and ")"
    def split_closure element
      open = -1
      elements=[]
      element.each_char.with_index do |char, i|
        if char=="("
          open += 1
        elsif char==")"
          open -= 1
          if open == -1 #this happens when the element is unique
            elements = [element]
            break
          elsif open == 0
            if element[(i+1)..(i+2)]=="OR"
              elements = [element[1..i], "OR", element[(i+3)..-2]]
            else # element[(i+1)..(i+3)]=="and"
              elements = [element[1..i], "AND", element[(i+4)..-2]]
            end
            break
          end
        end
      end
      elements
    end

    #do the query
    def process_criterium split
      knowledge_area = KnowledgeArea.find(split.first)
      ability = Ability.find(split.second)
      competence_matrix = knowledge_area.competence_matrix
      value = Value.find_by(rank: split.fourth, competence_matrix_id: competence_matrix.id)
      if value.nil?
        value = Value.find_by(rank: split.fourth, ability_id: ability.id)
      end
      " (#{competence_matrix.name}:#{knowledge_area.name}:#{ability.name}:#{split.third}:#{value.value})"
    end
end

{'fraction_non_alphanumeric': 0.1166532582461786, 'fraction_numerical': 0.007642799678197908, 'mean_word_length': 2.416208791208791, 'pattern_counts': {'":': 1, '<': 5, '<?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': '7460290', 'n_tokens_mistral': 769, 'n_tokens_neox': 749, 'n_words': 181}