Search code examples
ibm-watsonwatson-conversation

How to sample random numbers in Watson Assistant?


I'm using IBM Watson Assistant for creating a chatbot. I'm using the web interface with the intents, entities and dialog flow|tree (I don't know how it is called, I'm just calling it web interface).

I would like to define an array of the numbers [1,2,3,4,5]. Then one node should sample a random number without replacement from that array (e.g. 2), i.e. the remaining array is then [1,3,4,5]. After some time another node should pick another number at random from the array (say 4). And so on. How can this be implemented? I know about variables (e.g. $var) but I don't know how to represent arrays and sample random numbers.

Thank you so much for your answers in advance. And happy new year to everybody.


Solution

  • As a general rule about what might be possible we use the doc for the Spring Expression Language. Based on that, you could select a value from some data structure $some_array like this:

    {
     "context": {
     "randomNumber": "<?  (new java.util.Random().nextInt($some_array.size())) ?>"
     }
    }
    


    After that, you access your value using

    {
     "context": {
      "element": "<?  $some_array.get($randomNumber)  ?>",
      "shorter_array": "<?  $some_array.remove($randomNumber)  ?>"
     }
    }
    

    Now there are some issues (probably the bug mentioned in the comment), as the remove method should be the one from the gson JsonArray and behave like this:

    Removes the element at the specified position in this array. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the array.

    However, it returns the array with the element removed, not the element itself.

    1) In your Dialog Node, define slots slots 2) In the first one, generate the random value (if array defined and not empty) enter image description here 3) In the second, get the element and remove enter image description here

    Note, that even though this works, you should probably not use it in a production version of your Chatbot. Instead, put most of the logic in your application (including the control of the dialogue). The Web Interface is very difficult to test and the way the variables are parsed might change at some point.