Skip to content
Snippets Groups Projects
actions.py 1.37 KiB
Newer Older
from rasa_core_sdk import Action
from rasa_core_sdk.events import SlotSet
Jonas Wolff's avatar
Jonas Wolff committed

class ActionSearchBirthplace(Action):
    def name(self):
        return 'action_search_birthplace'

    def run(self, dispatcher, tracker, domain):
        import csv
        #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):
Jonas Wolff's avatar
Jonas Wolff committed
    def name(self):
        return 'action_utter_birthplace'
Jonas Wolff's avatar
Jonas Wolff committed

    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 + ".")
Jonas Wolff's avatar
Jonas Wolff committed
        return []