From fbe64eb7b9da771c56006ec1d002b8f5b10e9a0d Mon Sep 17 00:00:00 2001 From: dominicankev Date: Wed, 18 Jan 2017 09:24:13 -0500 Subject: [PATCH 1/4] Added code to make slots optional --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 4888873..25f88d3 100644 --- a/index.js +++ b/index.js @@ -157,6 +157,8 @@ function generateUtterances(str, slots, dictionary, exhaustiveUtterances) { // Convert all {-|Name} to {Name} to accomodate slot literals for (var idx in utterances) { utterances[idx] = utterances[idx].replace(/\{\-\|/g, "{"); + utterances[idx] = utterances[idx].replace(/\+(.*?)/, "{"); + utterances[idx] = utterances[idx].replace(/\+$/, "}"); } return utterances; From ed7df41d0936f115c884096ebef07df6e424bf34 Mon Sep 17 00:00:00 2001 From: dominicankev Date: Wed, 18 Jan 2017 09:24:13 -0500 Subject: [PATCH 2/4] Added code to make slots optional --- CHANGELOG.md | 4 ++-- README.md | 13 +++++++++++++ index.js | 2 ++ test/index.js | 16 ++++++++++------ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84525e3..ff6be09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ -### Next +### 0.2.2 -* Your contribution here. +* Added ability to make slots optional by adding "+" before and after slot name (example: {+SLOTNAME+|}) - [@dominicankev] (https://github.com/dominicankev). ### 0.2.1 diff --git a/README.md b/README.md index daa3746..d0f9711 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,19 @@ You may want to work with [Custom Slot Types](https://developer.amazon.com/appsa "your least favorite snack is {Fruit}" ``` +You can use a special syntax to leave a curly-braced slot name unparsed and make it optional. For example, if you have defined in your skill a `ROOM_NAME` with the values `Bedroom`, `Office` and `Living Room` for the slot `Room_Name`, you can keep `Room_Name` a curly-braced literal and optional as follows + +```javascript +"change channel {to|} {-|ChannelNumber} {in +ROOM_NAME+|+ROOM_NAME+|}" +=> +"change channel to {ChannelNumber} in {ROOM_NAME}" +"change channel {ChannelNumber} in {ROOM_NAME}" +"change channel to {ChannelNumber} {ROOM_NAME}" +"change channel {ChannelNumber} {ROOM_NAME}" +"change channel to {ChannelNumber} " +"change channel {ChannelNumber} " +``` + ### Contributing See [CONTRIBUTING](CONTRIBUTING.md) diff --git a/index.js b/index.js index 4888873..25f88d3 100644 --- a/index.js +++ b/index.js @@ -157,6 +157,8 @@ function generateUtterances(str, slots, dictionary, exhaustiveUtterances) { // Convert all {-|Name} to {Name} to accomodate slot literals for (var idx in utterances) { utterances[idx] = utterances[idx].replace(/\{\-\|/g, "{"); + utterances[idx] = utterances[idx].replace(/\+(.*?)/, "{"); + utterances[idx] = utterances[idx].replace(/\+$/, "}"); } return utterances; diff --git a/test/index.js b/test/index.js index 2e905b3..b1da6ef 100644 --- a/test/index.js +++ b/test/index.js @@ -111,15 +111,19 @@ test('exhaustive vs non-exhaustive expansion', function (t) { test('raw curly braces for custom slot types', function (t) { var dictionary = {}; - var slots = {"Artist": "CUSTOM_TYPE"}; - var template = "{my|your} {favorite|least favorite} fruit is {-|Fruit}"; + var slots = {"Artist": "CUSTOM_TYPE","ROOM_NAME": "AMAZON.Room"}; + var template = "{my|your} {favorite|least favorite} fruit is {-|Fruit} {in +ROOM_NAME+|}"; var result = utterances(template, slots, dictionary); t.deepEqual(result, [ - "my favorite fruit is {Fruit}", - "your favorite fruit is {Fruit}", - "my least favorite fruit is {Fruit}", - "your least favorite fruit is {Fruit}" + "my favorite fruit is {Fruit} in {ROOM_NAME}", + "your favorite fruit is {Fruit} in {ROOM_NAME}", + "my least favorite fruit is {Fruit} in {ROOM_NAME}", + "your least favorite fruit is {Fruit} in {ROOM_NAME}", + "my favorite fruit is {Fruit} ", + "your favorite fruit is {Fruit} ", + "my least favorite fruit is {Fruit} ", + "your least favorite fruit is {Fruit} " ]); t.end(); }); From b97559e406a83c3199b81882880d30d3eb05e2bd Mon Sep 17 00:00:00 2001 From: dominicankev Date: Wed, 1 Feb 2017 22:16:34 -0500 Subject: [PATCH 3/4] Fix issue with multiple optional slots in utterances --- index.js | 16 +++++++++++----- test/index.js | 28 ++++++++++++++++++---------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index a17756e..234b73d 100644 --- a/index.js +++ b/index.js @@ -154,16 +154,22 @@ function generateUtterances(str, slots, dictionary, exhaustiveUtterances) { utterances = [str]; } - // Convert all {-|Name} to {Name} to accomodate slot literals + // Convert all {-|Name} to {Name} and +Name+ to {Name} to accomodate slot literals and optional slot literals for (var idx in utterances) { utterances[idx] = utterances[idx].replace(/\{\-\|/g, "{"); - utterances[idx] = utterances[idx].replace(/\+(.*?)/, "{"); - utterances[idx] = utterances[idx].replace(/\+$/, "}"); + var strArray = utterances[idx].split(" "); + var strTemp = ""; + var strTemp2 = ""; + utterances[idx] = strArray.forEach(function(item){ + strTemp = item.replace(/\+(.*?)/, "{"); + strTemp = strTemp.replace(/\+$/, "}"); + strTemp2 += " " + strTemp; + }); + utterances[idx] = strTemp2; utterances[idx] = utterances[idx].trim(); } - + return utterances; } - module.exports = generateUtterances; diff --git a/test/index.js b/test/index.js index 01c9be3..975c512 100644 --- a/test/index.js +++ b/test/index.js @@ -126,19 +126,27 @@ test('raw curly braces for custom slot types', function (t) { test('optional custom slot types', function (t) { var dictionary = {}; - var slots = {"Artist": "CUSTOM_TYPE","ROOM_NAME": "AMAZON.Room"}; - var template = "{my|your} {favorite|least favorite} fruit is {-|Fruit} {in +ROOM_NAME+|}"; + var slots = {"FRUIT": "CUSTOM_TYPE","COLOR": "CUSTOM_TYPE","ROOM_NAME": "AMAZON.Room"}; + var template = "{my|your} {favorite|least favorite} thing is {+FRUIT+|+COLOR+} {in +ROOM_NAME+|}"; var result = utterances(template, slots, dictionary); t.deepEqual(result, [ - "my favorite fruit is {Fruit} in {ROOM_NAME}", - "your favorite fruit is {Fruit} in {ROOM_NAME}", - "my least favorite fruit is {Fruit} in {ROOM_NAME}", - "your least favorite fruit is {Fruit} in {ROOM_NAME}", - "my favorite fruit is {Fruit}", - "your favorite fruit is {Fruit}", - "my least favorite fruit is {Fruit}", - "your least favorite fruit is {Fruit}" + "my favorite thing is {FRUIT} in {ROOM_NAME}", + "your favorite thing is {FRUIT} in {ROOM_NAME}", + "my least favorite thing is {FRUIT} in {ROOM_NAME}", + "your least favorite thing is {FRUIT} in {ROOM_NAME}", + "my favorite thing is {COLOR} in {ROOM_NAME}", + "your favorite thing is {COLOR} in {ROOM_NAME}", + "my least favorite thing is {COLOR} in {ROOM_NAME}", + "your least favorite thing is {COLOR} in {ROOM_NAME}", + "my favorite thing is {FRUIT}", + "your favorite thing is {FRUIT}", + "my least favorite thing is {FRUIT}", + "your least favorite thing is {FRUIT}", + "my favorite thing is {COLOR}", + "your favorite thing is {COLOR}", + "my least favorite thing is {COLOR}", + "your least favorite thing is {COLOR}" ]); t.end(); }); From dbc7bd57a1b9606b4a346744eae7e238beaadc21 Mon Sep 17 00:00:00 2001 From: dominicankev Date: Thu, 2 Feb 2017 10:23:48 -0500 Subject: [PATCH 4/4] Simply fixed spacing issue --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 234b73d..3f48af1 100644 --- a/index.js +++ b/index.js @@ -163,7 +163,7 @@ function generateUtterances(str, slots, dictionary, exhaustiveUtterances) { utterances[idx] = strArray.forEach(function(item){ strTemp = item.replace(/\+(.*?)/, "{"); strTemp = strTemp.replace(/\+$/, "}"); - strTemp2 += " " + strTemp; + strTemp2 += " " + strTemp; }); utterances[idx] = strTemp2; utterances[idx] = utterances[idx].trim();