botbuilder-dialogs
Version:
A dialog stack based conversation manager for Microsoft BotBuilder.
138 lines • 6.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Recognizer = void 0;
/**
* @module botbuilder-dialogs-adaptive
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const botbuilder_core_1 = require("botbuilder-core");
const configurable_1 = require("./configurable");
const dialogTurnStateConstants_1 = require("./dialogTurnStateConstants");
const omit = require("lodash/omit");
/**
* Recognizer base class.
*/
class Recognizer extends configurable_1.Configurable {
constructor() {
super(...arguments);
/**
* The telemetry client for logging events.
* Default this to the NullTelemetryClient, which does nothing.
*/
this.telemetryClient = new botbuilder_core_1.NullTelemetryClient();
}
/**
* To recognize intents and entities in a users utterance.
*
* @param {DialogContext} _dialogContext Dialog Context.
* @param {Partial<Activity>} _activity Activity.
* @param {Record<string, string>} _telemetryProperties Additional properties to be logged to telemetry with event.
* @param {Record<string, number>} _telemetryMetrics Additional metrics to be logged to telemetry with event.
*/
recognize(_dialogContext, _activity, _telemetryProperties, _telemetryMetrics) {
throw new Error('Please implement recognize function.');
}
/**
* Creates choose intent result in the case that there are conflicting or ambiguous signals from the recognizers.
*
* @param {Record<string, RecognizerResult>} recognizerResults A group of recognizer results.
* @returns {RecognizerResult} Recognizer result which is ChooseIntent.
*/
createChooseIntentResult(recognizerResults) {
let text;
let sentiment = null;
const candidates = Object.entries(recognizerResults).reduce((candidates, [key, result]) => {
text = result.text;
sentiment = result.sentiment;
const { intent, score } = botbuilder_core_1.getTopScoringIntent(result);
if (intent !== 'None') {
candidates.push({
id: key,
intent,
score,
result,
});
}
return candidates;
}, []);
if (candidates.length) {
// return `ChooseIntent` with candidates array.
const recognizerResult = {
text,
intents: { ChooseIntent: { score: 1.0 } },
candidates,
entities: {},
};
return recognizerResult;
}
// just return a `None` intent.
const recognizerResult = {
text,
intents: { None: { score: 1.0 } },
entities: {},
sentiment: sentiment,
};
return recognizerResult;
}
/**
* Uses the RecognizerResult to create a list of properties to be included when tracking the result in telemetry.
*
* @param {RecognizerResult} recognizerResult Recognizer Result.
* @param {Record<string, string>} telemetryProperties A list of properties to append or override the properties created using the RecognizerResult.
* @param {DialogContext} _dialogContext Dialog Context.
* @returns {Record<string, string>} A collection of properties that can be included when calling the TrackEvent method on the TelemetryClient.
*/
fillRecognizerResultTelemetryProperties(recognizerResult, telemetryProperties, _dialogContext) {
const { intent, score } = botbuilder_core_1.getTopScoringIntent(recognizerResult);
const intents = Object.entries(recognizerResult.intents);
const properties = {
Text: recognizerResult.text,
AlteredText: recognizerResult.alteredText,
TopIntent: intents.length > 0 ? intent : undefined,
TopIntentScore: intents.length > 0 ? score.toString() : undefined,
Intents: intents.length > 0 ? JSON.stringify(recognizerResult.intents) : undefined,
Entities: recognizerResult.entities ? JSON.stringify(recognizerResult.entities) : undefined,
AdditionalProperties: JSON.stringify(omit(recognizerResult, ['text', 'alteredText', 'intents', 'entities'])),
};
// Additional Properties can override "stock" properties.
if (telemetryProperties) {
return Object.assign({}, properties, telemetryProperties);
}
return properties;
}
stringifyAdditionalPropertiesOfRecognizerResult(recognizerResult) {
const generalProperties = new Set(['text', 'alteredText', 'intents', 'entities']);
const additionalProperties = {};
for (const key in recognizerResult) {
if (!generalProperties.has(key)) {
additionalProperties[key] = recognizerResult[key];
}
}
return Object.keys(additionalProperties).length > 0 ? JSON.stringify(additionalProperties) : undefined;
}
/**
* Tracks an event with the event name provided using the TelemetryClient attaching the properties/metrics.
*
* @param {DialogContext} dialogContext Dialog context.
* @param {string} eventName The name of the event to track.
* @param {Record<string, string>} telemetryProperties The properties to be included as part of the event tracking.
* @param {Record<string, number>} telemetryMetrics The metrics to be included as part of the event tracking.
*/
trackRecognizerResult(dialogContext, eventName, telemetryProperties, telemetryMetrics) {
var _a;
if (this.telemetryClient instanceof botbuilder_core_1.NullTelemetryClient) {
const turnStateTelemetryClient = (_a = dialogContext.context.turnState.get(dialogTurnStateConstants_1.DialogTurnStateConstants.telemetryClient)) !== null && _a !== void 0 ? _a : dialogContext.context.turnState.get(botbuilder_core_1.BotTelemetryClientKey);
this.telemetryClient = turnStateTelemetryClient !== null && turnStateTelemetryClient !== void 0 ? turnStateTelemetryClient : this.telemetryClient;
}
this.telemetryClient.trackEvent({
name: eventName,
properties: telemetryProperties,
metrics: telemetryMetrics,
});
}
}
exports.Recognizer = Recognizer;
//# sourceMappingURL=recognizer.js.map
;