Somewhat fancy voice command recognition software
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2017 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. class PluginBase():
  6. """Base class for Kaylee plugins
  7. Each Kaylee plugin module must define a subclass of this class, named
  8. ``Plugin``.
  9. """
  10. def __init__(self, config, name):
  11. """Initialize the plugin
  12. All strings to be included in the corpus should be elements of
  13. ``corpus_strings`` by the end of __init__'s execution. Note however
  14. that the words required for number support are automatically included,
  15. so they need not be listed explicitly on a per-plugin basis.
  16. """
  17. super().__init__()
  18. self.config = config
  19. self.name = name
  20. self.options = config.plugins[name]
  21. self.corpus_strings = set()
  22. def get_handler(self, text):
  23. """Return a handler for the given text
  24. This method returns a Handler object which handles the command when
  25. called. If the plugin does not recognize the command at all, this
  26. method must return None instead. The method must also return None if
  27. the command is recognized, but for some reason would not perform any
  28. action currently, e.g. "pause music" when no music player is running.
  29. """
  30. return None
  31. class Handler:
  32. """Base class for Kaylee plugin handlers
  33. Plugins should subclass this for their own command handlers.
  34. Rich comparison methods are defined for use with queue.PriorityQueue.
  35. They compare Handler objects based on their confidence alone, with
  36. Handlers of high confidence comparing less than ones of low
  37. confidence.
  38. """
  39. def __init__(self, confidence):
  40. """Initialize the handler
  41. The confidence with which this handler will handle the command must be
  42. passed as ``confidence``, a floating-point number in (0, 1]. A
  43. confidence of 1 indicates that the handler is definitely supposed to
  44. handle the command it was created for. Values between 0 and 1 may be
  45. used to indicate that the plugin may be able to handle the command,
  46. but it was misunderstood by the speech recognition system.
  47. """
  48. self.confidence = confidence
  49. def __call__(self, tts):
  50. """Handle the command
  51. When called, ``tts`` is a function which takes a string as its only
  52. parameter and passes it to Kaylee's configured text-to-speech system.
  53. If command handling fails for some reason that can be anticipated, this
  54. method must raise a HandlerFailure to tell Kaylee to keep trying other
  55. handlers.
  56. """
  57. def __eq__(self, other):
  58. if isinstance(other, Handler):
  59. return (self.confidence == other.confidence)
  60. return NotImplemented
  61. def __lt__(self, other):
  62. if isinstance(other, Handler):
  63. # Intentionally reversed
  64. return (self.confidence > other.confidence)
  65. return NotImplemented
  66. def __le__(self, other):
  67. if isinstance(other, Handler):
  68. # Intentionally reversed
  69. return (self.confidence >= other.confidence)
  70. return NotImplemented
  71. def __ge__(self, other):
  72. if isinstance(other, Handler):
  73. # Intentionally reversed
  74. return (self.confidence <= other.confidence)
  75. return NotImplemented
  76. def __gt__(self, other):
  77. if isinstance(other, Handler):
  78. # Intentionally reversed
  79. return (self.confidence < other.confidence)
  80. return NotImplemented
  81. class HandlerFailure(Exception):
  82. """Handler failed to handle its command."""