Class: Typesafe::ScoreAnswer

Inherits:
Answer
  • Object
show all
Defined in:
lib/typesafe/score_answer.rb

Overview

The answer to a Score question: the probability-weighted value across the levels (which can land between two levels), the legend mapping each level back to its description, the probability distribution across levels, and a confidence between 0 and 1.

Typesafe::ScoreAnswer.new(
score: 1.6,
legend: { "1" => "Calm", "2" => "Frustrated", "3" => "Very angry" },
probabilities: { "1" => 0.6, "2" => 0.35, "3" => 0.05 },
confidence: 0.7
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Answer

#==, #hash, #to_json

Constructor Details

#initialize(score:, legend:, probabilities:, confidence:) ⇒ ScoreAnswer

Returns a new instance of ScoreAnswer.

Parameters:

  • score (Numeric)

    the probability-weighted answer across levels.

  • legend (Hash)

    level keys (String or Symbol) mapped to non-empty String descriptions.

  • probabilities (Hash)

    the same level keys mapped to probabilities in 0..1; the values must sum to 1 and the keys must match legend.

  • confidence (Numeric)

    the model's confidence, between 0 and 1.

Raises:

  • (ArgumentError)

    if any attribute is invalid.



37
38
39
40
41
42
43
# File 'lib/typesafe/score_answer.rb', line 37

def initialize(score:, legend:, probabilities:, confidence:)
  @score = validate_number(score, "score")
  @legend = normalize_legend(legend)
  @probabilities = normalize_probabilities(probabilities, allowed_keys: @legend.keys)
  @confidence = validate_probability(confidence, "confidence")
  freeze
end

Instance Attribute Details

#confidenceNumeric (readonly)

Returns how certain the model is, between 0 and 1.

Returns:

  • (Numeric)

    how certain the model is, between 0 and 1.



28
29
30
# File 'lib/typesafe/score_answer.rb', line 28

def confidence
  @confidence
end

#legendHash (readonly)

Returns each level number mapped back to its description.

Returns:

  • (Hash)

    each level number mapped back to its description.



21
22
23
# File 'lib/typesafe/score_answer.rb', line 21

def legend
  @legend
end

#probabilitiesHash (readonly)

Returns each level mapped to its probability; keys match the legend.

Returns:

  • (Hash)

    each level mapped to its probability; keys match the legend.



25
26
27
# File 'lib/typesafe/score_answer.rb', line 25

def probabilities
  @probabilities
end

#scoreNumeric (readonly)

Returns the probability-weighted answer across the levels; can land between levels.

Returns:

  • (Numeric)

    the probability-weighted answer across the levels; can land between levels.



18
19
20
# File 'lib/typesafe/score_answer.rb', line 18

def score
  @score
end

Class Method Details

.from_h(hash) ⇒ ScoreAnswer

Parameters:

  • hash (Hash)

    a score answer entry from the API response, with String or Symbol keys.

Returns:



48
49
50
51
52
53
54
55
# File 'lib/typesafe/score_answer.rb', line 48

def self.from_h(hash)
  new(
    score: hash["score"] || hash[:score],
    legend: hash["legend"] || hash[:legend],
    probabilities: hash["probabilities"] || hash[:probabilities],
    confidence: hash["confidence"] || hash[:confidence]
  )
end

Instance Method Details

#to_hObject



61
62
63
# File 'lib/typesafe/score_answer.rb', line 61

def to_h
  { type: type, score: score, legend: legend, probabilities: probabilities, confidence: confidence }
end

#typeObject



57
58
59
# File 'lib/typesafe/score_answer.rb', line 57

def type
  "score"
end