Somewhat fancy voice command recognition software
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

__init__.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. """
  54. def __eq__(self, other):
  55. if isinstance(other, Handler):
  56. return (self.confidence == other.confidence)
  57. return NotImplemented
  58. def __lt__(self, other):
  59. if isinstance(other, Handler):
  60. # Intentionally reversed
  61. return (self.confidence > other.confidence)
  62. return NotImplemented
  63. def __le__(self, other):
  64. if isinstance(other, Handler):
  65. # Intentionally reversed
  66. return (self.confidence >= other.confidence)
  67. return NotImplemented
  68. def __ge__(self, other):
  69. if isinstance(other, Handler):
  70. # Intentionally reversed
  71. return (self.confidence <= other.confidence)
  72. return NotImplemented
  73. def __gt__(self, other):
  74. if isinstance(other, Handler):
  75. # Intentionally reversed
  76. return (self.confidence < other.confidence)
  77. return NotImplemented