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 = UIAddViews(requestId) errorText = SiriProtocolHandler.__not_recognized[self.assistant.language] if self.assistant.language in SiriProtocolHandler.__not_recognized else SiriProtocolHandler.__not_recognized["en-US"] errorView = UIAssistantUtteranceView() errorView.text = errorText.format(best_match) errorView.speakableText = errorText.format(best_match) view.views = [errorView] websearchText = SiriProtocolHandler.__websearch[self.assistant.language] if self.assistant.language in SiriProtocolHandler.__websearch else SiriProtocolHandler.__websearch["en-US"] button = UIButton() 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 createSmsSnippet(self, sms, addConfirmationOptions, dialogIdentifier, text, language): createAnchor = UIAddViews(self.refId) createAnchor.dialogPhase = createAnchor.DialogPhaseConfirmationValue # create a view to ask for the message askCreateView = UIAssistantUtteranceView() askCreateView.dialogIdentifier = dialogIdentifier askCreateView.text = askCreateView.speakableText = text askCreateView.listenAfterSpeaking = True # create a snippet for the sms snippet = SmsSnippet() if addConfirmationOptions: # create some confirmation options conf = UIConfirmSnippet({}) conf.requestId = self.refId confOpts = UIConfirmationOptions() confOpts.submitCommands = [ SendCommands( [conf, StartRequest(False, "^smsConfirmation^=^yes^")]) ] confOpts.confirmCommands = confOpts.submitCommands cancel = UICancelSnippet({}) cancel.requestId = self.refId confOpts.cancelCommands = [ SendCommands([ cancel, StartRequest(False, "^smsConfirmation^=^cancel^") ]) ] confOpts.denyCommands = confOpts.cancelCommands confOpts.denyText = snippetButtons['denyText'][language] confOpts.cancelLabel = snippetButtons['cancelLabel'][language] confOpts.submitLabel = snippetButtons['submitLabel'][language] confOpts.confirmText = snippetButtons['confirmText'][language] confOpts.cancelTrigger = snippetButtons['cancelTrigger'][language] snippet.confirmationOptions = confOpts snippet.smss = [sms] createAnchor.views = [askCreateView, snippet] return createAnchor
def process_recognized_speech(self, googleJson, requestId, dictation): possible_matches = googleJson['hypotheses'] if len(possible_matches) > 0: best_match = possible_matches[0]['utterance'] 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 = UIAddViews(requestId) errorText = SiriProtocolHandler.__not_recognized[self.assistant.language] if self.assistant.language in SiriProtocolHandler.__not_recognized else SiriProtocolHandler.__not_recognized["en-US"] errorView = UIAssistantUtteranceView() errorView.text = errorText.format(best_match) errorView.speakableText = errorText.format(best_match) view.views = [errorView] websearchText = SiriProtocolHandler.__websearch[self.assistant.language] if self.assistant.language in SiriProtocolHandler.__websearch else SiriProtocolHandler.__websearch["en-US"] button = UIButton() 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: self.send_object(recognized) 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 presentPossibleUsers(self, persons, language): root = UIAddViews(self.refId) root.dialogPhase = root.DialogPhaseClarificationValue utterance = UIAssistantUtteranceView() utterance.dialogIdentifier = "ContactDataResolutionDucs#disambiguateContact" utterance.text = responses['select'][language] utterance.speakableText = responses['select'][language] utterance.listenAfterSpeaking = True root.views = [utterance] # create a list with all the possibilities lst = UIDisambiguationList() lst.items = [] lst.speakableSelectionResponse = "OK!" lst.listenAfterSpeaking = True lst.selectionResponse = "OK" root.views.append(lst) for person in persons: item = UIListItem() item.object = person item.selectionResponse = person.fullName item.selectionText = person.fullName item.title = person.fullName item.commands = [ SendCommands([ StartRequest( False, "^phoneCallContactId^=^urn:ace:{0}".format( person.identifier)) ]) ] lst.items.append(item) return root
def __init__(self, timers=None, confirm=False): super(TimerSnippet, self).__init__("com.apple.ace.timer") self.timers = timers if timers != None else [] if confirm: self.confirmationOptions = ConfirmationOptions( submitCommands=[ SendCommands([ StartRequest( utterance= "^timerConfirmation^=^yes^ ^timerVerb^=^set^ ^timerNoun^=^timer^" ) ]) ], cancelCommands=[ SendCommands([ StartRequest( utterance= "^timerConfirmation^=^no^ ^timerVerb^=^set^ ^timerNoun^=^timer^" ) ]) ], denyCommands=[ SendCommands([ StartRequest( utterance= "^timerConfirmation^=^no^ ^timerVerb^=^set^ ^timerNoun^=^timer^" ) ]) ], confirmCommands=[ SendCommands([ StartRequest( utterance= "^timerConfirmation^=^yes^ ^timerVerb^=^set^ ^timerNoun^=^timer^" ) ]) ], denyText="Keep it", cancelLabel="Keep it", submitLabel="Change it", confirmText="Change it", cancelTrigger="Confirm") else: self.confirmationOptions = None
def presentPossibleUsers(self, persons, language): root = AddViews(self.refId, False, False, "Clarification", [], []) root.views.append(AssistantUtteranceView(responses['select'][language], responses['select'][language], "ContactDataResolutionDucs#disambiguateContact", True)) lst = DisambiguationList([], "OK!", True, "OK!", speakableDemitter[language], ", ", "OK!") root.views.append(lst) for person in persons: item = ListItem(person.fullName, person.fullName, [], person.fullName, person) item.commands.append(SendCommands([StartRequest(False, "^phoneCallContactId^=^urn:ace:{0}".format(person.identifier))])) lst.items.append(item) return root
def relatedNamesAction(plugin, personsData, relation, language): root = UIAddViews(plugin.refId) root.scrollToTop = False root.temporary = False root.dialogPhase = "Clarification" root.views = [] root.callbacks = [] assistant = UIAssistantUtteranceView() assistant.text = assistant.speakableText = text["select"][language] assistant.dialogIdentifier = "ContactDataResolutionDucs#disambiguateContact" assistant.listenAfterSpeaking = True root.views.append(assistant) lst = UIDisambiguationList() lst.items = [] lst.speakableSelectionResponse = "OK!" lst.listenAfterSpeaking = True lst.speakableText = "" lst.speakableFinalDemitter = speakableDemitter lst.speakableDemitter = ", " lst.selectionResponse = "OK!" root.views.append(lst) i = 0 for person in personsData: if person.label == relation: i += 1 if i > 0: if i == 1: for person in personsData: if person.label == relation: returnData = person.name else: for person in personsData: if person.label == relation: item = UIListItem() item.title = person.name item.selectionText = person.name item.commands = [] item.speakableText = person.name item.obj = person # # # # # should we not better use the identifier here? name is not really unique item.commands.append( SendCommands([StartRequest(False, person.name)])) lst.items.append(item) returnData = plugin.getResponseForRequest(root) return returnData
def findPhoneForNumberType(self, person, numberType, language): # first check if a specific number was already requested phoneToCall = None if numberType != None: # try to find the phone that fits the numberType phoneToCall = filter(lambda x: x.label == numberType, person.phones) else: favPhones = filter(lambda y: y.favoriteVoice if hasattr(y, "favoriteVoice") else False, person.phones) if len(favPhones) == 1: phoneToCall = favPhones[0] if phoneToCall == None: # lets check if there is more than one number if len(person.phones) == 1: if numberType != None: self.say(errorNumberNotPresent.format(numberTypesLocalized[numberType][language], person.fullName)) phoneToCall = person.phones[0] else: # damn we need to ask the user which one he wants... while(phoneToCall == None): rootView = AddViews(self.refId, temporary=False, dialogPhase="Clarification", scrollToTop=False, views=[]) sayit = responses['selectNumber'][language].format(person.fullName) rootView.views.append(AssistantUtteranceView(text=sayit, speakableText=sayit, listenAfterSpeaking=True,dialogIdentifier="ContactDataResolutionDucs#foundAmbiguousPhoneNumberForContact")) lst = DisambiguationList(items=[], speakableSelectionResponse="OK...", listenAfterSpeaking=True, speakableText="", speakableFinalDemitter=speakableDemitter[language], speakableDemitter=", ",selectionResponse="OK...") rootView.views.append(lst) for phone in person.phones: numberType = phone.label item = ListItem() item.title = "" item.text = u"{0}: {1}".format(numberTypesLocalized[numberType][language], phone.number) item.selectionText = item.text item.speakableText = u"{0} ".format(numberTypesLocalized[numberType][language]) item.object = phone item.commands.append(SendCommands(commands=[StartRequest(handsFree=False, utterance=numberTypesLocalized[numberType][language])])) lst.items.append(item) answer = self.getResponseForRequest(rootView) numberType = self.getNumberTypeForName(answer, language) if numberType != None: matches = filter(lambda x: x.label == numberType, person.phones) if len(matches) == 1: phoneToCall = matches[0] else: self.say(errorNumberTypes[language]) else: self.say(errorNumberTypes[language]) return phoneToCall
def presentPossibleUsers(plugin, persons, language): root = UIAddViews(plugin.refId) root.scrollToTop = False root.temporary = False root.dialogPhase = "Clarification" root.views = [] root.callbacks = [] assistant = UIAssistantUtteranceView() assistant.text = assistant.speakableText = text["select"][language] assistant.dialogIdentifier = "ContactDataResolutionDucs#disambiguateContact" assistant.listenAfterSpeaking = True root.views.append(assistant) lst = UIDisambiguationList() lst.items = [] lst.speakableSelectionResponse = "OK!" lst.listenAfterSpeaking = True lst.speakableText = "" lst.speakableFinalDemitter = speakableDemitter lst.speakableDemitter = ", " lst.selectionResponse = "OK!" root.views.append(lst) for person in persons: item = UIListItem() item.title = person.fullName item.selectionText = person.fullName item.speakableText = person.fullName item.obj = person #use the identifier here, it can distinquish better between users item.commands = [ SendCommands([ StartRequest( False, "^phoneCallContactId^=^urn:ace:{0}".format( person.identifier)) ]) ] lst.items.append(item) return root
def findMailForMailType(plugin, person, mailType, language): mail = None if mailType != None: mailToWrite = filter(lambda x: x.label == mailType, person.emails) else: favMails = filter( lambda y: y.favoriteVoice if hasattr(y, "favoriteVoice") else False, person.emails) if len(favMails) == 1: mail = favMails[0] if mail == None: if len(person.emails) == 1: if mailType != None: plugin.say(text["numberNotPresent"][language].format( mailTypes[language][mailType], person.fullName)) mail = person.emails[0] else: while (mail == None): rootView = UIAddViews(plugin.refId) rootView.temporary = False rootView.dialogPhase = "Clarification" rootView.scrollToTop = False rootView.views = [] sayit = text['selectMail'][language].format(person.fullName) assistant = UIAssistantUtteranceView() assistant.text = assistant.speakableText = sayit assistant.listenAfterSpeaking = True assistant.dialogIdentifier = "ContactDataResolutionDucs#foundAmbiguousMailForContact" rootView.views.append(assistant) lst = UIDisambiguationList() lst.items = [] lst.speakableSelectionResponse = "OK..." lst.listenAfterSpeaking = True lst.speakableText = "" lst.speakableFinalDemitter = speakableDemitter[language] lst.speakableDemitter = ", " lst.selectionResponse = "OK..." rootView.views.append(lst) for email in person.emails: mailType = email.label item = UIListItem() item.title = "" item.text = u"{0}: {1}".format( mailTypes[language][mailType], email.emailAddress) item.selectionText = item.text item.speakableText = u"{0} ".format( mailTypes[language][mailType]) item.object = email item.commands = [] item.commands.append( SendCommands(commands=[ StartRequest(handsFree=False, utterance=mailTypes[language] [mailType]) ])) lst.items.append(item) answer = plugin.getResponseForRequest(rootView) answer = replaceMailType(answer, language) mailType = answer if mailType != None: matches = filter(lambda x: x.label == mailType, person.emails) if len(matches) == 1: mail = matches[0] else: plugin.say(text['errorNumberTypes'][language]) else: plugin.say(text['errorNumberTypes'][language]) return mail
def findPhoneForNumberType(plugin, person, numberType, language): number = None if numberType != None: phoneToCall = filter(lambda x: x.label == numberType, person.phones) else: favPhones = filter( lambda y: y.favoriteVoice if hasattr(y, "favoriteVoice") else False, person.phones) if len(favPhones) == 1: number = favPhones[0] if number == None: if len(person.phones) == 1: if numberType != None: plugin.say(text["numberNotPresent"][language].format( numberTypes[language][numberType], person.fullName)) number = person.phones[0] else: while (number == None): rootView = UIAddViews(plugin.refId) rootView.temporary = False rootView.dialogPhase = "Clarification" rootView.scrollToTop = False rootView.views = [] sayit = text['selectNumber'][language].format(person.fullName) assistant = UIAssistantUtteranceView() assistant.text = assistant.speakableText = sayit assistant.listenAfterSpeaking = True assistant.dialogIdentifier = "ContactDataResolutionDucs#foundAmbiguousPhoneNumberForContact" rootView.views.append(assistant) lst = UIDisambiguationList() lst.items = [] lst.speakableSelectionResponse = "OK..." lst.listenAfterSpeaking = True lst.speakableText = "" lst.speakableFinalDemitter = speakableDemitter[language] lst.speakableDemitter = ", " lst.selectionResponse = "OK..." rootView.views.append(lst) for phone in person.phones: numberType = numberTypesLocalized[phone.label][ language] if phone.label in numberTypesLocalized else phone.label item = UIListItem() item.title = "" item.text = u"{0}: {1}".format(numberType, phone.number) item.selectionText = item.text item.speakableText = u"{0} ".format(numberType) item.object = phone item.commands = [ SendCommands(commands=[ StartRequest(handsFree=False, utterance=numberType) ]) ] lst.items.append(item) answer = plugin.getResponseForRequest(rootView) answer = getNumberTypeForName(answer, language) numberType = answer if numberType != None: matches = filter(lambda x: x.label == numberType, person.phones) if len(matches) == 1: number = matches[0] else: plugin.say(text['errorNumberTypes'][language]) else: plugin.say(text['errorNumberTypes'][language]) return number
def findPhoneForNumberType(self, person, numberType, language): # first check if a specific number was already requested phoneToMessage = None if numberType != None: # try to find the phone that fits the numberType phoneToMessage = filter(lambda x: x.label == numberType, person.phones) else: favPhones = filter( lambda y: y.favoriteVoice if hasattr(y, "favoriteVoice") else False, person.phones) if len(favPhones) == 1: phoneToMessage = favPhones[0] if phoneToMessage == None: # lets check if there is more than one number if len(person.phones) == 1: if numberType != None: self.say( errorNumberNotPresent.format( numberTypesLocalized[numberType][language], person.fullName)) phoneToMessage = person.phones[0] else: # damn we need to ask the user which one he wants... while (phoneToMessage == None): root = UIAddViews(self.refId) root.dialogPhase = root.DialogPhaseClarificationValue utterance = UIAssistantUtteranceView() utterance.dialogIdentifier = "ContactDataResolutionDucs#foundAmbiguousPhoneNumberForContact" utterance.speakableText = utterance.text = responses[ 'selectNumber'][language].format(person.fullName) utterance.listenAfterSpeaking = True root.views = [utterance] lst = UIDisambiguationList() lst.items = [] lst.speakableSelectionResponse = "OK!" lst.listenAfterSpeaking = True lst.selectionResponse = "OK" root.views.append(lst) for phone in person.phones: numberType = numberTypesLocalized[phone.label][ language] if phone.label in numberTypesLocalized else phone.label item = UIListItem() item.title = "" item.text = u"{0}: {1}".format(numberType, phone.number) item.selectionText = item.text item.speakableText = u"{0} ".format(numberType) item.object = phone item.commands = [ SendCommands(commands=[ StartRequest(handsFree=False, utterance=numberType) ]) ] lst.items.append(item) answer = self.getResponseForRequest(root) numberType = self.getNumberTypeForName(answer, language) if numberType != None: matches = filter(lambda x: x.label == numberType, person.phones) if len(matches) == 1: phoneToMessage = matches[0] else: self.say(errorNumberTypes[language]) else: self.say(errorNumberTypes[language]) return phoneToMessage