UNPKG

clwoz-models

Version:
425 lines 19.3 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var TrainDialog_1 = require("./TrainDialog"); var ModelUtils = /** @class */ (function () { function ModelUtils() { } ModelUtils.generateGUID = function () { var d = new Date().getTime(); var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) { var r = ((d + Math.random() * 16) % 16) | 0; d = Math.floor(d / 16); return (char === 'x' ? r : (r & 0x3) | 0x8).toString(16); }); return guid; }; /** Remove n words from start of string */ ModelUtils.RemoveWords = function (text, numWords) { if (text.length === 0 || numWords === 0) { return text; } var firstSpace = text.indexOf(' '); var remaining = firstSpace > 0 ? text.slice(firstSpace + 1) : ''; numWords--; return this.RemoveWords(remaining, numWords); }; //==================================================================== // CONVERSION: LabeledEntity == PredictedEntity //==================================================================== ModelUtils.ToLabeledEntity = function (predictedEntity) { var score = predictedEntity.score, labeledEntity = __rest(predictedEntity, ["score"]); return labeledEntity; }; ModelUtils.ToLabeledEntities = function (predictedEntities) { var labeledEntities = []; for (var _i = 0, predictedEntities_1 = predictedEntities; _i < predictedEntities_1.length; _i++) { var predictedEntity = predictedEntities_1[_i]; var labelEntity = ModelUtils.ToLabeledEntity(predictedEntity); labeledEntities.push(labelEntity); } return labeledEntities; }; ModelUtils.ToPredictedEntity = function (labeledEntity) { return __assign(__assign({}, labeledEntity), { score: undefined }); }; ModelUtils.ToPredictedEntities = function (labeledEntities) { var predictedEntities = []; for (var _i = 0, labeledEntities_1 = labeledEntities; _i < labeledEntities_1.length; _i++) { var labeledEntity = labeledEntities_1[_i]; var predictedEntity = ModelUtils.ToPredictedEntity(labeledEntity); predictedEntities.push(predictedEntity); } return predictedEntities; }; //==================================================================== // CONVERSION: ExtractResponse == TextVariation //==================================================================== ModelUtils.ToTextVariation = function (extractResponse) { var labeledEntities = this.ToLabeledEntities(extractResponse.predictedEntities); var textVariation = { text: extractResponse.text, labelEntities: labeledEntities }; return textVariation; }; ModelUtils.ToExtractResponse = function (textVariation) { var predictedEntities = this.ToPredictedEntities(textVariation.labelEntities); var extractResponse = { definitions: { entities: [], actions: [], trainDialogs: [] }, packageId: '', metrics: { wallTime: 0 }, text: textVariation.text, predictedEntities: predictedEntities }; return extractResponse; }; ModelUtils.ToExtractResponses = function (textVariations) { var extractResponses = []; for (var _i = 0, textVariations_1 = textVariations; _i < textVariations_1.length; _i++) { var textVariation = textVariations_1[_i]; var predictedEntities = this.ToPredictedEntities(textVariation.labelEntities); var extractResponse = { definitions: { entities: [], actions: [], trainDialogs: [] }, packageId: '', metrics: { wallTime: 0 }, text: textVariation.text, predictedEntities: predictedEntities }; extractResponses.push(extractResponse); } return extractResponses; }; ModelUtils.ToTextVariations = function (extractResponses) { var textVariations = []; for (var _i = 0, extractResponses_1 = extractResponses; _i < extractResponses_1.length; _i++) { var extractResponse = extractResponses_1[_i]; var labelEntities = this.ToLabeledEntities(extractResponse.predictedEntities); var textVariation = { text: extractResponse.text, labelEntities: labelEntities }; textVariations.push(textVariation); } return textVariations; }; //==================================================================== // CONVERSION: LogDialog == TrainDialog //==================================================================== ModelUtils.ToTrainDialog = function (logDialog, actions, entities) { if (actions === void 0) { actions = null; } if (entities === void 0) { entities = null; } var trainRounds = []; for (var _i = 0, _a = logDialog.rounds; _i < _a.length; _i++) { var logRound = _a[_i]; var trainRound = ModelUtils.ToTrainRound(logRound); trainRounds.push(trainRound); } var appDefinition = null; if (entities !== null && actions !== null) { appDefinition = { entities: entities, actions: actions, trainDialogs: [] }; } // Update initialFilledEntity list to include those that were saved in onEndSession var initialFilledEntities = []; if (trainRounds.length !== 0 && trainRounds[0].scorerSteps.length !== 0) { // Get entities extracted on first input var firstEntityIds_1 = trainRounds[0].extractorStep.textVariations[0] ? trainRounds[0].extractorStep.textVariations[0].labelEntities.map(function (le) { return le.entityId; }) : []; // Intial entities are ones on first round that weren't extracted on the first utterance initialFilledEntities = trainRounds[0].scorerSteps[0].input.filledEntities .filter(function (fe) { return !firstEntityIds_1.includes(fe.entityId); }); } return { createdDateTime: logDialog.createdDateTime, lastModifiedDateTime: logDialog.lastModifiedDateTime, packageCreationId: 0, packageDeletionId: 0, trainDialogId: '', sourceLogDialogId: logDialog.logDialogId, version: 0, rounds: trainRounds, definitions: appDefinition, initialFilledEntities: initialFilledEntities, tags: [], description: '' }; }; //==================================================================== // CONVERSION: LogRound == TrainRound //==================================================================== ModelUtils.ToTrainRound = function (logRound) { return { extractorStep: { textVariations: [ { labelEntities: ModelUtils.ToLabeledEntities(logRound.extractorStep.predictedEntities), text: logRound.extractorStep.text } ], type: TrainDialog_1.ExtractorStepType.USER_INPUT }, scorerSteps: logRound.scorerSteps.map(function (logScorerStep) { return ({ input: logScorerStep.input, labelAction: logScorerStep.predictedAction, logicResult: logScorerStep.logicResult, scoredAction: undefined, uiScoreResponse: logScorerStep.predictionDetails }); }) }; }; //==================================================================== // CONVERSION: LogScorerStep == TrainScorerStep //==================================================================== ModelUtils.ToTrainScorerStep = function (logScorerStep) { return { input: logScorerStep.input, labelAction: logScorerStep.predictedAction, logicResult: logScorerStep.logicResult, scoredAction: undefined }; }; //==================================================================== // CONVERSION: TrainDialog == CreateTeachParams //==================================================================== ModelUtils.ToCreateTeachParams = function (trainDialog) { var createTeachParams = { contextDialog: trainDialog.rounds, sourceLogDialogId: trainDialog.sourceLogDialogId, initialFilledEntities: trainDialog.initialFilledEntities }; // TODO: Change to non destructive operation // Strip out "entityType" (*sigh*) for (var _i = 0, _a = createTeachParams.contextDialog; _i < _a.length; _i++) { var round = _a[_i]; for (var _b = 0, _c = round.extractorStep.textVariations; _b < _c.length; _b++) { var textVariation = _c[_b]; for (var _d = 0, _e = textVariation.labelEntities; _d < _e.length; _d++) { var labeledEntity = _e[_d]; delete labeledEntity.entityType; } } } return createTeachParams; }; //==================================================================== // CONVERSION: TeachResponse == Teach //==================================================================== ModelUtils.ToTeach = function (teachResponse) { return { teachId: teachResponse.teachId, trainDialogId: teachResponse.trainDialogId, createdDatetime: undefined, lastQueryDatetime: undefined, packageId: undefined }; }; //==================================================================== // Misc utils shared between SDK and UI //==================================================================== ModelUtils.areEqualTextVariations = function (tv1, tv2) { if (tv1.text !== tv2.text) { return false; } if (tv1.labelEntities.length !== tv2.labelEntities.length) { return false; } var _loop_1 = function (le1) { var le2 = tv2.labelEntities.find(function (le) { return le.entityId === le1.entityId && le.entityText === le1.entityText && le.startCharIndex === le1.startCharIndex; }); if (!le2) { return { value: false }; } }; for (var _i = 0, _a = tv1.labelEntities; _i < _a.length; _i++) { var le1 = _a[_i]; var state_1 = _loop_1(le1); if (typeof state_1 === "object") return state_1.value; } return true; }; ModelUtils.areEqualMemoryValues = function (mvs1, mvs2) { if (mvs1.length !== mvs2.length) { return false; } var _loop_2 = function (mv1) { var match = mvs2.find(function (mv2) { if (mv1.userText !== mv2.userText) { return false; } if (mv1.displayText !== mv2.displayText) { return false; } if (mv1.builtinType !== mv2.builtinType) { return false; } if (JSON.stringify(mv1.resolution) !== JSON.stringify(mv2.resolution)) { return false; } return true; }); if (!match) { return { value: false }; } }; for (var _i = 0, mvs1_1 = mvs1; _i < mvs1_1.length; _i++) { var mv1 = mvs1_1[_i]; var state_2 = _loop_2(mv1); if (typeof state_2 === "object") return state_2.value; } return true; }; ModelUtils.changedFilledEntities = function (originalEntityMap, newEntityMap) { var changedFilledEntities = []; // Capture emptied entities for (var entityName in originalEntityMap.map) { if (!newEntityMap.map[entityName]) { var filledEntity = { entityId: originalEntityMap.map[entityName].entityId, values: [] }; changedFilledEntities.push(filledEntity); } } for (var entityName in newEntityMap.map) { // Capture new entities if (!originalEntityMap.map[entityName]) { changedFilledEntities.push(newEntityMap.map[entityName]); } // Capture changed entities else if (!ModelUtils.areEqualMemoryValues(newEntityMap.map[entityName].values, originalEntityMap.map[entityName].values)) { changedFilledEntities.push(newEntityMap.map[entityName]); } } return changedFilledEntities; }; ModelUtils.userText = function (extractorStep, excludedEntities, useMarkdown) { if (excludedEntities === void 0) { excludedEntities = []; } if (useMarkdown === void 0) { useMarkdown = false; } if (extractorStep.type === TrainDialog_1.ExtractorStepType.OUT_OF_DOMAIN) { return TrainDialog_1.OUT_OF_DOMAIN_INPUT; } if (useMarkdown) { return ModelUtils.textVariationToMarkdown(extractorStep.textVariations[0], excludedEntities); } return extractorStep.textVariations[0].text; }; ModelUtils.textVariationToMarkdown = function (textVariation, excludeEntities) { if (textVariation.labelEntities.length === 0) { return textVariation.text; } // Remove resolvers that aren't labelled var labelEntities = textVariation.labelEntities.filter(function (le) { return !excludeEntities.includes(le.entityId); }); // Remove duplicate labels labelEntities = labelEntities.filter(function (le, i) { return labelEntities.findIndex(function (fi) { return fi.startCharIndex === le.startCharIndex; }) === i; }); // Remove overlapping labels (can happen if have CUSTOM and Pre-Trained) labelEntities = labelEntities.filter(function (le) { return labelEntities.findIndex(function (fe) { return fe.entityId !== le.entityId && (le.startCharIndex >= fe.startCharIndex && le.endCharIndex <= fe.endCharIndex); }) === -1; }); if (labelEntities.length === 0) { return textVariation.text; } labelEntities = labelEntities.sort(function (a, b) { return (a.startCharIndex > b.startCharIndex ? 1 : a.startCharIndex < b.startCharIndex ? -1 : 0); }); var text = textVariation.text.substring(0, labelEntities[0].startCharIndex); for (var index in labelEntities) { var curEntity = labelEntities[index]; text = text + "**_" + textVariation.text.substring(curEntity.startCharIndex, curEntity.endCharIndex + 1) + "_**"; var nextEntity = labelEntities[Number(index) + 1]; if (nextEntity) { text = "" + text + textVariation.text.substring(curEntity.endCharIndex + 1, nextEntity.startCharIndex); } else { text = "" + text + textVariation.text.substring(curEntity.endCharIndex + 1, textVariation.text.length); } } return text; }; ModelUtils.PrebuiltDisplayText = function (builtinType, resolution, entityText) { if (!builtinType || !resolution) { return entityText; } if (['builtin.geography', 'builtin.encyclopedia'].some(function (prefix) { return builtinType.startsWith(prefix); })) { return entityText; } switch (builtinType) { case 'builtin.datetimeV2.date': var date = resolution.values[0].value; if (resolution.values[1]) { date += " or " + resolution.values[1].value; } return date; case 'builtin.datetimeV2.time': var time = resolution.values[0].value; if (resolution.values[1]) { time += " or " + resolution.values[1].value; } return time; case 'builtin.datetimeV2.daterange': return resolution.values[0].start + " to " + resolution.values[0].end; case 'builtin.datetimeV2.timerange': return resolution.values[0].start + " to " + resolution.values[0].end; case 'builtin.datetimeV2.datetimerange': return resolution.values[0].start + " to " + resolution.values[0].end; case 'builtin.datetimeV2.duration': return resolution.values[0].value + " seconds"; case 'builtin.datetimeV2.set': return "" + resolution.values[0].value; case 'builtin.number': return resolution.value; case 'builtin.ordinal': return resolution.value; case 'builtin.temperature': return resolution.value; case 'builtin.dimension': return resolution.value; case 'builtin.money': return resolution.value; case 'builtin.age': return resolution.value; case 'builtin.percentage': return resolution.value; default: return entityText; } }; return ModelUtils; }()); exports.ModelUtils = ModelUtils; //# sourceMappingURL=ModelUtils.js.map