UNPKG

clwoz-models

Version:
579 lines 24.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var _1 = require("./"); function makeMemoryValue(userText) { if (userText === void 0) { userText = 'userText'; } return { userText: userText, displayText: 'displayText', builtinType: 'number', resolution: { data: 'one', number: '5' } }; } function makeFilledEntity(elementValues) { var values = []; for (var _i = 0, elementValues_1 = elementValues; _i < elementValues_1.length; _i++) { var value = elementValues_1[_i]; values.push(makeMemoryValue(value)); } return { entityId: elementValues.join('.'), values: values }; } function makeFilledEntityMap(mapMakers) { var filledEntityMap = new _1.FilledEntityMap(); for (var _i = 0, mapMakers_1 = mapMakers; _i < mapMakers_1.length; _i++) { var mapMaker = mapMakers_1[_i]; var filledEntities = makeFilledEntity(mapMaker.values); filledEntityMap.map[mapMaker.name] = filledEntities; } return filledEntityMap; } describe('ModelUtils', function () { describe('RemoveWords', function () { test('given empty string return empty string', function () { expect(_1.ModelUtils.RemoveWords('', 0)).toEqual(''); }); test('given string and removing 0 words return string', function () { expect(_1.ModelUtils.RemoveWords('test', 0)).toEqual('test'); }); test('given string with 2 word and removing 1 word return word', function () { expect(_1.ModelUtils.RemoveWords('test1 test2', 1)).toEqual('test2'); }); }); describe('TextVariationEqual', function () { test("pass", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] }, { text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] })).toEqual(true); }); test("complex pass", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'this is tag and frog', labelEntities: [ { entityId: 'c56fb3b8-923c-4d58-9150-838616e29913', startCharIndex: 8, endCharIndex: 10, entityText: 'tag', resolution: {}, builtinType: '' }, { entityId: 'c56fb3b8-923c-4d58-9150-838616e29913', startCharIndex: 16, endCharIndex: 19, entityText: 'frog', resolution: {}, builtinType: '' } ] }, { text: 'this is tag and frog', labelEntities: [ { entityId: 'c56fb3b8-923c-4d58-9150-838616e29913', startCharIndex: 8, endCharIndex: 10, entityText: 'tag', resolution: {}, builtinType: '' }, { entityId: 'c56fb3b8-923c-4d58-9150-838616e29913', startCharIndex: 16, endCharIndex: 19, entityText: 'frog', resolution: {}, builtinType: 'LUIS' } ] })).toEqual(true); }); test("fail based on different text", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] }, { text: 'some different text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] })).toEqual(false); }); test("fail based on different number of entities", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] }, { text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' }, { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] })).toEqual(false); }); test("fail based on different entityIds", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] }, { text: 'some text', labelEntities: [ { entityId: 'differentGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] })).toEqual(false); }); test("fail based on different positions", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] }, { text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 10, endCharIndex: 14, entityText: 'text', resolution: {}, builtinType: '' } ] })).toEqual(false); }); test("fail based on different entity text", function () { expect(_1.ModelUtils.areEqualTextVariations({ text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'text', resolution: {}, builtinType: '' } ] }, { text: 'some text', labelEntities: [ { entityId: 'someGUID', startCharIndex: 5, endCharIndex: 9, entityText: 'other', resolution: {}, builtinType: '' } ] })).toEqual(false); }); }); describe('areEqualMemoryValues', function () { test("equal", function () { var userText = 'User Text'; var mv1 = makeMemoryValue(userText); var mv2 = makeMemoryValue(userText); expect(_1.ModelUtils.areEqualMemoryValues([mv1], [mv2])).toEqual(true); }); test("mv1 diff", function () { var mv1 = makeMemoryValue('mv1'); var mv2 = makeMemoryValue('mv2'); expect(_1.ModelUtils.areEqualMemoryValues([mv1], [mv2])).toEqual(false); }); test("no mv2", function () { var mv1 = makeMemoryValue('mv1'); expect(_1.ModelUtils.areEqualMemoryValues([mv1], [])).toEqual(false); }); test("no mv2", function () { var mv1 = makeMemoryValue('mv1'); expect(_1.ModelUtils.areEqualMemoryValues([], [mv1])).toEqual(false); }); }); describe('changedFilledEntities', function () { test("equal", function () { var fe1 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }, { name: 'e2', values: ['bob'] }]); var fe2 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }, { name: 'e2', values: ['bob'] }]); expect(Object.keys(_1.ModelUtils.changedFilledEntities(fe1, fe2).map).length).toEqual(0); }); test("removed entity", function () { var fe1 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }, { name: 'e2', values: ['bob'] }]); var fe2 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }]); var cem = _1.ModelUtils.changedFilledEntities(fe1, fe2); expect(cem.length).toEqual(1); expect(cem[0].entityId === 'bob'); expect(cem[0].values.length === 0); }); test("added entity", function () { var fe1 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }]); var fe2 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }, { name: 'e2', values: ['bob'] }]); var cem = _1.ModelUtils.changedFilledEntities(fe1, fe2); expect(cem.length).toEqual(1); expect(cem[0].values.length === 1); expect(cem[0].values[0].userText === 'bob'); }); test("changed entity", function () { var fe1 = makeFilledEntityMap([{ name: 'e1', values: ['joe', 'sue'] }, { name: 'e2', values: ['bob'] }]); var fe2 = makeFilledEntityMap([{ name: 'e1', values: ['mary', 'sue'] }, { name: 'e2', values: ['bob'] }]); var cem = _1.ModelUtils.changedFilledEntities(fe1, fe2); expect(cem.length).toEqual(1); expect(cem[0].values.length === 1); expect(cem[0].values[0].userText === 'mary'); }); }); describe('PrebuiltDisplayText', function () { test("given prebuilt with unknown type return entity text", function () { expect(_1.ModelUtils.PrebuiltDisplayText('builtin.nonexistingtype', null, 'entityText')).toEqual('entityText'); }); test('given prebuilt type starts with encyclopedia should return entityText', function () { // Arrange var expected = 'randomValue1'; // Act var actual = _1.ModelUtils.PrebuiltDisplayText('builtin.encyclopedia', null, expected); // Assert expect(actual).toEqual(expected); }); test("given prebuilt with type datetimeV2.date should return resolution values split by 'or'", function () { expect(_1.ModelUtils.PrebuiltDisplayText('builtin.datetimeV2.date', { values: [ { value: 'fake-date' }, { value: 'fake-date-2' } ] }, 'entity-text')).toEqual('fake-date or fake-date-2'); }); test("given prebuilt with type datetimeV2.time should return resolution values split by 'or'", function () { expect(_1.ModelUtils.PrebuiltDisplayText('builtin.datetimeV2.time', { values: [ { value: 'fake-time' }, { value: 'fake-time-2' } ] }, 'entity-text')).toEqual('fake-time or fake-time-2'); }); test("given prebuilt with type datetimeV2.daterange return start and end values from resolution", function () { expect(_1.ModelUtils.PrebuiltDisplayText('builtin.datetimeV2.daterange', { values: [ { start: 'start', end: 'end' } ] }, 'entityText')).toEqual('start to end'); }); test("given prebuilt with type datetimeV2.daterange return start and end values from resolution", function () { expect(_1.ModelUtils.PrebuiltDisplayText('builtin.datetimeV2.duration', { values: [ { value: 'duration' } ] }, 'entityText')).toEqual('duration seconds'); }); }); describe('Params', function () { var trainDialog = { createdDateTime: new Date().toJSON(), lastModifiedDateTime: new Date().toJSON(), trainDialogId: 'trainDialogId', sourceLogDialogId: 'sourceLogDialogId', version: 1, packageCreationId: 1, packageDeletionId: 2, definitions: null, validity: _1.Validity.VALID, initialFilledEntities: [], tags: [], description: '', rounds: [ { extractorStep: { textVariations: [], type: _1.ExtractorStepType.USER_INPUT }, scorerSteps: [ { input: { filledEntities: [], context: {}, maskedActions: [] }, logicResult: undefined, labelAction: 'test', scoredAction: undefined } ] } ] }; var createTeachParams = _1.ModelUtils.ToCreateTeachParams(trainDialog); expect(createTeachParams).toEqual({ contextDialog: trainDialog.rounds, sourceLogDialogId: trainDialog.sourceLogDialogId, initialFilledEntities: trainDialog.initialFilledEntities }); }); }); describe('textVariationToMarkdown', function () { test('no entities', function () { var textVariation = { text: 'no entities', labelEntities: [] }; var expected = 'no entities'; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test('**_start_** entity only', function () { var textVariation = { text: 'start entity only', labelEntities: [ { entityId: '0f427885-b4b2-4b62-af10-040e5e1001de', startCharIndex: 0, endCharIndex: 4, entityText: 'start', resolution: {}, builtinType: 'LUIS' } ] }; var expected = '**_start_** entity only'; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test('**_start_** and **_end_**', function () { var textVariation = { text: 'start and end', labelEntities: [ { entityId: '0f427885-b4b2-4b62-af10-040e5e1001de', startCharIndex: 0, endCharIndex: 4, entityText: 'start', resolution: {}, builtinType: 'LUIS' }, { entityId: 'b558509d-5045-4055-b8c4-a8673b4b9ace', startCharIndex: 10, endCharIndex: 12, entityText: 'end', resolution: {}, builtinType: 'LUIS' } ] }; var expected = '**_start_** and **_end_**'; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test('is **_next_** **_together_** entities', function () { var textVariation = { text: 'is next together entities', labelEntities: [ { entityId: '0f427885-b4b2-4b62-af10-040e5e1001de', startCharIndex: 3, endCharIndex: 6, entityText: 'next', resolution: {}, builtinType: 'LUIS' }, { entityId: 'b558509d-5045-4055-b8c4-a8673b4b9ace', startCharIndex: 8, endCharIndex: 15, entityText: 'together', resolution: {}, builtinType: 'LUIS' } ] }; var expected = 'is **_next_** **_together_** entities'; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test('a **_multi word_** entity', function () { var textVariation = { text: 'a multi word entity', labelEntities: [ { entityId: '0f427885-b4b2-4b62-af10-040e5e1001de', startCharIndex: 2, endCharIndex: 11, entityText: 'multi word', resolution: {}, builtinType: 'LUIS' } ] }; var expected = 'a **_multi word_** entity'; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test('**_solo_**', function () { var textVariation = { text: 'solo', labelEntities: [ { entityId: '0f427885-b4b2-4b62-af10-040e5e1001de', startCharIndex: 0, endCharIndex: 3, entityText: 'solo', resolution: {}, builtinType: 'LUIS' } ] }; var expected = '**_solo_**'; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test("i'd like to go **_tomorrow_**", function () { var textVariation = { text: "i'd like to go tomorrow", labelEntities: [ { score: 0, entityId: "b18c67af-26c3-4756-88af-76baf68a59ee", startCharIndex: 15, endCharIndex: 22, entityText: "tomorrow", resolution: { values: [ { timex: "2019-01-03", type: "date", value: "2019-01-03" } ] }, builtinType: "builtin.datetimeV2.date" }, { entityId: "7367096c-80a1-4fe6-8d55-e78522e342bf", startCharIndex: 15, endCharIndex: 22, entityText: "tomorrow", resolution: {}, builtinType: "LUIS", score: 0 } ] }; var expected = "i'd like to go **_tomorrow_**"; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test("**_today_**", function () { var textVariation = { text: "today", labelEntities: [ { entityId: "cb7c14f5-bb3b-4f4f-a7ba-bd8289efa995", startCharIndex: 0, endCharIndex: 4, entityText: "today" } ] }; var expected = "**_today_**"; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); test("send to **_lars@outlook.com_**", function () { var textVariation = { text: "send to lars@outlook.com", labelEntities: [ { entityId: "1a4cad5c-0eab-41d4-b569-e93af4b6a19e", startCharIndex: 8, endCharIndex: 23, entityText: "lars@outlook.com", resolution: { value: "lars@outlook.com" }, builtinType: "builtin.email" } ] }; var expected = "send to **_lars@outlook.com_**"; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); // Built in that is only used as a resolver and not labelled test("i'd like to go tomorrow", function () { var textVariation = { text: "i'd like to go tomorrow", labelEntities: [ { score: 0, entityId: "b18c67af-26c3-4756-88af-76baf68a59ee", startCharIndex: 15, endCharIndex: 22, entityText: "tomorrow", resolution: { values: [ { timex: "2019-01-03", type: "date", value: "2019-01-03" } ] }, builtinType: "builtin.datetimeV2.date" } ] }; var expected = "i'd like to go tomorrow"; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, ["b18c67af-26c3-4756-88af-76baf68a59ee"]); expect(result).toBe(expected); }); // Built-in subset of custom test("meet me on Wednesday", function () { var textVariation = { text: "meet me on Wednesday", labelEntities: [ { score: 0, entityId: "26f0b940-64a0-44c3-ab1a-14894ccae3fe", startCharIndex: 0, endCharIndex: 19, entityText: "meet me on wednesday", resolution: {}, builtinType: "LUIS" }, { entityId: "6875e7e7-a020-4c88-b7d3-3b8023303022", startCharIndex: 11, endCharIndex: 19, entityText: "wednesday", resolution: { values: [ { timex: "XXXX-WXX-3", type: "date", value: "2019-07-03" }, { timex: "XXXX-WXX-3", type: "date", value: "2019-07-10" } ] }, builtinType: "builtin.datetimeV2.date" } ] }; var expected = "**_meet me on Wednesday_**"; var result = _1.ModelUtils.textVariationToMarkdown(textVariation, []); expect(result).toBe(expected); }); }); //# sourceMappingURL=modelUtils.test.js.map