<commit_before>class Calculation < ActiveRecord::Base
  belongs_to :identity
  validates :name, presence: true
  
  belongs_to :calculation_form, dependent: :destroy, :autosave => true
  accepts_nested_attributes_for :calculation_form
  validates_presence_of :calculation_form

  attr_accessor :original_calculation_form_id

  validate do
    if !calculation_form.nil?
      calc = create_cal
      dependencies = calc.dependencies(calculation_form.equation)
      dependencies.each do |dependency|
        missing_input = calculation_form.calculation_inputs.find{|calculation_input| calculation_input.variable_name == dependency.to_s }
        if !missing_input.nil?
          errors.add(:answer, I18n.t("myplaceonline.calculations.input_missing", input_name: missing_input.input_name))
        end
      end
    end
  end
  
  def create_cal
    calc = Dentaku::Calculator.new
    calculation_form.calculation_inputs.each do |calculation_input|
      calc.store(calculation_input.variable_name => calculation_input.input_value.to_f)
    end
    calc
  end
  
  def evaluate
    create_cal.evaluate(calculation_form.equation).to_s
  end
end
<commit_msg>Only try to evaluate non-blank inputs<commit_after>class Calculation < ActiveRecord::Base
  belongs_to :identity
  validates :name, presence: true
  
  belongs_to :calculation_form, dependent: :destroy, :autosave => true
  accepts_nested_attributes_for :calculation_form
  validates_presence_of :calculation_form

  attr_accessor :original_calculation_form_id

  validate do
    if !calculation_form.nil?
      calc = create_cal
      dependencies = calc.dependencies(calculation_form.equation)
      dependencies.each do |dependency|
        missing_input = calculation_form.calculation_inputs.find{|calculation_input| calculation_input.variable_name == dependency.to_s }
        if !missing_input.nil?
          errors.add(:answer, I18n.t("myplaceonline.calculations.input_missing", input_name: missing_input.input_name))
        end
      end
    end
  end
  
  def create_cal
    calc = Dentaku::Calculator.new
    calculation_form.calculation_inputs.each do |calculation_input|
      if !calculation_input.input_value.blank?
        calc.store(calculation_input.variable_name => calculation_input.input_value.to_f)
      end
    end
    calc
  end
  
  def evaluate
    create_cal.evaluate(calculation_form.equation).to_s
  end
end

{'fraction_non_alphanumeric': 0.07151819322459223, 'fraction_numerical': 0.001672940192388122, 'mean_word_length': 4.067796610169491, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6311535', 'n_tokens_mistral': 763, 'n_tokens_neox': 725, 'n_words': 134}