def respond(self, **kwargs): tags = kwargs.get('tags') plain_text = kwargs.get('plain_text') the_time = datetime.datetime.now() if JenniferTimePlugin.is_asking_for_time(tags): hour = the_time.strftime('%I').lstrip('0') return JenniferResponse(self, [ JenniferTextResponseSegment( the_time.strftime('{}:%M %p'.format(hour))) ]) elif JenniferTimePlugin.is_asking_for_date(tags): # Could be asking "what was the date _____", "what is the date", "what is the date _____", let's parse possible_dates = list(semantic.dates.extractDates(plain_text)) def time_format(dt_obj): inflect_eng = inflect.engine() date_format = '%A, %B {}, %Y'.format( inflect_eng.ordinal(dt_obj.strftime('%d'))) return dt_obj.strftime(date_format) # Asking for date today if not possible_dates: response = 'Today\'s date is {}'.format(time_format(the_time)) else: # See if they specified a day? the_time = possible_dates[0] response = f'{time_format(the_time)}' return JenniferResponse(self, [JenniferTextResponseSegment(response)])
def give_output_string(self, creator, text): if isinstance(text, str): response_obj = JenniferResponse(creator, [JenniferTextResponseSegment(text)]) elif issubclass(text.__class__, JenniferResponseAbstractSegment): response_obj = JenniferResponse(creator, [text]) else: raise Exception("client.give_output_string must receive a string or instance of JenniferResponseAbstractSegment") return self.give_output(response_obj)
def respond_or_unsure(self, respond_to, tags, client, text_input): try: return respond_to.respond(tags=tags, client=client, brain=self, plain_text=text_input) except Exception as e: return JenniferResponse( self, [JenniferTextResponseSegment(self.UNSURE_TEXT)])
def respond_count(self, account, **kwargs): imap = GmailImapWrapper(account['email'], account['password'], self.settings['markAsRead']) unread = imap.num_unread_emails unread = unread if unread != 0 else "no" total = imap.num_total_emails return JenniferResponse(self, [ JenniferTextResponseSegment( "You have {} new {}, and {} {} total".format( unread, self.pluralize_engine.plural('email', unread), total, self.pluralize_engine.plural('message', total))) ])
def respond(self, **kwargs): plain_text = kwargs.get('plain_text') client = kwargs.get('client') # Setup service = semantic.units.ConversionService() units = service.extractUnits(plain_text) i_engine = inflect.engine() first_unit_plural = i_engine.plural_noun(units[0], 10).replace('ss', 's') try: # Give the conversion return JenniferResponse(self, [ JenniferTextResponseSegment( i_engine.number_to_words(service.convert(plain_text))) ]) except TypeError: # If we've already done this before, return cont = kwargs.get('bail_if_exception', False) if cont: client.give_output_string(self, "Sorry I can't convert that") return # user didn't specify how many of the unit to convert client.give_output_string(self, "How many {}?".format(first_unit_plural)) number = client.collect_input() # Sub out the input text actual_units = '{} {}'.format(number, units[0]) plain_text = plain_text.replace(units[0], actual_units) # Call self, but with flag only allowing this to happen once return self.respond(client=client, plain_text=plain_text, bail_if_exception=True) except ValueError: # Failure client.give_output_string( self, "Sorry I can't convert {} to {}".format(units[0], units[1])) return