Skip to content

Commit

Permalink
Added in hotword support for cloudspeech
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Bisulco committed Oct 27, 2017
1 parent cb558fa commit 0b4972b
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/aiy/cloudspeech.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,51 @@ class _CloudSpeechRecognizer(object):
def __init__(self, credentials_file):
self._request = aiy._apis._speech.CloudSpeechRequest(credentials_file)
self._recorder = aiy.audio.get_recorder()
self._hotwords = []

def recognize(self):
"""Recognizes the user's speech and transcript it into text.
This function listens to the user's speech via the VoiceHat speaker. Then it
contacts Google CloudSpeech APIs and returns a textual transcript if possible.
If hotword list is populated this method will only respond if hotword is said.
"""
self._request.reset()
self._request.set_endpointer_cb(self._endpointer_callback)
self._recorder.add_processor(self._request)
return self._request.do_request().transcript
text = self._request.do_request().transcript
if self._hotwords and text:
text = text.lower()
loc_min = len(text)
hotword_found = ''
for hotword in self._hotwords:
loc_temp = text.find(hotword)
if loc_temp > -1 and loc_min > loc_temp:
loc_min = loc_temp
hotword_found = hotword
if hotword_found:
parse_text = text.split(hotword_found)[1]
return parse_text.strip()
else:
return ''
else:
return '' if self._hotwords else text

def expect_hotword(self, hotword_list):
"""Enables hotword detection for a selected list
This method is optional and populates the list of hotwords
to be used for hotword activation.
For example, to create a recognizer for Google:
recognizer.expect_hotword('Google')
recognizer.expect_hotword(['Google','Raspberry Pi'])
"""
if isinstance(hotword_list, list):
for hotword in hotword_list:
self._hotwords.append(hotword.lower())
else:
self._hotwords.append(hotword_list.lower())

def expect_phrase(self, phrase):
"""Explicitly tells the engine that the phrase is more likely to appear.
Expand Down

0 comments on commit 0b4972b

Please sign in to comment.