4.2. Advanced Interaction

Built-in Macros

Besides #ONT for checking an ontology, STDM provides several macros useful for dialogue design.

#LEM

The following example shows a use case of the macro #LEM that uses the NLTK Lemmatizer to match lemmas of "raining tacos":

transitions = {
    'state': 'start',
    '`What can I do for you?`': {
        '[play, [!{#LEM(rain), rainy}, #LEM(taco)]]': {
            'state': 'play_raining_tacos',
            '`It\'s raining tacos. From out of the sky ...`': 'start'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        },
    }
}
  • #5: maches the input with the lemma of "rain" or "rainy", then the lemma of "taco".

S: What can I do for you?
U: Play raining tacos
S: It's raining tacos. From out of the sky ... What can I do for you?
U: Play raining taco
S: It's raining tacos. From out of the sky ... What can I do for you?
U: Play rain taco
S: It's raining tacos. From out of the sky ... What can I do for you?
U: Play rainy taco
S: It's raining tacos. From out of the sky ... What can I do for you?
U: Bye
S: Sorry, I didn't understand you.

#UNX

The #UNX macro can be used as an alternative to the 'error' transition, which prepends a short acknowledgment phrase (e.g., "yeah", "sure") to the error statement:

  • #8: prepends an acknowledgment phrase to the error statement, "Thanks for sharing" in #8.

  • #3,5: add acknowledgment phrases given the user inputs.

  • #7: does not add an acknowledgment phrase when the user input is short.

When the user input contains fewer than three tokens, #UNK does not add any acknowledgment.

#SET & #IF

Let us update the above transitions so that it sometimes refuses to sing the same song:

  • #6: can be picked if the variable $RAINING_TACOS equals to the string 'True'.

  • #7: sets $RAINING_TACOS to 'True' after prompting the system output.

Once $RAINING_TACOS is set to 'True', STDM randomly picks a statement in #6 or #7 as the system output.

Notice that the #SET macro only assigns a string value to the variable. It is possible to write a macro to set any variable to an actual boolean value (e.g., True, False):

  • #3-4: checks if there are two arguments.

  • #6-8: retrieves the variable name.

  • #10-12: checks if the argument is a proper boolean value.

  • #14: stores the boolean value to the variable.

Given MacroSetBool, the above transitions can be updated as follow:

  • #6: is selected if $RAINING_TACOS is True.

  • #0: sets the variable $RAINING_TACOS to the boolean value True.

Currently, STDM randomly chooses the statements in #6 and #7 once $RAINING_TACOS becomes True. We can write another macro that prompts #7 only for the first time:

  • #3: the get() method in dict returns the value corresponding to the key, RAINING_TACOS if exists; otherwise, False.

Given MacroPlayRainingTacos, the transitions can be updated as follow:

  • #7: is selected if the macro #PLAY_RAINING_TACOS returns True.

  • #17: adds #PLAY_RAINING_TACOS to the macro dictionary.

Music

It is possible to make our system actually sings instead of prompting the lyric. Let us first install the VLC package:

Then, import the package and update the MacroPlayRainingTacos macro to play the MP3 file, resources/raining_tacos.mp3:

  • #1: imports the VLC package.

  • #6: creates a VLC media player and plays the specified MP3 file.

When you run the above dialogue flow, it now plays the MP3 file and prompts the lyric.

Time

The transitions in Section 4.1 prompt the same time (3PM) upon every request. Let us create a new macro that checks the current (system) time:

  • #1: imports the time package.

  • #5: retrieves the current time in the specified format using the strftime method.

  • #6: returns the current time using the str.format method.

The macro MacroTime can be called to generate the system output:

  • #11: calls the TIME macro to generate the system output displaying the current time.

  • #22: adds #TIME to the macro dictionary.

Weather

The transitions in Section 4.1 prompt the same weather (sunny) upon every request. Let us retrieve the latitude and the longitude of the system using Google Maps:

Mathematics and Science Center at Emory University (400 Dowman Dr, Atlanta, GA, 30322)

Then, use a web API provided by the National Weather Service to retrieve the grid correlates to the coordinate: https://api.weather.gov/points/33.7904,-84.3266

Write a macro that retrieves the current weather for the grid using another web API:

  • #1: imports the json package.

  • #2: imports the requests package.

  • #6: specifies the forecast URL.

  • #7: retrieves the content from the URL in JSON.

  • #8: saves the JSON content to a dictionary.

  • #9: retrieves forecasts for all supported periods.

  • #10: retrieves the forecast for today.

  • #11: returns today's forecast.

Finally, update the transitions with the weather macro:

  • #15: calls the WEATHER macro to generate the system output displaying today's weather.

  • #22: adds #WEATHER to the macro dictionary.

The time and the weather retrieved by the above macros are oriented to the system, not the user. It is possible to anticipate the users' location if one's IP address is provided; however, this is not possible unless the user uses a specific device (e.g., Amazon Echo, smartphone) and agrees to send one's private information to our system.

Last updated

Was this helpful?