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.

shell.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. import subprocess
  6. from .pluginbase import PluginBase
  7. from ..numbers import NumberParser
  8. class Plugin(PluginBase):
  9. def __init__(self, config, name):
  10. super().__init__(config, name)
  11. self.options = vars(self.config.options)
  12. self.number_parser = NumberParser()
  13. self.commands = self.options['plugins'][name]
  14. for voice_cmd in self.commands.keys():
  15. self.corpus_strings.add(voice_cmd.strip().replace('%d', ''))
  16. def _run_command(self, cmd):
  17. """Print the command, then run it"""
  18. print(cmd)
  19. subprocess.call(cmd, shell=True)
  20. def recognizer_finished(self, recognizer, text):
  21. """Process a recognized voice command
  22. Recognized commands have the corresponding shell commands executed,
  23. possibly with number(s) substituted.
  24. """
  25. numt, nums = self.number_parser.parse_all_numbers(text)
  26. # Is there a matching command?
  27. if text in self.commands:
  28. self.emit('processed', text)
  29. cmd = self.commands[text]
  30. self._run_command(cmd)
  31. return True
  32. # Is there a matching command with numbers substituted?
  33. elif numt in self.commands:
  34. self.emit('processed', text)
  35. cmd = self.commands[numt]
  36. cmd = cmd.format(*nums)
  37. self._run_command(cmd)
  38. return True
  39. else:
  40. return False