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
137 changes: 88 additions & 49 deletions packages/lu/src/parser/lufile/luParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,62 +27,101 @@ class LUParser {
let sections = [];
let content = text;

let { fileContent, errors } = this.getFileContent(text);
if (errors.length > 0) {
return new LUResource(sections, content, errors);
let {fileContent, errors} = this.getFileContent(text);

try {
let modelInfoSections = this.extractModelInfoSections(fileContent);
modelInfoSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(modelInfoSections);
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing model information: ${err.message}`
}))
}

let modelInfoSections = this.extractModelInfoSections(fileContent);
modelInfoSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(modelInfoSections);

let isSectionEnabled = this.isSectionEnabled(sections);

let nestedIntentSections = this.extractNestedIntentSections(fileContent, content);
nestedIntentSections.forEach(section => errors = errors.concat(section.Errors));
if (isSectionEnabled) {
sections = sections.concat(nestedIntentSections);
} else {
nestedIntentSections.forEach(section => {
let emptyIntentSection = new SimpleIntentSection();
emptyIntentSection.ParseTree = section.ParseTree.nestedIntentNameLine();
emptyIntentSection.Name = section.Name;
let errorMsg = `no utterances found for intent definition: "# ${emptyIntentSection.Name}"`
let error = BuildDiagnostic({
message: errorMsg,
context: emptyIntentSection.ParseTree,
severity: DiagnosticSeverity.WARN
})

errors.push(error);
sections.push(emptyIntentSection);

section.SimpleIntentSections.forEach(subSection => {
sections.push(subSection);
errors = errors.concat(subSection.Errors);
})
});
try {
let isSectionEnabled = this.isSectionEnabled(sections);

let nestedIntentSections = this.extractNestedIntentSections(fileContent, content);
nestedIntentSections.forEach(section => errors = errors.concat(section.Errors));
if (isSectionEnabled) {
sections = sections.concat(nestedIntentSections);
} else {
nestedIntentSections.forEach(section => {
let emptyIntentSection = new SimpleIntentSection();
emptyIntentSection.ParseTree = section.ParseTree.nestedIntentNameLine();
emptyIntentSection.Name = section.Name;
let errorMsg = `no utterances found for intent definition: "# ${emptyIntentSection.Name}"`
let error = BuildDiagnostic({
message: errorMsg,
context: emptyIntentSection.ParseTree,
severity: DiagnosticSeverity.WARN
})

errors.push(error);
sections.push(emptyIntentSection);

section.SimpleIntentSections.forEach(subSection => {
sections.push(subSection);
errors = errors.concat(subSection.Errors);
})
});
}
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing nested intent section: ${err.message}`
}))
}

try {
let simpleIntentSections = this.extractSimpleIntentSections(fileContent, content);
simpleIntentSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(simpleIntentSections);
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing simple intent section: ${err.message}`
}))
}

let simpleIntentSections = this.extractSimpleIntentSections(fileContent, content);
simpleIntentSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(simpleIntentSections);
try {
let entitySections = this.extractEntitiesSections(fileContent);
entitySections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(entitySections);
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing entities: ${err.message}`
}))
}

let entitySections = this.extractEntitiesSections(fileContent);
entitySections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(entitySections);
try {
let newEntitySections = this.extractNewEntitiesSections(fileContent);
newEntitySections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(newEntitySections);
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing new entities: ${err.message}`
}))
}

let newEntitySections = this.extractNewEntitiesSections(fileContent);
newEntitySections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(newEntitySections);
try {
let importSections = this.extractImportSections(fileContent);
importSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(importSections);
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing import section: ${err.message}`
}))
}

let importSections = this.extractImportSections(fileContent);
importSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(importSections);

let qnaSections = this.extractQnaSections(fileContent);
qnaSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(qnaSections);
try {
let qnaSections = this.extractQnaSections(fileContent);
qnaSections.forEach(section => errors = errors.concat(section.Errors));
sections = sections.concat(qnaSections);
} catch (err) {
errors.push(BuildDiagnostic({
message: `Error happened when parsing qna section: ${err.message}`
}))
}

return new LUResource(sections, content, errors);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/lu/test/parser/lufile/sectionapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,23 @@ describe('Section CRUD test', () => {
assert.equal(luresource.Sections[2].SimpleIntentSections[1].Entities[0].Type, 'simple');
assert.equal(luresource.Sections[2].SimpleIntentSections[1].UtteranceAndEntitiesMap[0].utterance, 'check my deleted todo');
});

it('update section test for invalid content', () => {

// missing '-' before utterance hello
let newFileConent = `# Greeting${NEWLINE}- hi${NEWLINE}hello`;

let sectionId = luresource.Sections[3].Id;
luresource = new SectionOperator(luresource).updateSection(luresource.Sections[3].Id, newFileConent);

assert.equal(luresource.Errors.length, 1);
assert.equal(luresource.Sections.length, 4);
assert.equal(luresource.Sections[3].Errors.length, 1);
assert.equal(luresource.Sections[3].SectionType, LUSectionTypes.SIMPLEINTENTSECTION);
assert.equal(luresource.Sections[3].Name, 'Greeting');
assert.equal(luresource.Sections[3].Id, sectionId);
assert.equal(luresource.Sections[3].UtteranceAndEntitiesMap.length, 1);
assert.equal(luresource.Sections[3].UtteranceAndEntitiesMap[0].utterance, 'hi');
assert.equal(luresource.Sections[3].Body, `- hi${NEWLINE}hello`);
});
});