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

Commit 88b3e4c

Browse files
vishwacsenaChristopher Anderson
andauthored
Fixes #444 (#492)
Co-authored-by: Christopher Anderson <chrande@microsoft.com>
1 parent 096a1fd commit 88b3e4c

File tree

6 files changed

+3970
-2
lines changed

6 files changed

+3970
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ const RemoveDuplicatePatternAnyEntity = function(parsedContent, pEntityName, ent
12671267
return false;
12681268
}
12691269
});
1270-
if (PAEntityFound !== undefined && PAIdx !== -1) {
1270+
if (PAEntityFound !== undefined && PAIdx !== -1 && entityType != EntityTypeEnum.PATTERNANY) {
12711271
if (entityType.toLowerCase().trim().includes('phraselist')) {
12721272
let errorMsg = `Phrase lists cannot be used as an entity in a pattern "${pEntityName}"`;
12731273
let error = BuildDiagnostic({
@@ -1762,7 +1762,7 @@ const parseAndHandleModelInfoSection = function (parsedContent, luResource, log)
17621762
if (modelInfos && modelInfos.length > 0) {
17631763
for (const modelInfo of modelInfos) {
17641764
let line = modelInfo.ModelInfo
1765-
let kvPair = line.split(/@(app|kb|intent|entity|enableMergeIntents).(.*)=/g).map(item => item.trim());
1765+
let kvPair = line.split(/@(app|kb|intent|entity|enableMergeIntents|patternAnyEntity).(.*)=/g).map(item => item.trim());
17661766
if (kvPair.length === 4) {
17671767
if (kvPair[1] === 'enableMergeIntents' && kvPair[3] === 'false') {
17681768
enableMergeIntents = false;
@@ -1842,6 +1842,31 @@ const parseAndHandleModelInfoSection = function (parsedContent, luResource, log)
18421842
process.stdout.write(chalk.default.yellowBright('[WARN]: Invalid entity inherits information found. Skipping "' + line + '"\n'));
18431843
}
18441844
}
1845+
} else if (kvPair[1].toLowerCase() === 'patternanyentity') {
1846+
if (kvPair[2].toLowerCase() === 'inherits') {
1847+
let inheritsProperties = kvPair[3].split(/[:;]/g).map(item => item.trim());
1848+
if (inheritsProperties.length !== 6) {
1849+
process.stdout.write(chalk.default.yellowBright('[WARN]: Invalid Pattern.Any inherits information found. Skipping "' + line + '"\n'));
1850+
} else {
1851+
// find the intent
1852+
let entity = parsedContent.LUISJsonStructure.patternAnyEntities.find(item => item.name == inheritsProperties[1]);
1853+
if (entity === undefined) {
1854+
let newEntity = new helperClass.patternAnyEntity(inheritsProperties[1]);
1855+
newEntity.inherits = {};
1856+
newEntity['inherits'][inheritsProperties[2]] = inheritsProperties[3];
1857+
newEntity['inherits'][inheritsProperties[4]] = inheritsProperties[5];
1858+
parsedContent.LUISJsonStructure.patternAnyEntities.push(newEntity);
1859+
} else {
1860+
if (entity['inherits'] === undefined) entity['inherits'] = {};
1861+
entity['inherits'][inheritsProperties[2]] = inheritsProperties[3];
1862+
entity['inherits'][inheritsProperties[4]] = inheritsProperties[5];
1863+
}
1864+
}
1865+
} else {
1866+
if (log) {
1867+
process.stdout.write(chalk.default.yellowBright('[WARN]: Invalid entity inherits information found. Skipping "' + line + '"\n'));
1868+
}
1869+
}
18451870
}
18461871
} else {
18471872
if (log) {

packages/lu/src/parser/luis/luConverter.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const luisToLuContent = function(luisJSON){
2828
fileContent += parseToLuClosedLists(luisJSON)
2929
fileContent += parseRegExEntitiesToLu(luisJSON)
3030
fileContent += parseCompositesToLu(luisJSON)
31+
fileContent += parsePatternAnyEntitiesToLu(luisJSON)
3132
return fileContent
3233
}
3334

@@ -213,6 +214,33 @@ const parseCompositesToLu = function(luisJson){
213214
return fileContent
214215
}
215216

217+
const parsePatternAnyEntitiesToLu = function(luisJson){
218+
let fileContent = ''
219+
if(!luisJson.patternAnyEntities || luisJson.patternAnyEntities.length <= 0) {
220+
return fileContent;
221+
}
222+
luisJson.patternAnyEntities.forEach(patternAnyEntity => {
223+
// Add inherits information if any
224+
if (patternAnyEntity.inherits !== undefined) {
225+
fileContent += '> # Pattern.Any entities' + NEWLINE + NEWLINE;
226+
// > !# @intent.inherits = {name = Web.WebSearch; domain_name = Web; model_name = WebSearch}
227+
fileContent += '> !# @patternAnyEntity.inherits = name : ' + patternAnyEntity.name;
228+
if (patternAnyEntity.inherits.domain_name !== undefined) {
229+
fileContent += '; domain_name : ' + patternAnyEntity.inherits.domain_name;
230+
}
231+
if (patternAnyEntity.inherits.model_name !== undefined) {
232+
fileContent += '; model_name : ' + patternAnyEntity.inherits.model_name;
233+
}
234+
fileContent += NEWLINE + NEWLINE;
235+
// For back compat we will only write this if the pattern.any has inherits information.
236+
fileContent += `@ patternany ${patternAnyEntity.name}`;
237+
fileContent += addRolesAndFeatures(patternAnyEntity);
238+
fileContent += NEWLINE;
239+
}
240+
})
241+
return fileContent;
242+
}
243+
216244
/**
217245
* Helper to handle phrase lists both in the new and old property.
218246
* @param {Object[]} collection

packages/lu/test/commands/luis/convert.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,30 @@ describe('luis:convert new entity format', () => {
615615
})
616616
})
617617

618+
describe('luis:convert with pattern.any inherits information', () => {
619+
before(async function(){
620+
await fs.mkdirp(path.join(__dirname, './../../../results/'))
621+
})
622+
623+
after(async function(){
624+
await fs.remove(path.join(__dirname, './../../../results/'))
625+
})
626+
627+
test
628+
.stdout()
629+
.command(['luis:convert', '--in', `${path.join(__dirname, './../../fixtures/verified/LUISAppWithPAInherits.lu')}`, '--out', './results/LUISAppWithPAInherits.lu.json'])
630+
.it('luis:convert with pattern.any inherits information correctly produces a LUIS app', async () => {
631+
expect(await compareLuFiles('./../../../results/LUISAppWithPAInherits.lu.json', './../../fixtures/verified/LUISAppWithPAInherits.lu.json')).to.be.true
632+
})
633+
634+
test
635+
.stdout()
636+
.command(['luis:convert', '--in', `${path.join(__dirname, './../../fixtures/testcases/LUISAppWithPAInherits.json')}`, '--out', './results/root2_a.lu'])
637+
.it('luis:convert successfully reconstructs a markdown file from a LUIS input file with pattern.any inherits information', async () => {
638+
expect(await compareLuFiles('./../../../results/root2_a.lu', './../../fixtures/verified/LUISAppWithPAInherits.lu')).to.be.true
639+
})
640+
})
641+
618642
describe('luis:convert file creation', () => {
619643
test
620644
.stderr()

0 commit comments

Comments
 (0)