<commit_before>class Opinion < OurOhm
  
  # naming conventions as in the document describing the calculations
  # b = belief
  # d = disbeliefs
  # u = uncertain
  #
  # a = authority
  #
  # _r in redis
  
  #TODO: refactor
  attribute :b_r
  attribute :d_r
  attribute :u_r
  attribute :a_r
  
  def b
    self.b_r.to_f
  end
  def d
    self.d_r.to_f
  end
  def u
    self.u_r.to_f
  end
  def a
    self.a_r.to_f
  end
  def b=(val)
    self.b_r=val
  end
  def d=(val)
    self.d_r=val
  end
  def u=(val)
    self.u_r=val
  end
  def a=(val)
    self.a_r=val
  end
  
  alias :authority :a
  
  def take_values(other)
    self.a = other.a
    self.b = other.b
    self.d = other.d
    self.u = other.u
    save
  end
  
  def self.tuple(b,d,u,a=0)
    self.new(:b_r=>b,:d_r=>d,:u_r=>u,:a_r=>a)
  end

  def self.identity
    self.new(:b_r=>0,:d_r=>0,:u_r=>1,:a_r=>0)
  end

  def Opinion.for_type(type, authority=0)
    case type
    when :beliefs
      Opinion.new(:b=>1,:d=>0,:u=>0,:a=>authority)
    when :disbeliefs
      Opinion.new(:b=>0,:d=>1,:u=>0,:a=>authority)
    when :doubts
      Opinion.new(:b=>0,:d=>0,:u=>1,:a=>authority)
    end
  end
  
  # inefficient, but allows for quickly changing the + def
  def Opinion.combine(list)
    unless list.length > 0
      Opinion.identity
    else
      a = list.inject(Opinion.identity) { | result, element |  result + element }
    end
  end

  #CHANGE ALONG WITH + !!!!
  def weight
    return (self.b + self.d + self.u)*self.a
  end

  #CHANGE weight ALONG WITH + !!!
  def +(second)
    a = self.a + second.a
    
    if a == 0
      # No authority
      return Opinion.identity
    end
    
    b = (self.b*self.a + second.b*second.a)/a
    d = (self.d*self.a + second.d*second.a)/a
    u = (self.u*self.a + second.u*second.a)/a
    return Opinion.tuple(b,d,u,a)
  end

  
  #TODO : better name
  def dfa(fr,fl)
    result = self.discount_by(fr).discount_by(fl)

    result.a = [fr.a, fl.a].min
    return result
  end
  
  def ==(other)
    self.a == other.a and
      self.b == other.b and
      self.d == other.d and
      self.u == other.u
  end

  define_memoized_method :as_percentages do
    total = b + d + u

    l_believe_percentage = calc_percentage(total, b).round.to_i
    l_disbelieve_percentage = calc_percentage(total, d).round.to_i
    l_doubt_percentage = 100 - l_believe_percentage - l_disbelieve_percentage

    @percentage_hash = {
      :believe => {
        :percentage => l_believe_percentage
      },
      :disbelieve => {
        :percentage => l_disbelieve_percentage
      },
      :doubt => {
        :percentage => l_doubt_percentage
      },
      :authority => a.round.to_i
    }
  end

<commit_msg>reformatted<commit_after>class Opinion < OurOhm
  
  # naming conventions as in the document describing the calculations
  # b = belief
  # d = disbeliefs
  # u = uncertain
  #
  # a = authority
  #
  # _r in redis
  
  #TODO: refactor
  attribute :b_r
  attribute :d_r
  attribute :u_r
  attribute :a_r
  
  def b
    self.b_r.to_f
  end
  def d
    self.d_r.to_f
  end
  def u
    self.u_r.to_f
  end
  def a
    self.a_r.to_f
  end
  def b=(val)
    self.b_r=val
  end
  def d=(val)
    self.d_r=val
  end
  def u=(val)
    self.u_r=val
  end
  def a=(val)
    self.a_r=val
  end
  
  alias :authority :a
  
  def take_values(other)
    self.a = other.a
    self.b = other.b
    self.d = other.d
    self.u = other.u
    save
  end
  
  def self.tuple(b,d,u,a=0)
    self.new(:b_r=>b,:d_r=>d,:u_r=>u,:a_r=>a)
  end

  def self.identity
    self.new(:b_r=>0,:d_r=>0,:u_r=>1,:a_r=>0)
  end

  def Opinion.for_type(type, authority=0)
    case type
    when :beliefs
      Opinion.new(:b=>1,:d=>0,:u=>0,:a=>authority)
    when :disbeliefs
      Opinion.new(:b=>0,:d=>1,:u=>0,:a=>authority)
    when :doubts
      Opinion.new(:b=>0,:d=>0,:u=>1,:a=>authority)
    end
  end
  
  # inefficient, but allows for quickly changing the + def
  def Opinion.combine(list)
    unless list.length > 0
      Opinion.identity
    else
      a = list.inject(Opinion.identity) { | result, element |  result + element }
    end
  end

  #CHANGE ALONG WITH + !!!!
  def weight
    return (self.b + self.d + self.u)*self.a
  end

  #CHANGE weight ALONG WITH + !!!
  def +(second)
    a = self.a + second.a
    
    if a == 0
      # No authority
      return Opinion.identity
    end
    
    b = (self.b*self.a + second.b*second.a)/a
    d = (self.d*self.a + second.d*second.a)/a
    u = (self.u*self.a + second.u*second.a)/a
    return Opinion.tuple(b,d,u,a)
  end

  
  #TODO : better name
  def dfa(fr,fl)
    result = self.discount_by(fr).discount_by(fl)

    result.a = [fr.a, fl.a].min
    return result
  end
  
  def ==(other)
    self.a == other.a and
      self.b == other.b and
      self.d == other.d and
      self.u == other.u
  end

  define_memoized_method :as_percentages do
    total = b + d + u

    l_believe_percentage = calc_percentage(total, b).round.to_i
    l_disbelieve_percentage = calc_percentage(total, d).round.to_i
    l_doubt_percentage = 100 - l_believe_percentage - l_disbelieve_percentage

    @percentage_hash = {
      believe:    { percentage: l_believe_percentage },
      disbelieve: { percentage: l_disbelieve_percentage },
      doubt:      { percentage: l_doubt_percentage  },
      authority: a.round.to_i
    }
  end


{'fraction_non_alphanumeric': 0.1309880239520958, 'fraction_numerical': 0.0074850299401197605, 'mean_word_length': 2.338538413491568, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '2950386', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2174, 'n_words': 446}