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

Commit 3f9ee15

Browse files
Shuai WangJosh Gummersall
andauthored
fix import filtered intents not work in luis:cross-train (#1232)
* fix filter intent not working * rewrite export statement * modify import * add a test case * fix a import bug * extract code to a function * fix issues * fix import syntax and other minor changes * fix deep clone Co-authored-by: Josh Gummersall <[email protected]>
1 parent 30f8b2f commit 3f9ee15

File tree

21 files changed

+280
-15
lines changed

21 files changed

+280
-15
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lu/src/parser/cross-train/cross-train.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const fs = require('fs-extra')
88
const filehelper = require('../../utils/filehelper')
99
const fileExtEnum = require('../utils/helpers').FileExtTypeEnum
1010
const crossTrainer = require('./crossTrainer')
11+
const LuisBuilder = require('./../luis/luisCollate')
12+
const {parseUtterancesToLu} = require('./../luis/luConverter')
1113

1214
module.exports = {
1315
/**
@@ -35,12 +37,32 @@ module.exports = {
3537
let importResolver = async function (id, idsToFind) {
3638
let importedContents = []
3739
const idWithoutExt = path.basename(id, path.extname(id))
38-
const locale = /\w\.\w/.test(idWithoutExt) ? idWithoutExt.split('.').pop() : defaultLocale;
39-
for (let idx = 0; idx < idsToFind.length; idx++) {
40-
let file = idsToFind[idx]
40+
const locale = /\w\.\w/.test(idWithoutExt) ? idWithoutExt.split('.').pop() : defaultLocale
41+
const intentFilteringHandler = async (filePathOrFound, intent, isAbsolutePath) => {
42+
let content = filePathOrFound
43+
if (isAbsolutePath) {
44+
importFile = (await filehelper.getFileContent(filePathOrFound, fileExtEnum.LUFile))[0]
45+
content = importFile ? [importFile.content] : ''
46+
}
47+
48+
const luObj = await LuisBuilder.build(content, false, undefined, importResolver)
49+
50+
const matchedUtterence = luObj.utterances.find(e => e.intent === intent)
51+
const fileContent = `# ${intent}\r\n${parseUtterancesToLu([matchedUtterence], luObj)}`
52+
const foundItem = isAbsolutePath ? importFile : filePathOrFound[0]
53+
const cloned = JSON.parse(JSON.stringify(foundItem))
54+
cloned.content = fileContent
55+
importedContents.push(cloned)
56+
}
57+
58+
for (const file of idsToFind) {
4159
if (path.isAbsolute(file.filePath)) {
4260
if (file.filePath.endsWith(fileExtEnum.LUFile)) {
43-
importedContents.push(...await filehelper.getFilesContent(file.filePath, fileExtEnum.LUFile))
61+
if (!file.intent) {
62+
importedContents.push(...await filehelper.getFilesContent(file.filePath, fileExtEnum.LUFile))
63+
} else {
64+
await intentFilteringHandler(file.filePath, file.intent, true)
65+
}
4466
} else if (file.filePath.endsWith(fileExtEnum.QnAFile)) {
4567
importedContents.push(...await filehelper.getFilesContent(file.filePath, fileExtEnum.QnAFile))
4668
}
@@ -58,7 +80,11 @@ module.exports = {
5880
}
5981

6082
if(found.length > 0) {
61-
importedContents.push(...found)
83+
if (!file.intent) {
84+
importedContents.push(...found)
85+
} else {
86+
await intentFilteringHandler(found, file.intent, false)
87+
}
6288
} else {
6389
const matchedLuisFiles = typedContents.filter(content => path.basename(content.fullPath) === id)
6490
for (const matchFile of matchedLuisFiles) {

packages/lu/src/parser/cross-train/crossTrainer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ const qnaAddMetaData = function (qnaResource, fileName) {
511511
const parseAndValidateContent = async function (objectArray, verbose, importResolver, fileExt) {
512512
let fileIdToResourceMap = new Map()
513513
let allEmpty = true
514-
for (const object of objectArray) {
514+
for (const object of objectArray) {
515515
let fileContent = object.content
516516
let objectId = object.id
517517
if (object.content && object.content !== '') {

packages/lu/src/parser/lufile/classes/filesToParse.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ class FileToParse {
99
/**
1010
* @property {Boolean} includeInCollate
1111
*/
12-
constructor(filePath, includeInCollate) {
12+
/**
13+
* @property {string} intent
14+
*/
15+
constructor(filePath, includeInCollate, intent = '') {
1316
this.filePath = filePath?filePath:'';
1417
if(includeInCollate === undefined) this.includeInCollate = true;
1518
else this.includeInCollate = includeInCollate;
19+
this.intent = intent
1620
}
1721
}
1822

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c
967967
}
968968
let parsedLinkUriInUtterance = helpers.parseLinkURISync(utterance);
969969
// examine and add these to filestoparse list.
970-
parsedContent.additionalFilesToParse.push(new fileToParse(parsedLinkUriInUtterance.fileName, false));
970+
parsedContent.additionalFilesToParse.push(new fileToParse(parsedLinkUriInUtterance.fileName, false, parsedLinkUriInUtterance.path));
971971
}
972972

973973
(utteranceAndEntities.entities || []).forEach(entity => {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,5 @@ const objectSortByStartPos = function (objectArray) {
550550
return ObjectByStartPos;
551551
}
552552

553-
module.exports = luisToLuContent
553+
module.exports = luisToLuContent
554+
module.exports.parseUtterancesToLu = parseUtterancesToLu
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# InternalIntent
2+
- Intent within application.lu
3+
4+
# ExternalIntentShouldBeIncluded
5+
- [ExternalFile](External.lu#ExternalIntentShouldBeIncluded)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ? Question In ExternalFile.qna
2+
3+
```
4+
Answer to question in ExternalFile.qna
5+
```

packages/luis/bin/app/External.lu

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ExternalIntentShouldBeIncluded
2+
- [ExternalFile2](External2.lu#ExternalIntentShouldBeIncluded)
3+
4+
# ExternalIntentShouldNotBeIncluded
5+
- Intent which should NOT be included

packages/luis/bin/app/External2.lu

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ExternalIntentShouldBeIncluded
2+
- Intent which should be included
3+
4+
# ExternalIntentShouldNotBeIncluded2
5+
- Intent2 which should NOT be included

0 commit comments

Comments
 (0)