UNPKG

botbuilder-dialogs-adaptive

Version:

Rule system for the Microsoft BotBuilder dialog system.

106 lines 5.07 kB
"use strict"; /** * @module botbuilder-dialogs-adaptive */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityRecognizer = void 0; const adaptiveRecognizer_1 = require("../adaptiveRecognizer"); const textEntity_1 = require("./textEntity"); /** * Entity recognizers base class. */ class EntityRecognizer extends adaptiveRecognizer_1.AdaptiveRecognizer { /** * To recognize intents and entities in a users utterance. * * @param {DialogContext} dialogContext Dialog Context. * @param {Partial<Activity>} activity Activity. * @param {object} _telemetryProperties Additional properties to be logged to telemetry with event. * @param {object} _telemetryMetrics Additional metrics to be logged to telemetry with event. * @returns {Promise<RecognizerResult>} Analysis of utterance. */ recognize(dialogContext, activity, _telemetryProperties, _telemetryMetrics) { var _a; return __awaiter(this, void 0, void 0, function* () { // Identify matched intents const text = (_a = activity.text) !== null && _a !== void 0 ? _a : ''; const recognizerResult = { text, intents: {}, }; if (!text) { // nothing to recognize, return empty result return recognizerResult; } const textEntity = new textEntity_1.TextEntity(text); textEntity.start = 0; textEntity.end = text.length; textEntity.score = 1.0; // add entities from regexRecognizer to the entities pool const entityPool = [textEntity]; // process entities using EntityRecognizer const newEntities = yield this.recognizeEntities(dialogContext, dialogContext.context.activity.text, dialogContext.context.activity.locale, entityPool); entityPool.push(...newEntities); // map entityPool of Entity objects => RecognizerResult entity format const entities = {}; entityPool .filter((e) => e !== textEntity) .forEach((entityResult) => { var _a, _b, _c; var _d, _e; const { type: entityType, text: entityText } = entityResult; // add value (_a = entities[_d = `${entityType}`]) !== null && _a !== void 0 ? _a : (entities[_d] = []); const value = entities[`${entityType}`]; if (Array.isArray(value)) { value.push(entityText); } // get/create $instance (_b = entities['$instance']) !== null && _b !== void 0 ? _b : (entities['$instance'] = {}); const instanceRoot = entities['$instance']; // add instanceData (_c = instanceRoot[_e = `${entityType}`]) !== null && _c !== void 0 ? _c : (instanceRoot[_e] = []); const instanceData = instanceRoot[`${entityType}`]; instanceData.push({ startIndex: entityResult.start, endIndex: entityResult.end, score: 1.0, text: entityResult.text, type: entityResult.type, resolution: entityResult.resolution, }); }); recognizerResult.entities = entities; return recognizerResult; }); } /** * Recognizes entities from an [Entity](xref:botbuilder-core.Entity) list. * * @param {DialogContext} _dialogContext The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation. * @param {string} _text Text to recognize. * @param {string} _locale Locale to use. * @param {Entity[]} _entities The [Entity](xref:botbuilder-core.Entity) list to be recognized. * @returns {Promise<Entity[]>} Recognized [Entity](xref:botbuilder-core.Entity) list. */ recognizeEntities(_dialogContext, _text, _locale, _entities) { return __awaiter(this, void 0, void 0, function* () { return []; }); } } exports.EntityRecognizer = EntityRecognizer; //# sourceMappingURL=entityRecognizer.js.map