2.3. Matching Strategy

A taste of matching strategies provided in Emora STDM.

Multiple Matching

There is more than one way of expressing "good" or "bad". Let us use a set to match multiple expressions per condition by surrounding them with curly brackets:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        '{good, fantastic}': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        '{bad, could be better}': {
            '`I hope your day gets better soon :(`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        }
    }
}
  • #4: matches the input with either 'good' or 'fantastic'.

  • #7: matches the input with either 'bad' or 'could be better'.

The above transitions can handle the following inputs (on top of the cases of good and bad):

S: Hello. How are you?
U: Fantastic!!
S: Glad to hear that you are doing well :)

A phrase can be used for matching, although it needs to match the entire input such that 'could be better' in #7 does not match an input like "It could be better".

Enter inputs such as "I'm good" or "Pretty bad" and see how the system responds.

Currently, STDM does not allow you to insert a single quote (') in any condition such that if you replace 'good' in #4 with 'I\'m good', it will throw a runtime error. This bug will be fixed in the next version.

Partial Matching

Matching the entire input is often too restrictive. Let us use a list to allow partial matching:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        '[good]': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        '[could be better]': {
            '`I hope your day gets better soon :(`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        },
    }
}
  • #4: the keyword 'good' is surrounded by the square brackets (good -> [good]), and matched to any word in the input.

  • #7: the keyphrase 'could be better' is surrounded by the square brackets, and matched to any phrase in the input.

S: Hello. How are you?
U: I'm good!!
S: Glad to hear that you are doing well :)

What are the differences between a set and a list in general data structures?

The above transitions yield the following dialogues:

Replace the condition in #4 with '[good, fantastic]'. Does it respond as expected?

Nesting

To allow partial matching of multiple expressions, a set can be put in a list:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        '[{good, fantastic}]': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        '[{bad, could be better}]': {
            '`I hope your day gets better soon :(`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        },
    }
}
  • #4: the keywords 'good' and 'fantastic' are first put in a set for multiple matching and then in a list for partial matching.

  • #7: the keyword 'bad' and the keyphrase 'could be better' are first put in a set for multiple matching, then in a list for partial matching.

S: Hello. How are you?
U: I'm so good!
S: Glad to hear that you are doing well :)

What are the meanings of nesting a set inside a list vs. a list inside a set?

What happens if '[{good, fantastic}]' in #4 is replaced with '{[good, fantastic]}'; in other words, put the keywords in a list first, then in a set?

Sequential Matching

Keywords or keyphrases delimited by commas in a list are matched sequentially:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        '[{good, fantastic}]': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        '[{bad, could be better}]': {
            '`I hope your day gets better soon :(`': 'end'
        },
        '[how, you]': {
            '`I feel superb. Thank you!`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        },
    }
}
  • #10: first matches 'how', then matches 'you' sequentially so that "how" must be followed by "you" in the input for this condition to be true.

S: Hello. How are you?
U: Fine. How are you doing?
S: I feel superb. Thank you!

All terms in the list must be matched sequentially such that the conditions are false for the following cases:

S: Hello. How are you?
U: Fine. How is it going?
S: Sorry, I didn't understand you.

Let us replace 'how' with '{how, and}' and 'you' with '{you, going}' in #10:

transitions = {
    'state': 'start',
    '`Hello. How are you?`': {
        '[{good, fantastic}]': {
            '`Glad to hear that you are doing well :)`': 'end'
        },
        '[{bad, could be better}]': {
            '`I hope your day gets better soon :(`': 'end'
        },
        '[{how, and}, {you, going}]': {
            '`I feel superb. Thank you!`': 'end'
        },
        'error': {
            '`Sorry, I didn\'t understand you.`': 'end'
        },
    }
}
  • #10: first matches ('how' or 'and'), then matches ('you' or 'going') sequentially.

S: Hello. How are you?
U: Fine. How is it going?
S: I feel superb. Thank you!

What other inputs does the condition in #10 match? Is there any consequence of matching that many expressions?

How does the system respond when the input is "Good, how are you"?

Code Snippet

def matching_strategy() -> DialogueFlow:
    transitions = {
        'state': 'start',
        '`Hello. How are you?`': {
            '[{good, fantastic}]': {
                '`Glad to hear that you are doing well :)`': 'end'
            },
            '[{bad, could be better}]': {
                '`I hope your day gets better soon :(`': 'end'
            },
            '[{how, and}, {you, going}]': {
                '`I feel superb. Thank you!`': 'end'
            },
            'error': {
                '`Sorry, I didn\'t understand you.`': 'end'
            },
        }
    }

    df = DialogueFlow('start', end_state='end')
    df.load_transitions(transitions)
    return df

if __name__ == '__main__':
    matching_strategy().run()

Last updated

©2023 Emory University - All rights reserved