def __init__(self, *args, **kwargs): super(LuisRecognizerTest, self).__init__(*args, **kwargs) self._mocked_results: RecognizerResult = RecognizerResult( intents={"Test": IntentScore(score=0.2), "Greeting": IntentScore(score=0.4)} ) self._empty_luis_response: Dict[str, object] = json.loads( '{ "query": null, "intents": [], "entities": [] }' )
def get_intents(luis_result: LuisResult) -> Dict[str, IntentScore]: if luis_result.intents is not None: return { LuisUtil.normalized_intent(i.intent): IntentScore(i.score or 0) for i in luis_result.intents } return { LuisUtil.normalized_intent(luis_result.top_scoring_intent.intent): IntentScore(luis_result.top_scoring_intent.score or 0) }
def top_intent(intents: Dict[Intent, dict]) -> TopIntent: max_intent = Intent.NONE_INTENT max_value = 0.0 for intent, value in intents: intent_score = IntentScore(value) if intent_score.score > max_value: max_intent, max_value = intent, intent_score.score return TopIntent(max_intent, max_value)
def _get_intents(self, luis_result): intents = {} if not luis_result["intents"]: return intents for intent in luis_result["intents"]: intents[self._normalize_name(intent)] = IntentScore( luis_result["intents"][intent]["score"] ) return intents
def top_intent(intents: Dict[Intent, dict]) -> TopIntent: """ This function determines the top intention of a user. """ max_intent = Intent.NONE_INTENT max_value = 0.0 for intent, value in intents: intent_score = IntentScore(value) if intent_score.score > max_value: max_intent, max_value = intent, intent_score.score return TopIntent(max_intent, max_value)
async def _recognize_internal( self, turn_context: TurnContext, telemetry_properties: Dict[str, str], telemetry_metrics: Dict[str, float], luis_prediction_options: Union[LuisPredictionOptions, LuisRecognizerOptionsV2, LuisRecognizerOptionsV3] = None, ) -> RecognizerResult: BotAssert.context_not_none(turn_context) if turn_context.activity.type != ActivityTypes.message: return None utterance: str = turn_context.activity.text if turn_context.activity is not None else None recognizer_result: RecognizerResult = None if luis_prediction_options: options = luis_prediction_options else: options = self._options if not utterance or utterance.isspace(): recognizer_result = RecognizerResult( text=utterance, intents={"": IntentScore(score=1.0)}, entities={}) else: luis_recognizer = self._build_recognizer(options) recognizer_result = await luis_recognizer.recognizer_internal( turn_context) # Log telemetry self.on_recognizer_result(recognizer_result, turn_context, telemetry_properties, telemetry_metrics) return recognizer_result
async def _recognize_internal( self, turn_context: TurnContext, telemetry_properties: Dict[str, str], telemetry_metrics: Dict[str, float], luis_prediction_options: LuisPredictionOptions = None, ) -> RecognizerResult: BotAssert.context_not_none(turn_context) if turn_context.activity.type != ActivityTypes.message: return None utterance: str = turn_context.activity.text if turn_context.activity is not None else None recognizer_result: RecognizerResult = None luis_result: LuisResult = None if luis_prediction_options: options = self._merge_options(luis_prediction_options) else: options = self._options if not utterance or utterance.isspace(): recognizer_result = RecognizerResult( text=utterance, intents={"": IntentScore(score=1.0)}, entities={}) else: luis_result = self._runtime.prediction.resolve( self._application.application_id, utterance, timezone_offset=options.timezone_offset, verbose=options.include_all_intents, staging=options.staging, spell_check=options.spell_check, bing_spell_check_subscription_key=options. bing_spell_check_subscription_key, log=options.log if options.log is not None else True, ) recognizer_result = RecognizerResult( text=utterance, altered_text=luis_result.altered_query, intents=LuisUtil.get_intents(luis_result), entities=LuisUtil.extract_entities_and_metadata( luis_result.entities, luis_result.composite_entities, options.include_instance_data if options.include_instance_data is not None else True, ), ) LuisUtil.add_properties(luis_result, recognizer_result) if self._include_api_results: recognizer_result.properties["luisResult"] = luis_result # Log telemetry self.on_recognizer_result(recognizer_result, turn_context, telemetry_properties, telemetry_metrics) await self._emit_trace_info(turn_context, luis_result, recognizer_result, options) return recognizer_result