transitions = {
'state': 'start',
'`What can I do for you?`': {
'[{time, clock}]': {
'`It\'s 3PM.`': 'end'
},
'[{weather, forecast}]': {
'`It\'s sunny outside`': 'end'
},
'[play, raining tacos]': {
'`It\'s raining tacos. From out of the sky ...`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'start'
}
}
}#13: names the state as play_raining_tacos.S: What can I do for you?
U: What time is it now?
S: It's 3PM.S: What can I do for you?
U: What's the weather like?
S: It's sunny outsideS: What can I do for you?
U: Play rainy taco
S: Sorry, I didn't understand you. What can I do for you?
U: Play raining tacos
S: It's raining tacos. From out of the sky ...transitions = {
'state': 'start',
'`What can I do for you?`': {
'[{time, clock}]': {
'state': 'time',
'`It\'s 3PM.`': 'end'
},
'[{weather, forecast}]': {
'state': 'weather',
'`It\'s sunny outside`': 'end'
},
'[play, raining tacos]': {
'state': 'play_raining_tacos',
'`It\'s raining tacos. From out of the sky ...`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'play_raining_tacos'
}
}
}S: What can I do for you?
U: Play rainy taco
S: Sorry, I didn't understand you. It's raining tacos. From out of the sky ...transitions = {
'state': 'start',
'`What can I do for you?`': {
'[{time, clock}]': {
'state': 'time',
'`It\'s 3PM.`': 'start'
},
'[{weather, forecast}]': {
'state': 'weather',
'`It\'s sunny outside`': 'start'
},
'[play, raining tacos]': {
'state': 'play_raining_tacos',
'`It\'s raining tacos. From out of the sky ...`': 'start'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'start'
},
'[exit]': {
'state': 'exit',
'`Goodbye!`': 'end'
}
}
}S: What can I do for you?
U: time
S: It's 3PM. What can I do for you?
U: weather
S: It's sunny outside 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: exit
S: Goodbye!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.
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'
},
}
}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'
},
'#UNX': {
'`Thanks for sharing.`': 'end'
},
}
}S: What can I do for you?
U: My name is Jinho
S: Right. Thanks for sharing. What can I do for you?
U: I am a professor
S: Yeah. Thanks for sharing. What can I do for you?
U: Hello world
S: Thanks for sharing. What can I do for you?
U: Play raining tacos
S: It's raining tacos. From out of the sky ...transitions = {
'state': 'start',
'`What can I do for you?`': {
'[play, [!{#LEM(rain), rainy}, #LEM(taco)]]': {
'state': 'play_raining_tacos',
'#IF($RAINING_TACOS=True) `Don\'t make me sing this again!`': 'start',
'`It\'s raining tacos. From out of the sky ...` #SET($RAINING_TACOS=True)': 'start',
},
'#UNX': {
'`Thanks for sharing.`': 'start'
},
}
}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 tacos
S: Don't make me sing this again! What can I do for you?
...class MacroSetBool(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[str]):
if len(args) != 2:
return False
variable = args[0]
if variable[0] == '$':
variable = variable[1:]
boolean = args[1].lower()
if boolean not in {'true', 'false'}:
return False
vars[variable] = bool(boolean)
return Truetransitions = {
'state': 'start',
'`What can I do for you?`': {
'[play, [!{#LEM(rain), rainy}, #LEM(taco)]]': {
'state': 'play_raining_tacos',
'#IF($RAINING_TACOS) `Don\'t make me sing this again!`': 'start',
'`It\'s raining tacos. From out of the sky ...` #SETBOOL(RAINING_TACOS, True)': 'start',
},
'#UNX': {
'`Thanks for sharing.`': 'start'
},
}
}
macros = {
'SETBOOL': MacroSetBool()
}class MacroPlayRainingTacos(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[str]):
return not vars.get('RAINING_TACOS', False)transitions = {
'state': 'start',
'`What can I do for you?`': {
'[play, [!{#LEM(rain), rainy}, #LEM(taco)]]': {
'state': 'play_raining_tacos',
'#IF($RAINING_TACOS) `Don\'t make me sing this again!`': 'start',
'#IF(#PLAY_RAINING_TACOS) `It\'s raining tacos. From out of the sky ...` #SETBOOL(RAINING_TACOS, True)': 'start',
},
'#UNX': {
'`Thanks for sharing.`': 'start'
},
}
}
macros = {
'SETBOOL': MacroSetBool(),
'PLAY_RAINING_TACOS': MacroPlayRainingTacos()
}
df = DialogueFlow('start', end_state='end')
df.load_transitions(transitions)
df.add_macros(macros)
df.run()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 tacos
S: Don't make me sing this again! What can I do for you?
U: Play raining tacos
S: Don't make me sing this again! What can I do for you?
...pip install python-vlcimport vlc
class MacroPlayRainingTacos(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[str]):
if not vars.get('RAINING_TACOS', False):
vlc.MediaPlayer("resources/raining_tacos.mp3").play()
return True
return Falseimport time
class MacroTime(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[str]):
current_time = time.strftime("%H:%M")
return "It's currently {}.".format(current_time)transitions = {
'state': 'start',
'`What can I do for you?`': {
'[play, [!{#LEM(rain), rainy}, #LEM(taco)]]': {
'state': 'play_raining_tacos',
'#IF($RAINING_TACOS) `Don\'t make me sing this again!`': 'start',
'#IF(#PLAY_RAINING_TACOS) `It\'s raining tacos. From out of the sky ...` #SETBOOL(RAINING_TACOS, True)': 'start',
},
'[{time, clock}]': {
'state': 'time',
'#TIME': 'end'
},
'#UNX': {
'`Thanks for sharing.`': 'start'
},
}
}
macros = {
'SETBOOL': MacroSetBool(),
'PLAY_RAINING_TACOS': MacroPlayRainingTacos(),
'TIME': MacroTime()
}S: What can I do for you?
U: What time is it now?
S: It's currently 05:27.{
...,
"properties": {
"@id": "https://api.weather.gov/points/33.7904,-84.3266",
...,
"forecast": "https://api.weather.gov/gridpoints/FFC/52,88/forecast",
...
}
}import json
import requests
class MacroWeather(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
url = 'https://api.weather.gov/gridpoints/FFC/52,88/forecast'
r = requests.get(url)
d = json.loads(r.text)
periods = d['properties']['periods']
today = periods[0]
return today['detailedForecast']transitions = {
'state': 'start',
'`What can I do for you?`': {
'[play, [!{#LEM(rain), rainy}, #LEM(taco)]]': {
'state': 'play_raining_tacos',
'#IF($RAINING_TACOS) `Don\'t make me sing this again!`': 'start',
'#IF(#PLAY_RAINING_TACOS) `It\'s raining tacos. From out of the sky ...` #SETBOOL(RAINING_TACOS, True)': 'start',
},
'[{time, clock}]': {
'state': 'time',
'#TIME': 'end'
},
'[{weather, forecast}]': {
'state': 'weather',
'#WEATHER': 'end'
},
'#UNX': {
'`Thanks for sharing.`': 'start'
},
}
}
macros = {
'SETBOOL': MacroSetBool(),
'PLAY_RAINING_TACOS': MacroPlayRainingTacos(),
'TIME': MacroTime(),
'WEATHER': MacroWeather()
}S: What can I do for you?
U: How is the weather today?
S: A chance of rain showers. Cloudy, with a high near 68. South wind 0 to 5 mph. Chance of precipitation is 30%.S: Good afternoon; it's sunny today. What should I call you?
U: Call me Jinho.
S: Hi, Jinho. Nice to meet you. What do you want me to recommend?
U: A movie, please.
S: How about "Spider-Man: Homecoming"? It's my personal favorite.
U: I've already seen that.
S: Got ya. How about "The Sound of Music"? It's classic.
U: Sure, I'll watch that one. Thanks.
S: Don't mention. S: Good morning; it's cloudy today. What's your name?
U: I'm Jinho.
S: Welcome back, Jinho. Did you get to watch "The Sound of Music"?
U: Yes, I did. I loved it.
S: Wonderful. What else do you want me to recommend?
U: Maybe, a song?
S: How about "Raining Tacos"?
U: What is it about?
S: It's a fun children's song by Parry Gripp.
U: Sure, I'll listen to that.
S: Enjoy!#3: directs to the music state.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'
}
}
}transitions_movie = {
'state': 'movie',
'`What is your favorite movie?`': {
'[{#LEM(avenger), iron man, hulk}]': {
'`I love the Marvel Cinematic Universe.`': 'start',
},
'error': {
'`Sorry, I don\'t know that movie.`': 'music'
}
}
}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.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.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',
}transitions = {
'state': 'start',
'`Let\'s talk about music.`': 'music',
'`Let\'s talk about movies.`': 'movie',
'`That\'s all I can talk about.`': 'end'
}df = DialogueFlow('start', end_state='end')
df.load_transitions(transitions)
df.load_transitions(transitions_music)
df.load_transitions(transitions_movie)
df.run()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',
}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
}
}def visits() -> DialogueFlow:
transitions = {
'state': 'start',
'`It\'s your` #VISITS `visit!`': 'end'
}
macros = {
'VISITS': MacroVisits()
}
df = DialogueFlow('start', end_state='end')
df.load_transitions(transitions)
df.add_macros(macros)
return df
S: That\'s all I can talk about.transitions = {
'state': 'start',
'`Hi there, how are you doing today?`': {
'[{good, fantastic}]': {
'state': 'good',
'`Glad to hear that.` #WHAT_ELSE': {
'[#LEM(movie)]': 'movie',
'[music]': 'music',
'error': {
'state': 'goodbye',
'`Goodbye!`': 'end'
}
}
},
'error': 'goodbye'
}
}
music_transitions = {
'state': 'music',
'`My favorite song is "Raining Tacos"! What\'s yours?`': {
'error': 'good'
}
}
movie_transitions = {
'state': 'movie',
'`My favorite movie is "Spider-Man: Homecoming"! What\'s yours?`': {
'error': 'good'
}
}
macros = {
'WHAT_ELSE': MacroWhatElse()
}
class MacroWhatElse(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
vn = 'HAVE_TALK'
if vn in vars and vars[vn]:
return 'What else do you want to talk about?'
else:
vars[vn] = True
return 'What do you want to talk about?'
df = DialogueFlow('start', end_state='end')
df.load_transitions(transitions)
df.load_transitions(music_transitions)
df.load_transitions(movie_transitions)
df.add_macros(macros)S: Hi there, how are you doing today?
U: Fantastic!
S: Glad to hear that. What do you want to talk about?
U: Let's talk about movies
S: My favorite movie is "Spider-Man: Homecoming"! What's yours?
U: The Sound of Music
S: Glad to hear that. What else do you want to talk about?
U: How about music?
S: My favorite song is "Raining Tacos"! What's yours?
U: Amazing Grace
S: Glad to hear that. What else do you want to talk about?
U: That's it..
S: Goodbye!gloabl_transitions = {
'[{covid, corona, virus}]': {
'`I hope you are OK.`': 'good'
},
'[{birthday}]': {
'`Happy birthday to you!`': 'good'
}
}
df.load_global_nlu(gloabl_transitions)S: Hi there, how are you doing today?
U: Good
S: Glad to hear that. What do you want to talk about?
U: Music
S: My favorite song is "Raining Tacos"! What's yours?
U: I got vaccinated for COVID
S: I hope you are OK. Glad to hear that. What else do you want to talk about?
U: It's my birthday
S: Happy birthday to you! Glad to hear that. What else do you want to talk about?
U: I'm done
S: Goodbye!S: Hi there, how are you doing today?
U: Good
S: Glad to hear that. What do you want to talk about?
U: Let's talk about virus movies
S: My favorite movie is "Spider-Man: Homecoming"! What's yours?S: Hi there, how are you doing today?
U: Good!
S: Glad to hear that. What do you want to talk about?
U: Let's talk about virus movies
S: I hope you are OK. Glad to hear that. What else do you want to talk about?gloabl_transitions = {
'[{covid, corona, virus}]': {
'score': 0.5,
'`I hope you are OK.`': 'good'
},
'[{birthday}]': {
'score': 0.5,
'`Happy birthday to you!`': 'good'
}
}#4: a writable (w) and binary (b) file and dumps the dictionry object into the file.class MacroVisits(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
vn = 'VISITS'
if vn not in vars:
vars[vn] = 1
return 'first'
else:
count = vars[vn] + 1
vars[vn] = count
match count:
case 2: return 'second'
case 3: return 'third'
case default: return '{}th'.format(count)S: It's your first visit!def save(df: DialogueFlow, varfile: str):
df.run()
d = {k: v for k, v in df.vars().items() if not k.startswith('_')}
pickle.dump(d, open(varfile, 'wb'))
save(visits(), 'resources/visits.pkl')def load(df: DialogueFlow, varfile: str):
d = pickle.load(open(varfile, 'rb'))
df.vars().update(d)
df.run()
save(df, varfile)S: It's your second visit!S: It's your third visit!S: It's your 4th visit!