| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import Queue
- import threading
- import console
- import google_cloud_speech
- import slack_bot
- LANGUAGE = 'en-GB'
- PREFERRED_PHRASES = None
- _TOKEN_STREAM = None
- class TokenStream(object):
- def __init__(self):
- self.preferred_phrases = None
- self.buffer = Queue.Queue()
-
- def add_token(self, token):
- self.buffer.put(token)
-
- def get_token(self, timeout):
- try:
- return self.buffer.get(timeout=timeout).strip().lower()
- except Queue.Empty:
- return None
- def get_line(self, timeout):
- return self.get_token(timeout)
- def launch_input_sources():
- global _TOKEN_STREAM
- _TOKEN_STREAM = TokenStream()
- services = [
- console.ConsoleInput(_TOKEN_STREAM),
- #google_cloud_speech.GoogleCloudSpeech(_TOKEN_STREAM),
- slack_bot.SlackBot(_TOKEN_STREAM),
- ]
- for s in services:
- t = threading.Thread(target=s.run)
- t.setDaemon(True)
- t.start()
- def get_token(timeout=120, preferred_phrases=None):
- global PREFERRED_PHRASES
- PREFERRED_PHRASES = preferred_phrases
- return _TOKEN_STREAM.get_token(timeout)
- def get_line(timeout=120, preferred_phrases=None):
- global PREFERRED_PHRASES
- PREFERRED_PHRASES = preferred_phrases
- return _TOKEN_STREAM.get_line(timeout)
- def token_available():
- return not _TOKEN_STREAM.buffer.empty()
- def get_until_terminated(timeout=120, terminators=['done', 'cancel'], preferred_phrases=None):
- global PREFERRED_PHRASES
- PREFERRED_PHRASES = preferred_phrases
- res = []
- t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
- while t not in terminators:
- res.append(t)
- t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
- return res, t
|