class MusicMode(object): def __init__(self, PERSONA, mic, mpdwrapper): self._logger = logging.getLogger(__name__) self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = mpdwrapper # index spotify playlists into new dictionary and language models phrases = [ "STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST" ] phrases.extend(self.music.get_soup_playlist()) music_stt_engine = mic.active_stt_engine.get_instance('music', phrases) self.mic = Mic(mic.speaker, mic.passive_stt_engine, music_stt_engine) def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print("SONG RESULTS") # print("============") # for song in songs: # print("Song: %s Artist: %s" % (song.title, song.artist)) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: threshold, transcribed = self.mic.passiveListen(self.persona) if not transcribed or not threshold: self._logger.info("Nothing has been said or transcribed.") continue self.music.pause() input = self.mic.activeListen(MUSIC=True) if input: if "close" in input.lower(): self.mic.say("Closing Spotify") return self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()
def handle(text, mic, profile): # Vocab used in Currency Exchange Rate Module phrases = [ "YES", "NO", "JAPANESE", "YEN", "AMERICAN", "DOLLAR", "INDIAN", "RUPEES", "EURO", "SWISS", "FRANC", "BRITISH", "POUND", "CHINESE", "YUAN", ] # Comment out below two lines if you are using Jasper in TEXT Mode currexch_stt_engine = mic.active_stt_engine.get_instance("rateexch", phrases) mic = Mic(mic.speaker, mic.passive_stt_engine, currexch_stt_enginess) serviceNum = NumberService() def convertToCode(currency): code_list = { "INDIAN": "INR", "RUPEES": "INR", "YEN": "JPY", "JAPANESE": "JPY", "AMERICAN": "USD", "DOLLAR": "USD", "YUAN": "CNH", "CHINESE": "CNH", "EURO": "EUR", "POUND": "GBP", "BRITISH": "GBP", " SWISS": "CHF", "FRANC": "CHF", } for key, value in code_list.iteritems(): if key in currency.split(): return value return "" def currencyConverterYahoo(currency_from, currency_to, currency_input): yql_base_url = "https://query.yahooapis.com/v1/public/yql" yql_query = ( 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("' + currency_from + currency_to + '")' ) yql_query_url = ( yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" ) yql_response = urllib2.urlopen(yql_query_url) yql_json = json.loads(yql_response.read()) currency_output = currency_input * float(yql_json["query"]["results"]["rate"]["Rate"]) return currency_output while True: mic.say(" First Currency?") currency_from = convertToCode(mic.activeListen()) mic.say(" Second Currency?") currency_to = convertToCode(mic.activeListen()) if currency_from != "" and currency_to != "": mic.say(" Getting exchange rate of " + currency_from + " against " + currency_to + ".") currency_input = 1 currency_input_str = serviceNum.parseMagnitude(currency_input) try: # YAHOO Services rate = currencyConverterYahoo(currency_from, currency_to, currency_input) rateStr = serviceNum.parseMagnitude(rate) except (IOError, ValueError, KeyError, TypeError): pass # invalid json mic.say(" An error occurred. Maybe the A P I is off line?") break else: pass # valid json mic.say(" Okay, here is the exchange rate.") mic.say( " It is approximately " + rateStr + " " + currency_to + " for " + currency_input_str + " " + currency_from + " ." ) mic.say(" Do you want to continue?") ans = mic.activeListen() if "NO" in ans.split(): break continue else: mic.say(" One or both currencies are not understood. Please try again.") continue
class MusicMode(object): def __init__(self, PERSONA, mic, mpdwrapper): self._logger = logging.getLogger(__name__) self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = mpdwrapper # index spotify playlists into new dictionary and language models phrases = ["STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST"] phrases.extend(self.music.get_soup_playlist()) music_stt_engine = mic.active_stt_engine.get_instance('music', phrases) self.mic = Mic(mic.speaker, mic.passive_stt_engine, music_stt_engine) def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print("SONG RESULTS") # print("============") # for song in songs: # print("Song: %s Artist: %s" % (song.title, song.artist)) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: threshold, transcribed = self.mic.passiveListen(self.persona) if not transcribed or not threshold: self._logger.info("Nothing has been said or transcribed.") continue self.music.pause() input = self.mic.activeListen(MUSIC=True) if input: if "close" in input.lower(): self.mic.say("Closing Spotify") return self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()
def handle(text, mic, profile): # Vocab used in Currency Exchange Rate Module phrases = [ "YES", "NO", "JAPANESE", "YEN", "AMERICAN", "DOLLAR", "INDIAN", "RUPEES", "EURO", "SWISS", "FRANC", "BRITISH", "POUND", "CHINESE", "YUAN" ] # Comment out below two lines if you are using Jasper in TEXT Mode currexch_stt_engine = mic.active_stt_engine.get_instance( 'rateexch', phrases) mic = Mic(mic.speaker, mic.passive_stt_engine, currexch_stt_enginess) serviceNum = NumberService() def convertToCode(currency): code_list = { "INDIAN": "INR", "RUPEES": "INR", "YEN": "JPY", "JAPANESE": "JPY", "AMERICAN": "USD", "DOLLAR": "USD", "YUAN": "CNH", "CHINESE": "CNH", "EURO": "EUR", "POUND": "GBP", "BRITISH": "GBP", " SWISS": "CHF", "FRANC": "CHF" } for key, value in code_list.iteritems(): if key in currency.split(): return value return '' def currencyConverterYahoo(currency_from, currency_to, currency_input): yql_base_url = "https://query.yahooapis.com/v1/public/yql" yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20("' + currency_from + currency_to + '")' yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" yql_response = urllib2.urlopen(yql_query_url) yql_json = json.loads(yql_response.read()) currency_output = currency_input * float( yql_json['query']['results']['rate']['Rate']) return currency_output while True: mic.say(" First Currency?") currency_from = convertToCode(mic.activeListen()) mic.say(" Second Currency?") currency_to = convertToCode(mic.activeListen()) if currency_from != "" and currency_to != "": mic.say(" Getting exchange rate of " + currency_from + " against " + currency_to + ".") currency_input = 1 currency_input_str = serviceNum.parseMagnitude(currency_input) try: # YAHOO Services rate = currencyConverterYahoo(currency_from, currency_to, currency_input) rateStr = serviceNum.parseMagnitude(rate) except (IOError, ValueError, KeyError, TypeError): pass # invalid json mic.say(" An error occurred. Maybe the A P I is off line?") break else: pass # valid json mic.say(" Okay, here is the exchange rate.") mic.say(" It is approximately " + rateStr + " " + currency_to + " for " + currency_input_str + " " + currency_from + " .") mic.say(" Do you want to continue?") ans = mic.activeListen() if "NO" in ans.split(): break continue else: mic.say( " One or both currencies are not understood. Please try again." ) continue