Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions packages/lu/src/parser/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,36 @@ const updateToV7 = function(finalLUISJSON) {
});
(finalLUISJSON.entities || []).forEach(entity => transformAllEntityConstraintsToFeatures(entity));
(finalLUISJSON.intents || []).forEach(intent => addIsRequiredProperty(intent));
transformUtterancesWithNDepthEntities(finalLUISJSON)
let entityParentTree = {};
const curPath = ["$root$"];
constructEntityParentTree(finalLUISJSON.entities, entityParentTree, curPath);
transformUtterancesWithNDepthEntities(finalLUISJSON, entityParentTree)
verifyPatternsDoNotHaveChildEntityReferences(finalLUISJSON, entityParentTree)
}
}

const verifyPatternsDoNotHaveChildEntityReferences = function(finalLUISJSON, entityParentTree)
{
if (finalLUISJSON.patterns === undefined || !Array.isArray(finalLUISJSON.patterns) || finalLUISJSON.patterns.length === 0) return;
(finalLUISJSON.patterns || []).forEach(pattern => {
// detect if pattern has an entity definition
let entitiesRegExp = /{(?<entity>[^{,}]+)}/gmi;
let entitiesFound = pattern.pattern.match(entitiesRegExp);
if (entitiesFound !== null) {
// verify that each entity is not a child entity
entitiesFound.forEach(entity => {
entity = entity.replace(/[{}]/g, '');
let entityInTree = entityParentTree[entity]
if (entityInTree !== undefined) {
if (entityInTree[0] != "$root$") {
throw (new exception(retCode.errorCode.INVALID_INPUT, `Patterns cannot contain references to child entities. Pattern: "${pattern.pattern}" has reference to "{${entity}}".`));
}
}
})
}
})
}

const constructEntityParentTree = function(entityCollection, entityParentTree, curPath)
{
entityCollection.forEach(entity => {
Expand All @@ -229,11 +255,8 @@ const updateTreeWithNode = function(curPath, entityName, entityParentTree) {
curPath.reverse();
}

const transformUtterancesWithNDepthEntities = function (finalLUISJSON) {
let entityParentTree = {};
const curPath = ["$root$"];
constructEntityParentTree(finalLUISJSON.entities, entityParentTree, curPath);
finalLUISJSON.utterances.forEach(utt => {
const transformUtterancesWithNDepthEntities = function (finalLUISJSON, entityParentTree) {
(finalLUISJSON.utterances || []).forEach(utt => {
if (utt.entities !== undefined && Array.isArray(utt.entities) && utt.entities.length !== 0) {
// sort all entities by start and end position
utt.entities = objectSortByStartPos(utt.entities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
- My last name is {@userProfile = {@userName = {@lastName = kannan}}}
- {@userProfile = {@userName = {@firstName = vishwac}}}
- {@userProfile = {@userAge = 36}}
- I'm {@firstName} and I'm {@userAge} [years old]

> Featurize intent
@ intent GetUserProfile usesFeatures userProfile, profileDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,7 @@
]
}
],
"patterns": [
{
"pattern": "this is a {nDepth_child1} pattern and {nDepth} pattern",
"intent": "None"
}
],
"patterns": [],
"patternAnyEntities": [],
"prebuiltEntities": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
- {@nDepth=i have {@nDepth_child1=20 years old}}
- {@nDepth=i have {@nDepth_child1=25 years old}}
- {@nDepth=i have {@nDepth_child1=98 years old}}
- this is a {@nDepth_child1} pattern and {nDepth} pattern


> # Entity definitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,7 @@
]
}
],
"patterns": [
{
"pattern": "I'm {firstName} and I'm {userAge} [years old]",
"intent": "GetUserProfile"
}
],
"patterns": [],
"patternAnyEntities": [],
"prebuiltEntities": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
- My last name is {@userProfile={@userName={@lastName=kannan}}}
- {@userProfile={@userName={@firstName=vishwac}}}
- {@userProfile={@userAge=36}}
- I'm {@firstName} and I'm {userAge} [years old]


@ intent GetUserProfile usesFeatures userProfile,profileDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,5 +765,22 @@ describe('V2 NDepth definitions using @ notation', function () {
.then(res => done(res))
.catch(err => done())
})

it('[V7 upgrade test] Patterns cannot have refernece to child entities', function(done) {
let luFile = `
@ ml userProfile =
- @ personName firstName
- @ personName lastName

@ prebuilt personName

# test
- [my] name is vishwac
- my first name is {@firstName} and last name is {@lastName}`;

parseFile.parseFile(luFile)
.then(res => done(res))
.catch(err => done())
})

});