Skip to content

Instantly share code, notes, and snippets.

@jeffbass
Created July 31, 2020 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffbass/eae04aab69adc84be3991fc0217b1ae7 to your computer and use it in GitHub Desktop.
Save jeffbass/eae04aab69adc84be3991fc0217b1ae7 to your computer and use it in GitHub Desktop.
Example method of ChatBot class that reports current temperature
# this is an example method from the Librarian ChatBot class
# note that this is a simple example with a few hard coded imagenode names
def report_temperature(self, locations):
""" report temperature by location
This is a very awkward test matching of location and temperature.
Needs to be made much more sophistcated than if / else / etc.
Parameters:
locations (set): location words (each a str)
Returns:
reply (str): Sentences reporting temperatures for all the locations.
"""
compound_reply = ''
# for all locations, report temperature; Then remove the reported
# location from the set of locations and repeat until all are reported.
while locations: # while there are any locations not yet reported
word = locations.pop()
if word.startswith('barn'):
(current, previous) = self.data.fetch_event_data('barn', 'Temp')
if current:
reply = 'Temperature behind barn is {0}.'.format(current[1])
else:
reply = 'No current barn temperature available.'
if word.startswith('deck'):
(current, previous) = self.data.fetch_event_data('backdeck', 'Temp')
if current:
reply = 'Temperature on back deck is {0}.'.format(current[1])
else:
reply = 'No current back deck temperature available.'
if word.startswith('garage'):
(current, previous) = self.data.fetch_event_data('garage', 'Temp')
if current:
reply = 'Temperature in garage is {0}.'.format(current[1])
else:
reply = 'No current garage temperature available.'
if word.startswith('office'):
(current, previous) = self.data.fetch_event_data('jeffoffice', 'Temp')
if current:
reply = 'Temperature inside house is {0}.'.format(current[1])
else:
reply = 'No current inside temperature available.'
compound_reply = ' '.join([compound_reply, reply])
if len(locations) < 1: # popped all locations; none left
break
return compound_reply.strip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment