def saytimenice(tree, _): time = datetime.datetime.now() hour = time.hour minute = time.minute # We're talking towards the next hour, so increase it by 1 if minute > 32: hour += 1 hour = hour % 12 minute = (5 * round(minute / 5)) % 60 # We don't usually say 0 hours, we say 12 if hour == 0: hour = 12 # Handle the different cases if minute == 0: s = f"It is {hour} 'o clock" elif minute == 45: s = f"It is quarter to {hour}" elif minute == 15: s = f"It is quarter past {hour}" elif minute == 30: s = f"It is half past {hour}" elif minute > 30: s = f"It is {60 - minute} to {hour}" else: s = f"It is {minute} past {hour}" say(s)
def handle_action(self, action): for a in action['actions']: if hasattr(Actions, a['type']): getattr(Actions, a['type'])(self, a['value']) else: say(f"ERROR! Unknown action type {a['type']}") if "next" in action: self.set_active(action['next']) else: self.deactivate()
def sayweather(tree, _): host = hosts['weather'] r = requests.get(host) w = r.json() starter = random.choice( ["It is currently", "It is now", "The weather right now is"]) t = round(w['main']['temp']) temp = random.choice([ f"with a temperature of {t} degrees", f"at {t} degrees", f", {t} degrees celcius" ]) s = f"{starter} {w['weather'][0]['main']} {temp}." say(s)
def main(): recognizer = sr.Recognizer() recognizer.pause_threshold = 0.5 recognizer.dynamic_energy_threshold = False microphone = sr.Microphone() print_status(recognizer) readjust(recognizer, microphone) tree = SpeechTree("tree.json") # Ready to go! say("Jarvis is online") while True: with microphone as source: try: audio = recognizer.listen(source, phrase_time_limit=5, timeout=3) handle(recognizer, audio, tree) except sr.WaitTimeoutError: # Timeout reached, run loop to check for if DEBUG: print("timeout") if tree.active and (time() - tree.last) > SPEECH_TIMEOUT: # Havent had a command in a while now, cancel active state tree.deactivate(True) if time() - last_adjust > READJUST_TIMEOUT: print("->Readjusting Audio") readjust(recognizer, microphone) if DEBUG: print(recognizer.energy_threshold)
def process_text(self, text): if self.active: if awis(self.stop['triggers'], text): say(random.choice(self.stop['responses'])) self.deactivate() else: scores = [] for action in self.current: score = cwis(action['trigger'], text) if score >= self.score_threshold: scores.append((score, action)) if len(scores) > 0: best_action = sorted(scores, key=lambda x: x[0], reverse=True)[0][1] self.handle_action(best_action) else: say("I can't help you with that") self.last = time() else: if awis(self.keyword['triggers'], text): responses = self.keyword['responses'] hr = datetime.datetime.now().hour if 6 <= hr <= 11: options = responses['any'] + responses['morning'] elif 12 <= hr <= 17: options = responses['any'] + responses['afternoon'] elif 18 <= hr <= 23: options = responses['any'] + responses['evening'] elif 0 <= hr <= 4: options = responses['any'] + responses['night'] else: options = responses['any'] say(random.choice(options)) self.activate()
def saydate(tree, _): time = datetime.datetime.now() number = ordinal(time.day) s = time.strftime(f"It is %A the {number} of %B, at %H %M") say(s)
def saytime(tree, _): time = datetime.datetime.now() say(time.strftime("The time is %H %M"))
def sayrandom(tree, options): say(random.choice(options))
def say(tree, text): say(text)
def nerdjoke(tree, _): j = requests.get(f"{hosts['joke']}/programming/random") joke = j.json()[0] s = joke['setup'] + ' ' + joke['punchline'] say(s)
def sayjoke(tree, _): j = requests.get(f"{hosts['joke']}/general/random") joke = j.json()[0] s = joke['setup'] + ' ' + joke['punchline'] say(s)
def please_repeat(self): say(random.choice(self.repeat))
def deactivate(self, tellme=False): if tellme: say("deactivating") self.current = None self.last = None