Class: Stealth::Nlp::Clu::Result
- Inherits:
-
Result
- Object
- Result
- Stealth::Nlp::Clu::Result
- Defined in:
- lib/stealth/nlp/clu/result.rb
Constant Summary collapse
- ENTITY_MAP =
{ 'money' => :currency, 'number' => :number, 'email' => :email, 'percentage' => :percentage, 'geographyV2' => :geo, 'age' => :age, 'phonenumber' => :phone, 'ordinalV2' => :ordinal, 'url' => :url, 'dimension' => :dimension, 'temperature' => :temp, 'keyPhrase' => :key_phrase, 'personName' => :name, 'datetimeV2' => :datetime }
Instance Method Summary collapse
- #entities ⇒ Object
-
#initialize(result:) ⇒ Result
constructor
A new instance of Result.
- #intent ⇒ Object
- #intent_score ⇒ Object
-
#parsed_result ⇒ Object
learn.microsoft.com/en-gb/azure/ai-services/language-service/conversational-language-understanding/how-to/call-api?tabs=REST-APIs#test-the-model Sample JSON result: { “kind”: “ConversationResult”, “result”: { “query”: “Text1”, “prediction”: { “topIntent”: “intent1”, “projectKind”: “Conversation”, “intents”: [ { “category”: “intent1”, “confidenceScore”: 1 }, { “category”: “intent2”, “confidenceScore”: 0 }, { “category”: “intent3”, “confidenceScore”: 0 } ], “entities”: [ { “category”: “entity1”, “text”: “text1”, “offset”: 29, “length”: 12, “confidenceScore”: 1 } ] } } }.
- #raw_entities ⇒ Object
Constructor Details
#initialize(result:) ⇒ Result
Returns a new instance of Result.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/stealth/nlp/clu/result.rb', line 17 def initialize(result:) @result = result if result.status.success? Stealth::Logger.l( topic: :nlp, message: 'NLP lookup successful' ) parsed_result else Stealth::Logger.l( topic: :nlp, message: "NLP lookup FAILED: (#{result.status.code}) #{result.body.to_s}" ) end end |
Instance Method Details
#entities ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/stealth/nlp/clu/result.rb', line 90 def entities return {} if raw_entities.blank? _entities = {} raw_entities.each do |entity| category = entity["category"] mapped_category = ENTITY_MAP[category] || category values = case mapped_category.to_sym when :number, :percentage entity["resolutions"]&.map { |res| res["value"] } when :phone, :email, :url, :key_phrase, :name [entity["text"]] when :currency, :age, :dimension, :temp entity["resolutions"]&.map do |res| { "number" => res["value"], "units" => res["unit"] } end when :ordinal entity["resolutions"]&.map do |res| { "offset" => res["offset"].to_i, "relativeTo" => res["relativeTo"] } end when :geo [{ "value" => entity["text"], "type" => "location" }] when :datetime [entity] else # Fallback: return text if no resolution resolution_values = entity["resolutions"]&.map { |res| res["value"] } resolution_values.presence || [entity["text"]] end next if values.blank? _entities[mapped_category.to_sym] ||= [] _entities[mapped_category.to_sym].concat(values) end _entities end |
#intent ⇒ Object
74 75 76 |
# File 'lib/stealth/nlp/clu/result.rb', line 74 def intent top_intent&.to_sym end |
#intent_score ⇒ Object
78 79 80 81 82 83 |
# File 'lib/stealth/nlp/clu/result.rb', line 78 def intent_score parsed_result&. dig('result', 'prediction', 'intents')&. find { |intent| intent['category'] == top_intent }&. dig('confidenceScore') end |
#parsed_result ⇒ Object
learn.microsoft.com/en-gb/azure/ai-services/language-service/conversational-language-understanding/how-to/call-api?tabs=REST-APIs#test-the-model Sample JSON result: {
"kind": "ConversationResult",
"result": {
"query": "Text1",
"prediction": {
"topIntent": "intent1",
"projectKind": "Conversation",
"intents": [
{
"category": "intent1",
"confidenceScore": 1
},
{
"category": "intent2",
"confidenceScore": 0
},
{
"category": "intent3",
"confidenceScore": 0
}
],
"entities": [
{
"category": "entity1",
"text": "text1",
"offset": 29,
"length": 12,
"confidenceScore": 1
}
]
}
}
}
70 71 72 |
# File 'lib/stealth/nlp/clu/result.rb', line 70 def parsed_result @parsed_result ||= MultiJson.load(result.body.to_s) end |
#raw_entities ⇒ Object
85 86 87 |
# File 'lib/stealth/nlp/clu/result.rb', line 85 def raw_entities parsed_result&.dig('result', 'prediction', 'entities') end |