Repository: alexa-js/alexa-app
Pull request #352 (2018-05-19 10:21:21)

allow utterance expansion in custom slot type synonyms


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 288331a..1f6b8c7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 ### 4.2.3 (Next)
 
 * [#344](https://github.com/alexa-js/alexa-app/pull/344): Add coveralls as a check for pull requests - [@kobim](https://github.com/kobim).
+* [#352](https://github.com/alexa-js/alexa-app/pull/352): Allow utterance expansion in custom slot type synonyms - [@daanzu](https://github.com/daanzu).
 * Your contribution here.
 
 ### 4.2.2 (April 7, 2018)
diff --git a/README.md b/README.md
index 2e26462..d77bc99 100644
--- a/README.md
+++ b/README.md
@@ -662,7 +662,7 @@ Note that the "CustomSlotType" type values must be specified in the Skill Interf
 
 #### custom slot type values
 
-If you have custom slot types, you can define your custom slot type values as well. Custom values can either be simple strings, or more full-fledged objects if you want to take advantage of Skill Builder features like synonyms.
+If you have custom slot types, you can define your custom slot type values as well. Custom values can either be simple strings, or more full-fledged objects if you want to take advantage of Skill Builder features like synonyms. If using synonyms, you can also take advantage of utterance expansion from alexa-utterances (including dictionary), as described below.
 
 ```javascript
 testApp.customSlot("animal", ["cat", "dog"]);
@@ -674,7 +674,7 @@ OR
 testApp.customSlot("animal", [{
   value: "dog",
   id: "canine",
-  synonyms: ["doggo", "pupper", "woofmeister"]
+  synonyms: ["doggo", "pup{per|}", "woofmeister"]
 }]);
 ```
 
diff --git a/index.js b/index.js
index 0f51639..84745a0 100644
--- a/index.js
+++ b/index.js
@@ -480,13 +480,22 @@ alexa.app = function(name) {
           synonyms: []
         };
       } else {
-        if (!value.id) {
-          value.id = null;
-        }
-        if (!value.synonyms) {
-          value.synonyms = [];
+        valueObj = {
+          value: value.value,
+          id: value.id || null,
+          synonyms: []
+        };
+        if (value.synonyms) {
+          value.synonyms.forEach(function(sample) {
+            var list = AlexaUtterances(sample,
+              null,
+              self.dictionary,
+              self.exhaustiveUtterances);
+            list.forEach(function(utterance) {
+              valueObj.synonyms.push(utterance);
+            });
+          });
         }
-        valueObj = value;
       }
       self.customSlots[slotName].push(valueObj);
     });


Test patch:
diff --git a/test/test_alexa_app_customslot.js b/test/test_alexa_app_customslot.js
index 2d78224..6ed1a34 100644
--- a/test/test_alexa_app_customslot.js
+++ b/test/test_alexa_app_customslot.js
@@ -44,7 +44,12 @@ describe("Alexa", function() {
     context("#customslot", function() {
       var slotValue = {
         value: "dog",
-        synonyms: ["pupper, doggo"],
+        synonyms: ["doggo", "pup{per|}"],
+        id: "dog"
+      };
+      var slotValueExpanded = {
+        value: "dog",
+        synonyms: ["doggo", "pupper", "pup"],
         id: "dog"
       };
 
@@ -54,7 +59,7 @@ describe("Alexa", function() {
         });
 
         it("adds the slot value", function() {
-          expect(testApp.customSlots["animals"]).to.eql([slotValue]);
+          expect(testApp.customSlots["animals"]).to.eql([slotValueExpanded]);
         });
       });
 
@@ -98,7 +103,7 @@ describe("Alexa", function() {
             value: "dog",
             synonyms: [],
             id: null
-          }, slotValue]);
+          }, slotValueExpanded]);
         });
       });
     });