class Aws::Lex::Conversation::Type::IntentConfidence

Public Instance Methods

ambiguous?(threshold: standard_deviation) click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 12
def ambiguous?(threshold: standard_deviation)
  candidates(threshold: threshold).size > 1
end
candidates(threshold: standard_deviation) click to toggle source

NOTE: by default this method looks for candidates with a confidence score within one standard deviation of the current intent. Confidence scores may not be normally distributed, so it's very possible that this method will return abnormal results for skewed sample sets.

If you want a consistent threshold for the condition, pass a static `threshold` parameter.

# File lib/aws/lex/conversation/type/intent_confidence.rb, line 28
def candidates(threshold: standard_deviation)
  intents = event.intents.select do |intent|
    diff = event.current_intent
      .nlu_confidence
      .to_f
      .-(intent.nlu_confidence.to_f)
      .abs

    diff <= threshold
  end

  # sort descending
  intents.sort do |a, b|
    b.nlu_confidence.to_f <=> a.nlu_confidence.to_f
  end
end
mean() click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 45
def mean
  @mean ||= calculate_mean
end
similar_alternates(threshold: standard_deviation) click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 49
def similar_alternates(threshold: standard_deviation)
  # remove the first element (current intent) from consideration
  candidates(threshold: threshold)[1..-1]
end
standard_deviation() click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 54
def standard_deviation
  @standard_deviation ||= calculate_standard_deviation
end
unambiguous?(threshold: standard_deviation) click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 16
def unambiguous?(threshold: standard_deviation)
  !ambiguous?(threshold: threshold)
end

Private Instance Methods

calculate_mean() click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 60
def calculate_mean
  sum = event.intents.sum { |i| i.nlu_confidence.to_f }
  sum / event.intents.size
end
calculate_standard_deviation() click to toggle source
# File lib/aws/lex/conversation/type/intent_confidence.rb, line 65
def calculate_standard_deviation
  normalized = event.intents.map do |intent|
    (intent.nlu_confidence.to_f - mean) ** 2
  end
  normalized_mean = normalized.sum / normalized.size
  Math.sqrt(normalized_mean)
end