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

Commit 76cad5c

Browse files
authored
Optimize empty file validation and import resolver corner cases in crossTrianer (#1038)
* optimize empty file validation in crossTrianer * adjust some corner case
1 parent 999bc32 commit 76cad5c

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ const mergeInterruptionIntent = function (fromUtterances, toResource, intentName
293293

294294
// add section here
295295
// not add the interruption intent if original file is empty
296-
if (toResource.content.Content !== '') {
296+
// here empty means there are no model related sections exception information section
297+
if (toResource.content.Sections.filter(s => s.SectionType !== LUSectionTypes.MODELINFOSECTION).length > 0) {
297298
toResource.content = new SectionOperator(toResource.content).addSection(newFileContent)
298299
}
299300
}
@@ -511,12 +512,15 @@ const parseAndValidateContent = async function (objectArray, verbose, importReso
511512
let allEmpty = true
512513
for (const object of objectArray) {
513514
let fileContent = object.content
515+
let objectId = object.id
514516
if (object.content && object.content !== '') {
515517
if (fileExt === fileExtEnum.LUFile) {
518+
if (!object.id.endsWith(fileExtEnum.LUFile)) object.id += fileExtEnum.LUFile
516519
let result = await LuisBuilderVerbose.build([object], verbose, undefined, importResolver)
517520
let luisObj = new Luis(result)
518521
fileContent = luisObj.parseToLuContent()
519522
} else {
523+
if (!object.id.endsWith(fileExtEnum.QnAFile)) object.id += fileExtEnum.QnAFile
520524
let result = await qnaBuilderVerbose.build([object], verbose, importResolver)
521525
fileContent = result.parseToQnAContent()
522526
}
@@ -540,16 +544,16 @@ const parseAndValidateContent = async function (objectArray, verbose, importReso
540544
}
541545
}
542546

543-
fileIdToResourceMap.set(object.id, resource)
547+
fileIdToResourceMap.set(objectId, resource)
544548
}
545549

546550
return {fileIdToResourceMap, allEmpty}
547551
}
548552

549553
const pretreatment = function (luContents, qnaContents) {
550554
// Parse lu and qna objects
551-
let luObjectArray = fileHelper.getParsedObjects(luContents)
552-
let qnaObjectArray = fileHelper.getParsedObjects(qnaContents)
555+
let luObjectArray = fileHelper.getParsedObjects(luContents, fileExtEnum.LUFile)
556+
let qnaObjectArray = fileHelper.getParsedObjects(qnaContents, fileExtEnum.QnAFile)
553557

554558
return {luObjectArray, qnaObjectArray}
555559
}

packages/lu/src/utils/filehelper.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
import {readTextFile} from './textfilereader'
77
import Lu = require('../parser/lu/lu')
8+
import QnA = require('../parser/lu/qna')
89
const exception = require('./../parser/utils/exception')
910
const retCode = require('./../parser/utils/enums/CLI-errors')
1011
const fs = require('fs-extra')
1112
const path = require('path')
1213
const helpers = require('./../parser/utils/helpers')
14+
const fileExtEnum = require('./../parser/utils/helpers').FileExtTypeEnum
1315
const LUOptions = require('./../parser/lu/luOptions')
16+
const QnAOptions = require('./../parser/lu/qnaOptions')
1417
const luParser = require('./../parser/lufile/luParser')
1518
const LUSectionTypes = require('./../parser/utils/enums/lusectiontypes')
1619
const globby = require('globby')
@@ -211,10 +214,15 @@ async function getConfigFile(input: string): Promise<string> {
211214
return defaultConfigFile
212215
}
213216

214-
export function getParsedObjects(contents: {id: string, content: string}[]) {
217+
export function getParsedObjects(contents: {id: string, content: string}[], extType: string) {
215218
const parsedObjects = contents.map(content => {
216-
const opts = new LUOptions(content.id)
217-
return new Lu(content.content, opts)
219+
if (extType === fileExtEnum.LUFile) {
220+
const opts = new LUOptions(content.id)
221+
return new Lu(content.content, opts)
222+
} else {
223+
const opts = new QnAOptions(content.id)
224+
return new QnA(content.content, opts)
225+
}
218226
})
219227

220228
return parsedObjects

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,4 +944,52 @@ describe('luis:cross training tests among lu and qna contents', () => {
944944
assert.equal(qnaResult.get(path.join(rootDir, "dia1.en-us")).Sections.filter(s => s.SectionType === sectionTypes.QNASECTION).length, 3)
945945
assert.isTrue(qnaResult.get(path.join(rootDir, "dia1.en-us")).Sections.filter(s => s.SectionType === sectionTypes.QNASECTION)[1].Body.includes(`# ? common_question${NEWLINE}${NEWLINE}**Filters:**${NEWLINE}- dialogName=dia1${NEWLINE}${NEWLINE}\`\`\`${NEWLINE}this is common answer${NEWLINE}\`\`\`${NEWLINE}${NEWLINE}`))
946946
})
947+
948+
it('luis:cross training can get expected result when a lu file is section emtpy', async () => {
949+
let luContentArray = []
950+
let qnaContentArray = []
951+
952+
luContentArray.push({
953+
content: `> this is comments
954+
# dia1_trigger
955+
- trigger dial1`,
956+
id: 'main'})
957+
958+
qnaContentArray.push({
959+
content:
960+
`# ?user guide
961+
962+
**Filters:**
963+
- aa=bb
964+
965+
\`\`\`
966+
Here is the [user guide](http://contoso.com/userguide.pdf)
967+
\`\`\`
968+
969+
# ?tell joke
970+
\`\`\`
971+
tell a funny joke
972+
\`\`\``,
973+
id: 'main'}
974+
)
975+
976+
luContentArray.push({
977+
content: `> !# @app.name = my luis application`,
978+
id: 'dia1'}
979+
)
980+
981+
let crossTrainConfig = {
982+
'main': {
983+
'rootDialog': true,
984+
'triggers': {
985+
'dia1_trigger': 'dia1'
986+
}
987+
}
988+
}
989+
990+
const trainedResult = await crossTrainer.crossTrain(luContentArray, qnaContentArray, crossTrainConfig)
991+
const luResult = trainedResult.luResult
992+
993+
assert.equal(luResult.get('dia1').Sections.filter(s => s.SectionType !== sectionTypes.MODELINFOSECTION).length, 0)
994+
})
947995
})

0 commit comments

Comments
 (0)