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

Added code to make slots optional #24

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

* 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

* Updated tap module to `6.1.1` - [@mreinstein](https://github.com/mreinstein).
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +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, "{");
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;
29 changes: 28 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('optional terms', function (t) {
var template = 'do {it |}';

var result = utterances(template, slots, dictionary);
t.deepEqual(result, [ 'do it ', 'do ' ]);
t.deepEqual(result, [ 'do it', 'do' ]);
t.end();
});

Expand Down Expand Up @@ -123,3 +123,30 @@ test('raw curly braces for custom slot types', function (t) {
]);
t.end();
});

test('optional custom slot types', function (t) {
var dictionary = {};
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 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();
});