Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add escape brackets in utterance capability #1104

Merged
merged 2 commits into from
Feb 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/lu/src/parser/lufile/visitor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const lp = require('./generated/LUFileParser').LUFileParser;
const LUISObjNameEnum = require('./../utils/enums/luisobjenum');
const InvalidCharsInIntentOrEntityName = require('./../utils/enums/invalidchars').InvalidCharsInIntentOrEntityName;
const EscapeCharsInUtterance = require('./../utils/enums/escapechars').EscapeCharsInUtterance;

class Visitor {
/**
Expand All @@ -20,6 +21,12 @@ class Visitor {
utterance = this.recurselyResolveTokenizedUtterance(tokUtt, entities, errorMsgs, utterance.trimLeft());
break;
}
case lp.ESCAPE_CHARACTER: {
let escapeCharacters = innerNode.getText();
let escapedUtterace = escapeCharacters.length > 1 && EscapeCharsInUtterance.includes(escapeCharacters[1]) ? escapeCharacters.slice(1) : escapeCharacters;
utterance = utterance.concat(escapedUtterace);
break;
}
default: {
utterance = utterance.concat(innerNode.getText());
break;
Expand Down
8 changes: 8 additions & 0 deletions packages/lu/src/parser/utils/enums/escapechars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
// Escape chars in utterance
module.exports = {
EscapeCharsInUtterance: ['{', '}', '\\']
};
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ describe('parseFile correctly parses utterances', function () {
.then(res => {
assert.equal(res.LUISJsonStructure.patternAnyEntities.length, 0);
assert.equal(res.LUISJsonStructure.entities.length, 0);
assert.equal(res.LUISJsonStructure.utterances[0].text, 'this is a \\{test\\}');
assert.equal(res.LUISJsonStructure.utterances[0].text, 'this is a {test}');
assert.equal(res.LUISJsonStructure.utterances[1].text, 'this ia a test \\n');
done();
})
Expand Down Expand Up @@ -1112,4 +1112,39 @@ describe('parseFile correctly parses utterances', function () {
})
.catch(err => done(err))
})

it("Correctly parses utterance with escape char \\ to escape entity definition", function (done) {
let testLU = `
# test
- this is another \\{@from = one} from \\{@to = tokyo} \\in japan`;
parseFile
.parseFile(testLU)
.then((res) => {
assert.equal(
res.LUISJsonStructure.utterances[0].text,
"this is another {@from = one} from {@to = tokyo} \\in japan"
);
done();
})
.catch((err) => done(err));
});

it("Correctly parses utterance that keeps @ at the the beginning of entity name", function (done) {
let testLU = `
# test
- this is another \\\\{@@from = one} from {@@to = tokyo}`;
parseFile
.parseFile(testLU)
.then((res) => {
assert.equal(
res.LUISJsonStructure.utterances[0].text,
"this is another \\one from tokyo"
);
assert.equal(res.LUISJsonStructure.entities.length, 2);
assert.equal(res.LUISJsonStructure.entities[0].name, "@from");
assert.equal(res.LUISJsonStructure.entities[1].name, "@to");
done();
})
.catch((err) => done(err));
});
})