def process_recognized_speech(self, googleJson, requestId, dictation): possible_matches = googleJson['hypotheses'] if len(possible_matches) > 0: best_match = possible_matches[0]['utterance'] if len(best_match) == 1: best_match = best_match.upper() else: best_match = best_match[0].upper() + best_match[1:] best_match_confidence = possible_matches[0]['confidence'] self.logger.info(u"Best matching result: \"{0}\" with a confidence of {1}%".format(best_match, round(float(best_match_confidence) * 100, 2))) # construct a SpeechRecognized token = Token(best_match, 0, 0, 1000.0, True, True) interpretation = Interpretation([token]) phrase = Phrase(lowConfidence=False, interpretations=[interpretation]) recognition = Recognition([phrase]) recognized = SpeechRecognized(requestId, recognition) if not dictation: if self.current_running_plugin == None: plugin = PluginManager.getPluginForImmediateExecution(self.assistant.assistantId, best_match, self.assistant.language, (self.send_object, self.send_plist, self.assistant, self.current_location)) if plugin != None: plugin.refId = requestId plugin.connection = self self.current_running_plugin = plugin self.send_object(recognized) self.current_running_plugin.start() else: self.send_object(recognized) view = AddViews(requestId) errorText = SiriProtocolHandler.__not_recognized[self.assistant.language] if self.assistant.language in SiriProtocolHandler.__not_recognized else SiriProtocolHandler.__not_recognized["en-US"] view.views += [AssistantUtteranceView(errorText.format(best_match), errorText.format(best_match))] websearchText = SiriProtocolHandler.__websearch[self.assistant.language] if self.assistant.language in SiriProtocolHandler.__websearch else SiriProtocolHandler.__websearch["en-US"] button = Button(text=websearchText) cmd = SendCommands() cmd.commands = [StartRequest(utterance=u"^webSearchQuery^=^{0}^^webSearchConfirmation^=^Yes^".format(best_match))] button.commands = [cmd] view.views.append(button) self.send_object(view) self.send_object(RequestCompleted(requestId)) elif self.current_running_plugin.waitForResponse != None: # do we need to send a speech recognized here? i.d.k self.current_running_plugin.response = best_match self.current_running_plugin.refId = requestId self.current_running_plugin.waitForResponse.set() else: self.send_object(recognized) self.send_object(RequestCompleted(requestId)) else: self.send_object(recognized) self.send_object(RequestCompleted(requestId))
def joke_du_Matin(self, speech, language): if language == 'fr-FR': rss = "http://blague.dumatin.fr/blagues.xml" IndiceBlague = random.randint(1,6) print IndiceBlague feeds = feedparser.parse(rss) NomBlague = feeds.entries[IndiceBlague]['title'] Blague = feeds.entries[IndiceBlague].description TxtBlagueSansCaractere = Blague.replace("<br />","") TxtBlague = TxtBlagueSansCaractere[0:TxtBlagueSansCaractere.index("<a href=")] LienSite = TxtBlagueSansCaractere[TxtBlagueSansCaractere.index("<a href=") + 16:TxtBlagueSansCaractere.index(">[ blague.dumatin.fr") - 2] LienSite = LienSite.replace("."," ") print LienSite if language == 'fr-FR': view = AddViews(self.refId, dialogPhase="Summary") viewBlague = AssistantUtteranceView(text=TxtBlague, speakableText="Voici une blague du matin", dialogIdentifier="Blague#created") button = Button(text=LienSite) cmd = systemObjects.SendCommands() cmd.commands = [systemObjects.StartRequest(utterance=u"^webSearchQuery^=^{0}^^webSearchConfirmation^=^Yes^".format(LienSite))] button.commands = [cmd] view.views = [viewBlague,button] self.send_object(view) self.complete_request()