Skip to content

Commit

Permalink
allow zip or city for search
Browse files Browse the repository at this point in the history
  • Loading branch information
paulaWesselmann committed Mar 26, 2019
1 parent bd1893d commit e0b7426
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 286 deletions.
47 changes: 29 additions & 18 deletions actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
"base": "https://data.medicare.gov/resource/{}.json",
"rbry-mqwu": {
"city_query": "?city={}",
"zip_code_query": "?$where=zip_code in({})",
"zip_code_query": "?zip_code={}",
"id_query": "?provider_id={}"
},
"b27b-2uc7": {
"city_query": "?provider_city={}",
"zip_code_query": "?$where=provider_zip_code in({})",
"zip_code_query": "?provider_zip_code={}",
"id_query": "?federal_provider_number={}"
},
"9wzi-peqs": {
"city_query": "?city={}",
"zip_code_query": "?$where=zip in({})",
"zip_code_query": "?zip={}",
"id_query": "?provider_number={}"
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ def run(self, dispatcher, tracker, domain):

def _create_path(base, resource, query, values):
'''Creates a path to find provider using the endpoints.'''

if isinstance(values, list):
return (base + query).format(resource,
', '.join(
Expand All @@ -100,11 +100,18 @@ def _create_path(base, resource, query, values):
return (base + query).format(resource, values)


def _find_facilities(city, resource):
def _find_facilities(location, resource):
'''Returns json of facilities matching the search criteria.'''

full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["city_query"], city.upper())
if str.isdigit(location):
full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["zip_code_query"],
location)
else:
full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["city_query"],
location.upper())

results = requests.get(full_path).json()
return results

Expand All @@ -127,13 +134,16 @@ def name(self):
return "find_facilities"

def run(self, dispatcher, tracker, domain):
city = tracker.get_slot('city')

location = tracker.get_slot('location')
facility_type = tracker.get_slot('facility_type')
results = _find_facilities(city, facility_type)

results = _find_facilities(location, facility_type)
button_name = _resolve_name(FACILITY_TYPES, facility_type)
if len(results) == 0:
dispatcher.utter_message(
"Sorry, we could not find a {} in {}.".format(button_name, city))
"Sorry, we could not find a {} in {}.".format(button_name,
location))
return []

buttons = []
Expand All @@ -155,7 +165,8 @@ def run(self, dispatcher, tracker, domain):

# limit number of buttons to 3 here for clear presentation purpose only
dispatcher.utter_button_message(
"Here is a list of 3 {}s near you".format(button_name),
"Here is a list of {} {}s near you".format(len(buttons[:3]),
button_name),
buttons[:3], button_type="custom")
# todo:note: button options are not working BUG in rasa_core

Expand Down Expand Up @@ -202,28 +213,28 @@ def run(self,

class FacilityForm(FormAction):
"""Custom form action to fill all slots required to find specific type
of healthcare facilities in a certain city."""
of healthcare facilities in a certain city or zip code."""

def name(self):
"""Unique identifier of the form"""

return "facility_form"

@staticmethod
def required_slots(tracker):
# type: (Tracker) -> List[Text]
"""A list of required slots that the form has to fill"""

return ["facility_type", "city"]
return ["facility_type", "location"]

def slot_mappings(self):
# type: () -> Dict[Text: Union[Dict, List[Dict]]]
return {
"facility_type": self.from_entity(entity="facility_type",
intent=["inform",
"search_provider"]),
"city": self.from_entity(entity="city", intent="inform")
}
"location": self.from_entity(entity="location",
intent=["inform",
"search_provider"])}

def validate(self, dispatcher, tracker, domain):
# type: (CollectingDispatcher, Tracker, Dict[Text, Any]) -> List[Dict]
Expand All @@ -233,12 +244,12 @@ def validate(self, dispatcher, tracker, domain):
# extract other slots that were not requested
# but set by corresponding entity
slot_values = self.extract_other_slots(dispatcher, tracker, domain)

# extract requested slot
slot_to_fill = tracker.get_slot(REQUESTED_SLOT)
if slot_to_fill:
slot_values.update(self.extract_requested_slot(dispatcher,
tracker, domain))

if not slot_values:
# reject form action execution
# if some slot was requested but nothing was extracted
Expand All @@ -259,7 +270,7 @@ def submit(self, dispatcher, tracker, domain):
# utter submit template
dispatcher.utter_template('utter_submit', tracker)
return [FollowupAction('find_facilities')]


class ActionChitchat(Action):
"""Returns the chitchat utterance dependent on the intent"""
Expand Down
6 changes: 3 additions & 3 deletions data/core/chithcat.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
- slot{"facility_type": "rbry-mqwu"}
- facility_form
- form{"name": "facility_form"}
- slot{"requested_slot": "city"}
* inform{"city": "san francisco"}
- slot{"city": "san francisco"}
- slot{"requested_slot": "location"}
* inform{"location": "san francisco"}
- slot{"location": "san francisco"}
- find_facilities
* inform{"facility_id": "050407"}
- slot{"facility_id": "050407"}
Expand Down
14 changes: 7 additions & 7 deletions data/core/stories.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- facility_form
- form{"name": "facility_form"}
- form{"name": null}
- slot{"requested_slot":"city"}
- slot{"requested_slot":"location"}
* inform{"facility_id":4245}
- find_healthcare_address
- utter_address
Expand All @@ -36,11 +36,11 @@
- slot{"facility_type": "9wzi-peqs"}
- facility_form
- form{"name": "facility_form"}
- slot{"requested_slot": "city"}
* form: inform{"city": "austin"}
- slot{"city": "austin"}
- slot{"requested_slot": "location"}
* form: inform{"location": "austin"}
- slot{"location": "austin"}
- form: facility_form
- slot{"city": "austin"}
- slot{"location": "austin"}
- form: followup{"name": "find_facilities"}
- form{"name": null}
- slot{"requested_slot": null}
Expand All @@ -54,8 +54,8 @@
- utter_noworries

## Generated Story -5318182399020957170
* search_provider{"city": "Austin", "facility_type": "rbry-mqwu"}
- slot{"city": "Austin"}
* search_provider{"location": "Austin", "facility_type": "rbry-mqwu"}
- slot{"location": "Austin"}
- slot{"facility_type": "rbry-mqwu"}
- find_facilities
* inform{"facility_id": "450871"}
Expand Down
Loading

0 comments on commit e0b7426

Please sign in to comment.