__init__.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Queue
  2. import threading
  3. import console
  4. import google_cloud_speech
  5. import slack_bot
  6. LANGUAGE = 'en-GB'
  7. PREFERRED_PHRASES = None
  8. _TOKEN_STREAM = None
  9. class TokenStream(object):
  10. def __init__(self):
  11. self.preferred_phrases = None
  12. self.buffer = Queue.Queue()
  13. def add_token(self, token):
  14. self.buffer.put(token)
  15. def get_token(self, timeout):
  16. try:
  17. return self.buffer.get(timeout=timeout).strip().lower()
  18. except Queue.Empty:
  19. return None
  20. def get_line(self, timeout):
  21. return self.get_token(timeout)
  22. def launch_input_sources():
  23. global _TOKEN_STREAM
  24. _TOKEN_STREAM = TokenStream()
  25. services = [
  26. console.ConsoleInput(_TOKEN_STREAM),
  27. #google_cloud_speech.GoogleCloudSpeech(_TOKEN_STREAM),
  28. slack_bot.SlackBot(_TOKEN_STREAM),
  29. ]
  30. for s in services:
  31. t = threading.Thread(target=s.run)
  32. t.setDaemon(True)
  33. t.start()
  34. def get_token(timeout=120, preferred_phrases=None):
  35. global PREFERRED_PHRASES
  36. PREFERRED_PHRASES = preferred_phrases
  37. return _TOKEN_STREAM.get_token(timeout)
  38. def get_line(timeout=120, preferred_phrases=None):
  39. global PREFERRED_PHRASES
  40. PREFERRED_PHRASES = preferred_phrases
  41. return _TOKEN_STREAM.get_line(timeout)
  42. def token_available():
  43. return not _TOKEN_STREAM.buffer.empty()
  44. def get_until_terminated(timeout=120, terminators=['done', 'cancel'], preferred_phrases=None):
  45. global PREFERRED_PHRASES
  46. PREFERRED_PHRASES = preferred_phrases
  47. res = []
  48. t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
  49. while t not in terminators:
  50. res.append(t)
  51. t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
  52. return res, t