Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 63ef9c8

Browse files
vishwacsenaVishwac Sena Kannanmunozemilio
authored
v7 upgrade throw patterns w/ ref to child entities (#733)
Co-authored-by: Vishwac Sena Kannan <[email protected]> Co-authored-by: Emilio Munoz <[email protected]>
1 parent 0c3f104 commit 63ef9c8

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

packages/lu/src/parser/utils/helpers.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,36 @@ const updateToV7 = function(finalLUISJSON) {
204204
});
205205
(finalLUISJSON.entities || []).forEach(entity => transformAllEntityConstraintsToFeatures(entity));
206206
(finalLUISJSON.intents || []).forEach(intent => addIsRequiredProperty(intent));
207-
transformUtterancesWithNDepthEntities(finalLUISJSON)
207+
let entityParentTree = {};
208+
const curPath = ["$root$"];
209+
constructEntityParentTree(finalLUISJSON.entities, entityParentTree, curPath);
210+
transformUtterancesWithNDepthEntities(finalLUISJSON, entityParentTree)
211+
verifyPatternsDoNotHaveChildEntityReferences(finalLUISJSON, entityParentTree)
208212
}
209213
}
210214

215+
const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, entityParentTree)
216+
{
217+
if (finalLUISJSON.patterns === undefined || !Array.isArray(finalLUISJSON.patterns) || finalLUISJSON.patterns.length === 0) return;
218+
(finalLUISJSON.patterns || []).forEach(pattern => {
219+
// detect if pattern has an entity definition
220+
let entitiesRegExp = /{(?<entity>[^{,}]+)}/gmi;
221+
let entitiesFound = pattern.pattern.match(entitiesRegExp);
222+
if (entitiesFound !== null) {
223+
// verify that each entity is not a child entity
224+
entitiesFound.forEach(entity => {
225+
entity = entity.replace(/[{}]/g, '');
226+
let entityInTree = entityParentTree[entity]
227+
if (entityInTree !== undefined) {
228+
if (entityInTree[0] != "$root$") {
229+
throw (new exception(retCode.errorCode.INVALID_INPUT, `Patterns cannot contain references to child entities. Pattern: "${pattern.pattern}" has reference to "{${entity}}".`));
230+
}
231+
}
232+
})
233+
}
234+
})
235+
}
236+
211237
const constructEntityParentTree = function(entityCollection, entityParentTree, curPath)
212238
{
213239
entityCollection.forEach(entity => {
@@ -229,11 +255,8 @@ const updateTreeWithNode = function(curPath, entityName, entityParentTree) {
229255
curPath.reverse();
230256
}
231257

232-
const transformUtterancesWithNDepthEntities = function (finalLUISJSON) {
233-
let entityParentTree = {};
234-
const curPath = ["$root$"];
235-
constructEntityParentTree(finalLUISJSON.entities, entityParentTree, curPath);
236-
finalLUISJSON.utterances.forEach(utt => {
258+
const transformUtterancesWithNDepthEntities = function (finalLUISJSON, entityParentTree) {
259+
(finalLUISJSON.utterances || []).forEach(utt => {
237260
if (utt.entities !== undefined && Array.isArray(utt.entities) && utt.entities.length !== 0) {
238261
// sort all entities by start and end position
239262
utt.entities = objectSortByStartPos(utt.entities)

packages/lu/test/fixtures/examples/newEntityWithFeatures.lu

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
- My last name is {@userProfile = {@userName = {@lastName = kannan}}}
1616
- {@userProfile = {@userName = {@firstName = vishwac}}}
1717
- {@userProfile = {@userAge = 36}}
18-
- I'm {@firstName} and I'm {@userAge} [years old]
1918

2019
> Featurize intent
2120
@ intent GetUserProfile usesFeatures userProfile, profileDefinition

packages/lu/test/fixtures/verified/nDepthEntityInUtterance.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,7 @@
150150
]
151151
}
152152
],
153-
"patterns": [
154-
{
155-
"pattern": "this is a {nDepth_child1} pattern and {nDepth} pattern",
156-
"intent": "None"
157-
}
158-
],
153+
"patterns": [],
159154
"patternAnyEntities": [],
160155
"prebuiltEntities": [
161156
{

packages/lu/test/fixtures/verified/nDepthEntityInUtterance.lu

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
- {@nDepth=i have {@nDepth_child1=20 years old}}
1515
- {@nDepth=i have {@nDepth_child1=25 years old}}
1616
- {@nDepth=i have {@nDepth_child1=98 years old}}
17-
- this is a {@nDepth_child1} pattern and {nDepth} pattern
1817

1918

2019
> # Entity definitions

packages/lu/test/fixtures/verified/newEntityWithFeatures.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,7 @@
272272
]
273273
}
274274
],
275-
"patterns": [
276-
{
277-
"pattern": "I'm {firstName} and I'm {userAge} [years old]",
278-
"intent": "GetUserProfile"
279-
}
280-
],
275+
"patterns": [],
281276
"patternAnyEntities": [],
282277
"prebuiltEntities": [
283278
{

packages/lu/test/fixtures/verified/newEntityWithFeatures.lu

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
- My last name is {@userProfile={@userName={@lastName=kannan}}}
2222
- {@userProfile={@userName={@firstName=vishwac}}}
2323
- {@userProfile={@userAge=36}}
24-
- I'm {@firstName} and I'm {userAge} [years old]
2524

2625

2726
@ intent GetUserProfile usesFeatures userProfile,profileDefinition

packages/lu/test/parser/lufile/parseFileContents.nDepthEntity.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,5 +765,22 @@ describe('V2 NDepth definitions using @ notation', function () {
765765
.then(res => done(res))
766766
.catch(err => done())
767767
})
768+
769+
it('[V7 upgrade test] Patterns cannot have refernece to child entities', function(done) {
770+
let luFile = `
771+
@ ml userProfile =
772+
- @ personName firstName
773+
- @ personName lastName
774+
775+
@ prebuilt personName
776+
777+
# test
778+
- [my] name is vishwac
779+
- my first name is {@firstName} and last name is {@lastName}`;
780+
781+
parseFile.parseFile(luFile)
782+
.then(res => done(res))
783+
.catch(err => done())
784+
})
768785

769786
});

0 commit comments

Comments
 (0)