Class: WitQueryService::WitData

Inherits:
Object
  • Object
show all
Defined in:
src/services/wit_query_service.rb

Overview

Wit.ai response data

Instance Method Summary collapse

Instance Method Details

#intent(min_confidence = 0.6) ⇒ String | nil

Get the intent of this message

Examples:

data.intent #= 'hi'

Parameters:

  • min_confidence (Float) (defaults to: 0.6)

    The minimum confidence that is required. Number between 0 and 1.

Returns:

  • (String | nil)

    The intent or nil if it could not be extracted or is low-confidence.



66
67
68
# File 'src/services/wit_query_service.rb', line 66

def intent(min_confidence = 0.6)
  single(min_confidence).intent
end

#multi(min_confidence = 0.6) ⇒ Object

Get multiple entities from this Wit.ai query

Examples:

# The entity is called `name`
data.multi.name #=> ['Ivan', 'Georgi']

Parameters:

  • min_confidence (Float) (defaults to: 0.6)

    The minimum confidence that is required. Number between 0 and 1.

Returns:

  • (Object)

    An object with methods for each entity key, returning an array of values.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'src/services/wit_query_service.rb', line 26

def multi(min_confidence = 0.6)
  @multi[min_confidence] ||= begin
    return Hashie::Mash.new({}) unless @response.entities

    entities = @response.entities.map do |name, values|
      [
        name.to_sym,
        values
          .select { |v| v.confidence > min_confidence }
          .map(&:value)
      ]
    end.to_h

    Hashie::Mash.new(entities)
  end
end

#single(min_confidence = 0.6) ⇒ String | nil

Get a single (top-confidence) entity from this Wit.ai query

Examples:

# The entity is called `name`
data.single.name #=> 'Ivan'

Parameters:

  • min_confidence (Float) (defaults to: 0.6)

    The minimum confidence that is required. Number between 0 and 1.

Returns:

  • (String | nil)

    The entity value or nil if no entity with this name has been extracted or is with lower confidence.



53
54
55
56
# File 'src/services/wit_query_service.rb', line 53

def single(min_confidence = 0.6)
  @single[min_confidence] ||=
    Hashie::Mash.new(multi(min_confidence).transform_values(&:first))
end