from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet class ActionSearchBirthplace(Action): def name(self): return 'action_search_birthplace' def run(self, dispatcher, tracker, domain): import csv import re #dispatcher.utter_message("Searching for Birthplace") person = tracker.get_slot('physicist') name_regex = re.compile(person, re.IGNORECASE) actual_birthplace = None with open('birthplace.tsv') as csvfile: spamreader = csv.DictReader(csvfile, delimiter='\t') for row in spamreader: if name_regex.match(row['name']): actual_birthplace = row['birthplace'] if actual_birthplace is None: return[SlotSet('birthplace', [])] return [SlotSet('birthplace', actual_birthplace if actual_birthplace is not None else [])] class ActionUtterBirthplace(Action): def name(self): return 'action_utter_birthplace' def run(self, dispatcher, tracker, domain): person = tracker.get_slot('physicist') birthplace = tracker.get_slot('birthplace') if birthplace == None: dispatcher.utter_message("Error: Birthplace of " + person + "not known.") else: dispatcher.utter_message("The birthplace of " + person + " is " + birthplace + ".") return []