Somewhat fancy voice command recognition software
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

shell.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. elif numt in self.commands:
  33. self.emit('processed', text)
  34. cmd = self.commands[numt]
  35. cmd = cmd.format(*nums)
  36. self._run_command(cmd)
  37. return True
  38. else:
  39. # TODO: This could be implemented as a plugin, implicitly loaded
  40. # last
  41. # Run the invalid_sentence_command if it's set
  42. if self.options['invalid_sentence_command']:
  43. subprocess.call(self.options['invalid_sentence_command'],
  44. shell=True)
  45. print("no matching command {0}".format(text))
  46. return False
  47. # TODO: Make the D-Bus interface so this can leave the main process
  48. ## If there is a UI and we are not continuous listen
  49. #if self.ui:
  50. # if not self.continuous_listen:
  51. # # Stop listening
  52. # self.recognizer.pause()
  53. # # Let the UI know that there is a finish
  54. # self.ui.finished(text)