Class: Typesafe::ScoreAnswer
- 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
-
#confidence ⇒ Numeric
readonly
How certain the model is, between 0 and 1.
-
#legend ⇒ Hash
readonly
Each level number mapped back to its description.
-
#probabilities ⇒ Hash
readonly
Each level mapped to its probability; keys match the legend.
-
#score ⇒ Numeric
readonly
The probability-weighted answer across the levels; can land between levels.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(score:, legend:, probabilities:, confidence:) ⇒ ScoreAnswer
constructor
A new instance of ScoreAnswer.
- #to_h ⇒ Object
- #type ⇒ Object
Methods inherited from Answer
Constructor Details
#initialize(score:, legend:, probabilities:, confidence:) ⇒ ScoreAnswer
Returns a new instance of ScoreAnswer.
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
#confidence ⇒ Numeric (readonly)
Returns how certain the model is, between 0 and 1.
28 29 30 |
# File 'lib/typesafe/score_answer.rb', line 28 def confidence @confidence end |
#legend ⇒ Hash (readonly)
Returns each level number mapped back to its description.
21 22 23 |
# File 'lib/typesafe/score_answer.rb', line 21 def legend @legend end |
#probabilities ⇒ Hash (readonly)
Returns 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 |
#score ⇒ Numeric (readonly)
Returns 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
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_h ⇒ Object
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 |
#type ⇒ Object
57 58 59 |
# File 'lib/typesafe/score_answer.rb', line 57 def type "score" end |