<commit_before>class Tasting < ActiveRecord::Base
  include Feedback

  belongs_to :user
  belongs_to :event_wine
  has_one :wine, through: :event_wine
  has_one :event, through: :event_wine

  enum red_fruits:      [:red, :blue, :black]
  enum white_fruits:    [:citrus, :stone, :tropical]
  enum fruit_condition: [:tart, :under_ripe, :ripe, :over_ripe, :jammy]
  enum climate:         [:cool, :warm]
  enum country:         [:france, :italy, :united_states, :australia, :argentina, \
                        :germany, :new_zealand]
  enum red_grape:       [:gamay, :cabernet_sauvignon, :merlot, :malbec, :syrah_shiraz, \
                        :pinot_noir, :sangiovese, :nebbiolo, :zinfandel]
  enum white_grape:     [:chardonnay, :sauvignon_blanc, :riesling, :chenin_blanc, \
                        :viognier, :pinot_grigio]    

  def get_super_tasting(grape, country)
    current_event_wine = User.first.event_wines.where(event: Event.first) 
    super_tastings = Tasting.where(event_wine: current_event_wine)
    current_wine = Wine.find_by(grape: grape, country: country)
    super_event_wine = EventWine.find_by(wine: current_wine) 
    super_tasting = super_tastings.find_by(event_wine: super_event_wine)
  end

  def score_report
    report = {}
    super_tasting = get_super_tasting(self.wine.grape, self.wine.country)
    guessed_tasting = get_guessed_tasting
    attributes = current_tasting_attributes
    user_results = {}
    correct_answers = {}
    user_conclusions = {}
    correct_conclusions = {}
    observation_dist = 0 
    conclusion_dist = 0
    observation_feedback = {}
    conclusion_feedback = []

    attributes.each do |attribute|
      formatted_attr = format_category(attribute)
      if conclusion_attr_hash[attribute]
        # score conclusions
        user_conclusions[formatted_attr] = format_category(self.send(attribute))
        correct_conclusions[formatted_attr] = format_category(super_tasting.send(attribute))
      else
        # score observations
        user_results[formatted_attr] = make_result_hash(attribute, self)
        correct_answers[formatted_attr] = make_result_hash(attribute, super_tasting)     
        if numerical_attributes[attribute]
          user_num = user_results[formatted_attr][:num_response]
          correct_num = correct_answers[formatted_attr][:num_response]
          dist = (correct_num - user_num) ** 2
          observation_dist += dist
          if dist > 1
            observation_feedback[formatted_attr] = get_feedback(formatted_attr)
          end
          # score conclusions based on observations
          conclusion_num = guessed_tasting[attribute]
          dist = (conclusion_num - user_num) ** 2
          conclusion_dist += dist
          if dist > 1 &&  !formatted_attr.match("Fruits")
            correct_response = convert_num_to_category(guessed_tasting.send(attribute))
            conclusion_feedback << { category: formatted_attr, correct_response: correct_response }
          end
        end
      end
    end

    report[:wine_bringer] = self.event_wine.wine_bringer.name_or_email
    report[:conclusion_score] = is_reasonable(conclusion_dist)
    report[:observation_score] = is_reasonable(observation_dist)
    report[:user_results] = user_results
    report[:correct_answers] = correct_answers
    report[:user_conclusions] = user_conclusions
    report[:correct_conclusions] = correct_conclusions
    report[:observation_feedback] = observation_feedback
    report[:conclusion_feedback] = conclusion_feedback

    return report
  end

  def conclusion_attr_hash
    {white_grape: 1, red_grape: 1, country: 1, climate: 1}
  end<commit_msg>Add conditional if no guessed_tasting fits conclusion<commit_after>class Tasting < ActiveRecord::Base
  include Feedback

  belongs_to :user
  belongs_to :event_wine
  has_one :wine, through: :event_wine
  has_one :event, through: :event_wine

  enum red_fruits:      [:red, :blue, :black]
  enum white_fruits:    [:citrus, :stone, :tropical]
  enum fruit_condition: [:tart, :under_ripe, :ripe, :over_ripe, :jammy]
  enum climate:         [:cool, :warm]
  enum country:         [:france, :italy, :united_states, :australia, :argentina, \
                        :germany, :new_zealand]
  enum red_grape:       [:gamay, :cabernet_sauvignon, :merlot, :malbec, :syrah_shiraz, \
                        :pinot_noir, :sangiovese, :nebbiolo, :zinfandel]
  enum white_grape:     [:chardonnay, :sauvignon_blanc, :riesling, :chenin_blanc, \
                        :viognier, :pinot_grigio]    

  def get_super_tasting(grape, country)
    current_event_wine = User.first.event_wines.where(event: Event.first) 
    super_tastings = Tasting.where(event_wine: current_event_wine)
    current_wine = Wine.find_by(grape: grape, country: country)
    super_event_wine = EventWine.find_by(wine: current_wine) 
    super_tasting = super_tastings.find_by(event_wine: super_event_wine)
  end

  def score_report
    report = {}
    super_tasting = get_super_tasting(self.wine.grape, self.wine.country)
    guessed_tasting = get_guessed_tasting
    attributes = current_tasting_attributes
    user_results = {}
    correct_answers = {}
    user_conclusions = {}
    correct_conclusions = {}
    observation_dist = 0 
    conclusion_dist = 0
    observation_feedback = {}
    conclusion_feedback = []

    attributes.each do |attribute|
      formatted_attr = format_category(attribute)
      if conclusion_attr_hash[attribute]
        # score conclusions
        user_conclusions[formatted_attr] = format_category(self.send(attribute))
        correct_conclusions[formatted_attr] = format_category(super_tasting.send(attribute))
      else
        # score observations
        user_results[formatted_attr] = make_result_hash(attribute, self)
        correct_answers[formatted_attr] = make_result_hash(attribute, super_tasting)     
        if numerical_attributes[attribute]
          user_num = user_results[formatted_attr][:num_response]
          correct_num = correct_answers[formatted_attr][:num_response]
          dist = (correct_num - user_num) ** 2
          observation_dist += dist
          if dist > 1
            observation_feedback[formatted_attr] = get_feedback(formatted_attr)
          end
          # score conclusions based on observations
          if guessed_tasting
            conclusion_num = guessed_tasting[attribute]
            dist = (conclusion_num - user_num) ** 2
            conclusion_dist += dist
            if dist > 1 &&  !formatted_attr.match("Fruits")
              correct_response = convert_num_to_category(guessed_tasting.send(attribute))
              conclusion_feedback << { category: formatted_attr, correct_response: correct_response }
            end
          end
        end
      end
    end

    report[:wine_bringer] = self.event_wine.wine_bringer.name_or_email
    report[:conclusion_score] = is_reasonable(conclusion_dist)
    report[:observation_score] = is_reasonable(observation_dist)
    report[:user_results] = user_results
    report[:correct_answers] = correct_answers
    report[:user_conclusions] = user_conclusions
    report[:correct_conclusions] = correct_conclusions
    report[:observation_feedback] = observation_feedback
    report[:conclusion_feedback] = conclusion_feedback

    return report
  end

  def conclusion_attr_hash
    {white_grape: 1, red_grape: 1, country: 1, climate: 1}
  end
{'fraction_non_alphanumeric': 0.08676848222732801, 'fraction_numerical': 0.0027030679821597515, 'mean_word_length': 3.3838862559241707, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '75332', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2287, 'n_words': 422}