Somewhat fancy voice command recognition software
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

shell.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2016 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. """Run shell commands that match voice commands
  6. This Kaylee plugin can be used to perform the job of `Blather
  7. <https://gitlab.com/jezra/blather>`__. Its configuration is of the
  8. following format::
  9. ".shell": {
  10. "VOICE_COMMAND": "SHELL_COMMAND",
  11. ...
  12. }
  13. A VOICE_COMMAND may contain any number of the special word ``%d``,
  14. which is a stand-in for a spoken number. The spoken number is
  15. transformed into a decimal integer which may be substituted into the
  16. SHELL_COMMAND any number of times. These substitutions are made by
  17. writing ``{INDEX}`` in the SHELL_COMMAND, where INDEX is an integer
  18. counting the ``%d`` strings from 0. An example of this type of command
  19. is::
  20. "%d is a number": "echo 'the number you said was {0}'"
  21. """
  22. import subprocess
  23. from .pluginbase import PluginBase
  24. from ..numbers import NumberParser
  25. class Plugin(PluginBase):
  26. """Run shell commands that match voice commands"""
  27. def __init__(self, config, name):
  28. """Initialize the shell plugin"""
  29. super().__init__(config, name)
  30. self.number_parser = NumberParser()
  31. self.commands = self.options
  32. for voice_cmd in self.commands.keys():
  33. self.corpus_strings.add(voice_cmd.strip().replace('%d', ''))
  34. def _run_command(self, cmd):
  35. """Print the command, then run it"""
  36. print(cmd)
  37. subprocess.call(cmd, shell=True)
  38. def recognizer_finished(self, recognizer, text):
  39. """Run the shell command corresponding to a recognized voice command
  40. Spoken number(s) may be substituted in the shell command. The shell
  41. commands are printed immediately before they are executed.
  42. """
  43. numt, nums = self.number_parser.parse_all_numbers(text)
  44. # Is there a matching command?
  45. if text in self.commands:
  46. self.emit('processed', text)
  47. cmd = self.commands[text]
  48. self._run_command(cmd)
  49. return True
  50. # Is there a matching command with numbers substituted?
  51. elif numt in self.commands:
  52. self.emit('processed', text)
  53. cmd = self.commands[numt]
  54. cmd = cmd.format(*nums)
  55. self._run_command(cmd)
  56. return True
  57. else:
  58. return False