Alexa - Using inputMap with synonyms

amazon-alexa

#1

I have a question about the inputMap feature:

I’ve read the docs at https://www.jovo.tech/docs/routing/input#inputmap but am still unsure if this is releveant for what I want to do.

I have a situation where I will play back a video from a specific URL, if the device supports video playback. The URL I choose will depend on the input from a user.

For example, let’s say I ask a user, “What’s your favourite drink?”:

// app.js:
 
FavouriteDrinkIntent() {
  
  let drinkType = this.$inputs.drinkType.value;
      
  const videoUrlOne = "https://www.example.com/one.mp4";  
  const videoUrlTwo = "https://www.example.com/two.mp4";  
  const videoUrlThree = "https://www.example.com/three.mp4";  

  if (drinkType === "coke") {

    let videoSourceURL = videoUrlOne;
    let videoSourceTitle = "A video about " + drinkType; 
    let videoSourceSubtitle = "Example Subtitle";

    this.$alexaSkill.showVideo(videoSourceURL, videoSourceTitle, videoSourceSubtitle);
      
  }

  ... code ...

}

This works perfectly if the users says, “coke” as their favourite drink. But what if they have used a synonym, like this?:

// models/en-GB.json:

"inputTypes": [
        {
            "name": "myExampleAppDrinkType",
            "values": [                
                {
                    "value": "coke",
                    "synonyms": [
                        "cola",
                        "coca cola"
                    ]
                }
            ]
        }
]

In my experience so far, if the user was to say “cola” as a synonym for “coke”, the video won’t be lauched because it won’t meet the conditions in the if statement above. Is there a way for me to use the inputMap feature in src/config.js to allow for the synonyms to be associated with the value?

I could just do this:

if (drinkType === "coke" || drinkType === "cola" || drinkType === "coca cola") {
    ... code ...
}

But that would be a LOT of repetition.

In the docs, there’s a reference to using key with synonyms but there isn’t much explanation:

The $inputs object:

{
  name: 'inputName',
  value: 'inputValue',
  key: 'mappedInputValue', // may differ from value if synonyms are used in language model
}

Also, in src/config.js:

module.exports = {

    inputMap: {
        'given-name' : 'name',
    },

    // ...

};

Any help on this would be much appreciated. I hope I’ve explained myself clearly enough, let me know if I haven’t.

Regards,
Simon


#2

Hi Simon,

I asked a similar question recently regarding synonyms and the answer was accessing the slot value like this:

this.$inputs.inputName.key

// Your example
this.$inputs.drinkType.key

So then you could use that in your if statements like:

if (this.$inputs.drinkType.key === "coke") {
    ... code ...
}

You can read the full explanation here: How to resolve synonyms

I hope it helps!


#3

Thanks Porterhaus, I will check that out, and give it a try!