4.3. Compound States

transitions = {
    'state': 'start',
    '#GATE `Let\'s talk about music.`': 'music',
    '#GATE `Let\'s talk about movies.`': 'movie',
    '`That\'s all I can talk about.`': 'end',
}

Multiple Topics

A dialogue flow can be complex when you start adding multiple topics. In this case, you can create a separate transition dictionary for each topic.

Let us create transitions talking about music and movies:

transitions = {
    'state': 'start',
    '`Let\'s talk about music.`': 'music',
    '`Let\'s talk about movies.`': 'movie',
    '`That\'s all I can talk about.`': 'end'
}
  • #3: directs to the music state.

  • #4: directs to the movie state.

The music and movie states can be defined in separate transition dictionaries:

transitions_music = {
    'state': 'music',
    '`What is your favorite song?`': {
        '[[!{#LEM(rain), rainy}, #LEM(taco)]]': {
            '`I love children\'s songs by Parry Gripp.`': 'start',
        },
        'error': {
            '`Sorry, I don\'t know that song.`': 'movie'
        }
    }
}
  • #5: directs to the start state.

  • #8: directs to the movie state.

Finally, all three transition dictionaries can be loaded to the same dialogue flow:

df = DialogueFlow('start', end_state='end')
df.load_transitions(transitions)
df.load_transitions(transitions_music)
df.load_transitions(transitions_movie)
df.run()

When the dialogue flow runs, it randomly selects one of the 3 states, music, movie, and end:

S: Let's talk about music. What is your favorite song?
U: Doremi Song
S: Sorry, I don't know that song. What is your favorite movie?
U: Superman
S: Sorry, I don't know that movie. What is your favorite song?
U: Raining Tacos
S: I love children's songs by Parry Gripp. Let's talk about movies. What is your favorite movie?
U: Iron man
S: I love the Marvel Cinematic Universe. Let's talk about music. What is your favorite song?
U: It's raining tacos
S: I love children's songs by Parry Gripp. That\'s all I can talk about.
  • #1: randomly selects the music state.

  • #3: switches to the movie state when it does not understand the user input.

  • #5: switches to the music state when it does not understand the user input.

  • #7: goes back to the start state when it understands the user input, and randomly selects the movie state.

  • #9: goes back to the start state when it understands the user input, and randomly selects the music state.

  • #11: goes back to the start state when it understands the user input, and randomly selects the end state.

Gating

The randomness in the above transitions can be quite annoying because it may end the dialogue immediately after it runs or repeats the same topic over and over. This can be alleviated by using the #GATE built-in macro:

transitions = {
    'state': 'start',
    '#GATE `Let\'s talk about music.`': 'music',
    '#GATE `Let\'s talk about movies.`': 'movie',
    '`That\'s all I can talk about.`': 'end',
}
  • #3: puts the music topic to an open gate.

  • #4: puts the movie topic to an open gate.

  • #4: has no gate to open (so it can be selected at any time).

Once an output is selected, #GATE closes the door for that output such that it will never be selected again.

S:  Let's talk about music. What is your favorite song?
U: Raining Tacos
S: I love children's songs by Parry Gripp.  Let's talk about movies. What is your favorite movie?
U: Iron Man
S: I love the Marvel Cinematic Universe. That's all I can talk about.

It is important to have at least one output with no gate; otherwise, the system will crash unless one of the outputs leads to the end state.

Scoring

The gating prevents the system from repeating the same topic, but the end state can still be selected at any time without consuming all the other topics. To ensure that the end state gets selected last, we use scoring:

transitions = {
    'state': 'start',
    '#GATE `Let\'s talk about music.`': 'music',
    '#GATE `Let\'s talk about movies.`': 'movie',
    '`That\'s all I can talk about.`': {
        'state': 'end',
        'score': 0.1
    }
}
  • #6: indicates that this is the end state.

  • #7: assigns the score of this state to 0.1.

By default, all states receive a score of 1; thus, assigning a score below 1 would make that state not selected at all unless there are dynamic scoring changes through macros such as #GATE.

Last updated

©2023 Emory University - All rights reserved