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

Commit 3810357

Browse files
authored
Fix for #122 - luis:convert fails on valid patterns (#143)
* spec * Fix for #122 * Fix up * updates
1 parent 27a2133 commit 3810357

File tree

8 files changed

+80
-44
lines changed

8 files changed

+80
-44
lines changed

packages/luis/parser/converters/lumerger.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@ const resolveReferencesInUtterances = async function(allParsedContent) {
161161
let newPatternsToAdd = [];
162162
let spliceList = [];
163163
(luisModel.LUISJsonStructure.utterances || []).forEach((utterance,idx) => {
164-
// Deep references must have [link name](link-value) notation
165-
if (utterance.text.indexOf('[') !== 0) return;
166-
// does this utterance have a deep link uri?
167-
let linkExp = (utterance.text || '').trim().match(new RegExp(/\(.*?\)/g));
168-
if (linkExp && linkExp.length !== 0) {
164+
// Fix for BF-CLI #122.
165+
// Ensure only links are detected and passed on to be parsed.
166+
if (helpers.isUtteranceLinkRef(utterance.text || '')) {
169167
// we have stuff to parse and resolve
170168
let parsedUtterance = helpers.parseLinkURI(utterance.text);
171169
if (!path.isAbsolute(parsedUtterance.luFile)) parsedUtterance.luFile = path.resolve(path.dirname(luisModel.srcFile), parsedUtterance.luFile);

packages/luis/parser/lufile/helpers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ const helpers = {
116116
returnValue.roles = parsedRoleDefinition.replace('[', '').replace(']', '').split(RolesSplitRegEx).map(item => item.trim());
117117
}
118118
return returnValue;
119+
},
120+
/**
121+
* Helper function to detect if a given text is a link reference
122+
* @param {String} utterance utterance text to examine
123+
* @returns {Boolean} true if input is a link reference
124+
*/
125+
isUtteranceLinkRef : function (utterance) {
126+
utterance = utterance || '';
127+
// Ensure only links are detected and passed on to be parsed.
128+
// Valid link: [bar](xyz)
129+
// Not a link: [bar](xyz|123), [bar[tar]](xyz), abc [foo](bar)
130+
let linkDetectRegex = /^\[[^\[]+\]\([^|]+\)$/gi;
131+
return linkDetectRegex.test(utterance);
132+
},
133+
/**
134+
* Helper function to detect if a given text is a pattern.
135+
* @param {String} utterance
136+
* @returns {Boolean} true if utterance is a pattern
137+
*/
138+
isUtterancePattern : function (utterance) {
139+
utterance = utterance || '';
140+
// link references cannot be a pattern
141+
if (this.isUtteranceLinkRef(utterance)) return false;
142+
143+
// patterns must have at least one [optional] and or one (group | text)
144+
let detectPatternRegex = /(\[.*?\])|(\(.*?(\|.*?)+\))/gi;
145+
return detectPatternRegex.test(utterance);
119146
}
120147
}
121148

packages/luis/parser/lufile/parseFileContents.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,12 @@ const parseAndHandleIntent = function (parsedContent, luResource) {
208208
for (const utteranceAndEntities of intent.UtteranceAndEntitiesMap) {
209209
// add utterance
210210
let utterance = utteranceAndEntities.utterance.trim();
211-
if (utterance.indexOf('[') == 0) {
212-
let linkExp = (utterance || '').trim().match(new RegExp(/\(.*?\)/g));
213-
if (linkExp && linkExp.length === 1) {
214-
let parsedLinkUriInUtterance = helpers.parseLinkURI(utterance);
215-
// examine and add these to filestoparse list.
216-
parsedContent.additionalFilesToParse.push(new fileToParse(parsedLinkUriInUtterance.luFile, false));
217-
}
211+
// Fix for BF-CLI #122.
212+
// Ensure only links are detected and passed on to be parsed.
213+
if (helpers.isUtteranceLinkRef(utterance || '')) {
214+
let parsedLinkUriInUtterance = helpers.parseLinkURI(utterance);
215+
// examine and add these to filestoparse list.
216+
parsedContent.additionalFilesToParse.push(new fileToParse(parsedLinkUriInUtterance.luFile, false));
218217
}
219218

220219
if (utteranceAndEntities.entities.length > 0) {
@@ -387,8 +386,14 @@ const parseAndHandleIntent = function (parsedContent, luResource) {
387386
}
388387

389388
} else {
390-
let utteranceObject = new helperClass.uttereances(utterance, intentName, []);
391-
parsedContent.LUISJsonStructure.utterances.push(utteranceObject);
389+
// detect if utterance is a pattern and if so add it as a pattern
390+
if (helpers.isUtterancePattern(utterance)) {
391+
let patternObject = new helperClass.pattern(utterance, intentName);
392+
parsedContent.LUISJsonStructure.patterns.push(patternObject);
393+
} else {
394+
let utteranceObject = new helperClass.uttereances(utterance, intentName, []);
395+
parsedContent.LUISJsonStructure.utterances.push(utteranceObject);
396+
}
392397
}
393398
}
394399
}

packages/luis/test/fixtures/verified/Skills/Calendar.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14434,11 +14434,6 @@
1443414434
}
1443514435
]
1443614436
},
14437-
{
14438-
"text": "^[(show|display)] next [page] [please]^",
14439-
"intent": "ShowNextCalendar",
14440-
"entities": []
14441-
},
1444214437
{
1444314438
"text": "bring the previous one",
1444414439
"intent": "ShowPreviousCalendar",
@@ -17620,6 +17615,10 @@
1762017615
"pattern": "^is (this|the|that) (meeting|conference) room (available|booked) [(at|on|for)] [{FromDate}] [(at|on|for)] [{FromTime}]^",
1762117616
"intent": "FindMeetingRoom"
1762217617
},
17618+
{
17619+
"pattern": "^[(show|display)] next [page] [please]^",
17620+
"intent": "ShowNextCalendar"
17621+
},
1762317622
{
1762417623
"pattern": "^i (want to|need to|would like to) (attend|join) {Subject}, [please] add it.^",
1762517624
"intent": "ShowNextCalendar"

packages/luis/test/fixtures/verified/Skills/Email.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5981,11 +5981,6 @@
59815981
}
59825982
]
59835983
},
5984-
{
5985-
"text": "^(show|give|tell) [me] [the] next (email|message|mail)^",
5986-
"intent": "ShowNext",
5987-
"entities": []
5988-
},
59895984
{
59905985
"text": "back to the last one from apple",
59915986
"intent": "ShowPrevious",
@@ -6182,11 +6177,6 @@
61826177
}
61836178
]
61846179
},
6185-
{
6186-
"text": "^(show|give|tell) [me] [the] previous (email|message|mail)^",
6187-
"intent": "ShowPrevious",
6188-
"entities": []
6189-
},
61906180
{
61916181
"text": "cancel",
61926182
"intent": "Cancel",
@@ -8905,6 +8895,14 @@
89058895
"pattern": "^(write|send|start) [(a|an|the)] [new] email to {ContactName} (about|on|with) [the subject] [that] {EmailSubject}^",
89068896
"intent": "SendEmail"
89078897
},
8898+
{
8899+
"pattern": "^(show|give|tell) [me] [the] next (email|message|mail)^",
8900+
"intent": "ShowNext"
8901+
},
8902+
{
8903+
"pattern": "^(show|give|tell) [me] [the] previous (email|message|mail)^",
8904+
"intent": "ShowPrevious"
8905+
},
89088906
{
89098907
"pattern": "^(yes|sure)[[,]i want (that|the ({DirectionalReference}|{ordinal}) ({number}|one))]",
89108908
"intent": "Confirm"

packages/luis/test/fixtures/verified/Skills/ToDo.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4510,11 +4510,6 @@
45104510
"intent": "Cancel",
45114511
"entities": []
45124512
},
4513-
{
4514-
"text": "^[(would you|could you|can you)] [please] cancel (previous|current|this) (step|operation)",
4515-
"intent": "Cancel",
4516-
"entities": []
4517-
},
45184513
{
45194514
"text": "confirm all of them",
45204515
"intent": "Confirm",
@@ -6100,11 +6095,6 @@
61006095
"intent": "ShowPreviousPage",
61016096
"entities": []
61026097
},
6103-
{
6104-
"text": "^[(can|could|would)] [you] [please] [(show|display|go back to)][the](first|previous)page",
6105-
"intent": "ShowPreviousPage",
6106-
"entities": []
6107-
},
61086098
{
61096099
"text": "access my shopping list",
61106100
"intent": "ShowToDo",
@@ -6837,6 +6827,10 @@
68376827
"pattern": "^[please] (add|put|append) [(items|item)] {FoodOfGrocery} [and] [{FoodOfGrocery}](on|to|in)[(my|the)]{ListType}list",
68386828
"intent": "AddToDo"
68396829
},
6830+
{
6831+
"pattern": "^[(would you|could you|can you)] [please] cancel (previous|current|this) (step|operation)",
6832+
"intent": "Cancel"
6833+
},
68406834
{
68416835
"pattern": "^(yes|ok|sure), [please][(add|delete|update)] [the] [(item|task|todo item)][(of|about|that)]{TaskContent}",
68426836
"intent": "Confirm"
@@ -6845,6 +6839,10 @@
68456839
"pattern": "^[task][(done|finished|completed)]{TaskContent},[please][(check off|mark it)][as][(done|finished|complete)]",
68466840
"intent": "MarkToDo"
68476841
},
6842+
{
6843+
"pattern": "^[(can|could|would)] [you] [please] [(show|display|go back to)][the](first|previous)page",
6844+
"intent": "ShowPreviousPage"
6845+
},
68486846
{
68496847
"pattern": "^(what|anything)[do] i (need|should|have to) (buy|shop|purchase) for {TaskContent}",
68506848
"intent": "ShowToDo"

packages/luis/test/fixtures/verified/calendar_all_prebuilt_parsed.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12496,11 +12496,6 @@
1249612496
"intent": "Calendar.ShowNext",
1249712497
"entities": []
1249812498
},
12499-
{
12500-
"text": "^[(show|display)] next [page] [please]^",
12501-
"intent": "Calendar.ShowNext",
12502-
"entities": []
12503-
},
1250412499
{
1250512500
"text": "what is the previous",
1250612501
"intent": "Calendar.ShowPrevious",
@@ -13023,6 +13018,10 @@
1302313018
"pattern": "^(remind|inform|tell) (the|all) attendees {Calendar.Message}^",
1302413019
"intent": "Calendar.ContactMeetingAttendees"
1302513020
},
13021+
{
13022+
"pattern": "^[(show|display)] next [page] [please]^",
13023+
"intent": "Calendar.ShowNext"
13024+
},
1302613025
{
1302713026
"pattern": "^i (want to|need to|would like to) (attend|join) {Calendar.Subject}, [please] add it.^",
1302813027
"intent": "Calendar.ShowNext"

packages/luis/test/parser/lufile/parseFileContents.parseFile.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,4 +970,16 @@ describe('parseFile correctly parses utterances', function () {
970970
.catch(err => done())
971971
})
972972

973+
it ('Test for BF CLI #122', function(done){
974+
let testLU = `# intent1
975+
- [[this]is] a new form (a | b)`;
976+
977+
parseFile.parseFile(testLU)
978+
.then(res => {
979+
assert.equal(res.LUISJsonStructure.patterns.length, 1);
980+
assert.equal(res.LUISJsonStructure.patterns[0].pattern, '[[this]is] a new form (a | b)');
981+
done();
982+
})
983+
.catch(err => done('Fail! Did not throw when expected'))
984+
})
973985
})

0 commit comments

Comments
 (0)