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

Commit 29659c9

Browse files
vishwacsenaVishwac Sena Kannan
andauthored
fix to handle pl with spaces correctly (#779)
Co-authored-by: Vishwac Sena Kannan <[email protected]>
1 parent 89810dc commit 29659c9

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

packages/lu/src/parser/lufile/parseFileContents.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ const featureProperties = {
4545
phraseListFeature: 'phraselist'
4646
}
4747
const INTENTTYPE = 'intent';
48+
const PLCONSTS = {
49+
DISABLED : 'disabled',
50+
ENABLEDFORALLMODELS: 'enabledforallmodels',
51+
INTERCHANGEABLE: '(interchangeable)'
52+
};
4853
const parseFileContentsModule = {
4954
/**
5055
* Main parser code to parse current file contents into LUIS and QNA sections.
@@ -304,6 +309,7 @@ const validateNDepthEntities = function(collection, entitiesAndRoles, intentsCol
304309
(child.features || []).forEach((feature, idx) => {
305310
if (typeof feature === "object") return;
306311
featureHandled = false;
312+
feature = feature.replace(/["']/gi, '');
307313
let featureExists = entitiesAndRoles.find(i => (i.name == feature || i.name == `${feature}(interchangeable)`));
308314
if (featureExists) {
309315
// is feature phrase list?
@@ -1008,12 +1014,16 @@ const validateAndGetRoles = function(parsedContent, roles, line, entityName, ent
10081014
newRoles.forEach(role => {
10091015
let roleFound = parsedContent.LUISJsonStructure.flatListOfEntityAndRoles.find(item => item.roles.includes(role) || item.name === role);
10101016
if (roleFound !== undefined) {
1011-
let errorMsg = `Roles must be unique across entity types. Invalid role definition found "${entityName}". Prior definition - '@ ${roleFound.type} ${roleFound.name}${roleFound.roles.length > 0 ? ` hasRoles ${roleFound.roles.join(',')}` : ``}'`;
1012-
let error = BuildDiagnostic({
1013-
message: errorMsg,
1014-
context: line
1015-
})
1016-
throw (new exception(retCode.errorCode.INVALID_INPUT, error.toString(), [error]));
1017+
// PL entities use roles for things like interchangeable, disabled, enabled for all models. There are really not 'dupes'.
1018+
let hasBadNonPLRoles = (roleFound.roles || []).filter(item => item !== PLCONSTS.INTERCHANGEABLE && item !== PLCONSTS.ENABLEDFORALLMODELS && item !== PLCONSTS.DISABLED);
1019+
if (hasBadNonPLRoles.length !== 0) {
1020+
let errorMsg = `Roles must be unique across entity types. Invalid role definition found "${entityName}". Prior definition - '@ ${roleFound.type} ${roleFound.name}${roleFound.roles.length > 0 ? ` hasRoles ${roleFound.roles.join(',')}` : ``}'`;
1021+
let error = BuildDiagnostic({
1022+
message: errorMsg,
1023+
context: line
1024+
})
1025+
throw (new exception(retCode.errorCode.INVALID_INPUT, error.toString(), [error]));
1026+
}
10171027
}
10181028
});
10191029

@@ -1337,11 +1347,11 @@ const handlePhraseList = function(parsedContent, entityName, entityType, entityR
13371347
if (entityRoles.length !== 0) {
13381348
// Phrase lists cannot have roles; however we will allow inline definition of enabledForAllModels as well as disabled as a property on phrase list.
13391349
entityRoles.forEach(item => {
1340-
if (item.toLowerCase() === 'disabled') {
1350+
if (item.toLowerCase() === PLCONSTS.DISABLED) {
13411351
isPLEnabled = false;
1342-
} else if (item.toLowerCase() === 'enabledforallmodels') {
1352+
} else if (item.toLowerCase() === PLCONSTS.ENABLEDFORALLMODELS) {
13431353
isPLEnabledForAllModels = true;
1344-
} else if (item.toLowerCase() === '(interchangeable)') {
1354+
} else if (item.toLowerCase() === PLCONSTS.INTERCHANGEABLE) {
13451355
entityName += item;
13461356
} else {
13471357
let errorMsg = `Phrase list entity ${entityName} has invalid role definition with roles = ${entityRoles.join(', ')}. Roles are not supported for Phrase Lists`;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,32 @@ describe('V2 Entity definitions using @ notation', function () {
11291129
})
11301130
.catch(err => done(err));
11311131
})
1132+
1133+
it('Phrase list definition with spaces in them are handled correctly when added as features', function(done){
1134+
let luFile = `
1135+
@ phraselist LeaveModifiers(interchangeable) =
1136+
- Change,cancel,replace,edit,remove,modify,delete,alter,change,drop
1137+
1138+
@ phraselist Durations(interchangeable) =
1139+
- days,day,month,months,weeks,week
1140+
1141+
@ phraselist "Months of the Year"(interchangeable) =
1142+
- January,Jan,Feburary,Feb,March,Mar,April,Apr,May,June,Jun,July,Jul,August,Aug,September,Sep,Sept,October,Oct,November,Nov,December,Dec
1143+
1144+
@ ml Leave
1145+
- @ ml LeaveType
1146+
- @ ml LeaveDate
1147+
- @ ml "Start Date" usesFeatures "Months of the Year"`;
1148+
1149+
parseFile.parseFile(luFile)
1150+
.then(res => {
1151+
assert.equal(res.LUISJsonStructure.phraselists.length, 3);
1152+
assert.equal(res.LUISJsonStructure.entities[0].children[1].children[0].name, 'Start Date');
1153+
assert.equal(res.LUISJsonStructure.entities[0].children[1].children[0].features[0].featureName, 'Months of the Year');
1154+
done();
1155+
})
1156+
.catch(err => done(err));
1157+
})
11321158
})
11331159

11341160
describe('multi-content', function(){

0 commit comments

Comments
 (0)