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()) self.mic = Mic(mic.speaker, mic.passive_stt_engine, mic.active_stt_engine.get_instance('music', phrases))
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)
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()
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
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
def start(self): def testConnection(): if Diagnostics.check_network_connection(): print "CONNECTED TO INTERNET" else: print "COULD NOT CONNECT TO NETWORK" self.speaker.say( "Warning: I was unable to connect to a network. Parts of the system may not work correctly, depending on your setup.") def fail(message): traceback.print_exc() self.speaker.say(message) sys.exit(1) def configure(): try: print "COMPILING DICTIONARY FOR MODULES" vocabcompiler.compile( "sentences.txt", "dictionary.dic", "languagemodel.lm") print "COMPILING DICTIONARY OF LOCATIONS OPTIONS" vocabcompiler.compileLocations( "sentences_locations.txt", "dictionary_locations.dic", "languagemodel_locations.lm"); print "STARTING CLIENT PROGRAM" except OSError: print "BOOT FAILURE: OSERROR" fail( "There was a problem starting EasyNav. You may be missing the language model and associated files. Please read the documentation to configure your Raspberry Pi.") except IOError: print "BOOT FAILURE: IOERROR" fail( "There was a problem starting EasyNav. You may have set permissions incorrectly on some part of the filesystem. Please read the documentation to configure your Raspberry Pi.") except: print "BOOT FAILURE" fail( "There was a problem starting EasyNav.") old_client = os.path.abspath(os.path.join(os.pardir, "old_client")) if os.path.exists(old_client): shutil.rmtree(old_client) print "===========================================================" print " JASPER The Talking Computer " print " Copyright 2013 Shubhro Saha & Charlie Marsh " print "===========================================================" self.speaker.say("Hello.... I am EASYNAV... Please wait one moment while I configure.") configure() profile = yaml.safe_load(open("profile.yml", "r")) try: api_key = profile['keys']['GOOGLE_SPEECH'] except KeyError: api_key = None try: stt_engine_type = profile['stt_engine'] except KeyError: print "stt_engine not specified in profile, defaulting to PocketSphinx" stt_engine_type = "sphinx" mic = Mic(self.speaker, stt.PocketSphinxSTT(), stt.newSTTEngine(stt_engine_type, api_key=api_key)) mic.say("Hi...i'm EasyNav.....your friendly navigation assistant....") mic.say("To invoke me, please say my Name......and i will beep....then proceed with your command") mic.say("To find out about commands...please say help......") self.dispatcherClient.start() #GetLocations.getLoc() conversation = Conversation("EASYNAV", mic, profile, self.dispatcherClient) conversation.handleForever()