Somewhat fancy voice command recognition software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 3.7KB

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