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 :)
S: Hello. How are you?
U: Could be better..
S: I hope your day gets better soon :(
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.
It responds with the error statement because the string key (e.g., 'good', 'bad') does not match the entire input (e.g., "I'm good", "Pretty bad").
If you replace 'bad' in #7 with 'pretty bad', it matches the input "Pretty bad". However, it no longer matches the input "bad".
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 :)
S: Hello. How are you?
U: It could be better..
S: I hope your day gets better soon :(
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?
All terms in a list must match some terms in the input sequentially. Thus, by replacing '[good]' with '[good, fantastic]', it no longer understands the inputs "good" or "fantastic":
S: Hello. How are you?
U: good
S: Sorry, I didn't understand you.
S: Hello. How are you?
U: fantastic
S: Sorry, I didn't understand you.
Instead, it understands the input "good and fantastic" by matching "good" first, then "fantastic" later:
S: Hello. How are you?
U: good and fantastic!
S: Glad to hear that you are doing well :)
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 :)
S: Hello. How are you?
U: It's fantastic!!
S: Glad to hear that you are doing well :)
S: Hello. How are you?
U: Pretty bad..
S: I hope your day gets better soon :(
S: Hello. How are you?
U: It could be better.
S: I hope your day gets better soon :(
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?
It behaves the same way as the one in Exercise 2.
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.
S: Hello. How are you?
U: Fine, and you?
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!
S: Hello. How are you?
U: Fine, and you?
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"?
Since both the conditions in #5 and #11 match the input, it randomly chooses one of the responses such that it can nondeterministically generate either of the followings:
S: Hello. How are you?
U: Good, how are you?
S: Glad to hear that you are doing well :)
S: Hello. How are you?
U: Good, how are you?
S: I feel superb. Thank you!
Code Snippet
defmatching_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 dfif__name__=='__main__':matching_strategy().run()