Somewhat fancy voice command recognition software
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

util.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. import re
  6. import json
  7. import hashlib
  8. import os
  9. from argparse import ArgumentParser, Namespace
  10. from collections import OrderedDict
  11. import pkg_resources
  12. import requests
  13. from gi.repository import GLib
  14. class Config:
  15. """Keep track of the configuration of Kaylee"""
  16. # Name of the program, for later use
  17. program_name = "kaylee"
  18. # Directories
  19. conf_dir = os.path.join(GLib.get_user_config_dir(), program_name)
  20. cache_dir = os.path.join(GLib.get_user_cache_dir(), program_name)
  21. data_dir = os.path.join(GLib.get_user_data_dir(), program_name)
  22. # Configuration files
  23. default_opt_file = pkg_resources.resource_filename('kayleevc',
  24. 'conf/options.json')
  25. local_opt_file = os.path.join(conf_dir, "options.json")
  26. plugins_file = os.path.join(conf_dir, "plugins.json")
  27. # Cache files
  28. history_file = os.path.join(cache_dir, program_name + "history")
  29. hash_file = os.path.join(cache_dir, "hash.json")
  30. # Data files
  31. strings_file = os.path.join(data_dir, "sentences.corpus")
  32. lang_file = os.path.join(data_dir, 'lm')
  33. dic_file = os.path.join(data_dir, 'dic')
  34. def __init__(self):
  35. # Ensure necessary directories exist
  36. self._make_dir(self.conf_dir)
  37. self._make_dir(self.cache_dir)
  38. self._make_dir(self.data_dir)
  39. # Set up the argument parser
  40. self._parser = ArgumentParser()
  41. self._parser.add_argument("-i", "--interface", type=str,
  42. dest="interface", action='store',
  43. help="Interface to use (if any). 'g' for GTK.")
  44. self._parser.add_argument("-c", "--continuous",
  45. action="store_true", dest="continuous", default=False,
  46. help="Start interface with 'continuous' listen enabled")
  47. self._parser.add_argument("-H", "--history", type=int,
  48. action="store", dest="history",
  49. help="Number of commands to store in history file")
  50. self._parser.add_argument("-m", "--microphone", type=int,
  51. action="store", dest="microphone", default=None,
  52. help="Audio input card to use (if other than system default)")
  53. self._parser.add_argument("--valid-sentence-command", type=str,
  54. dest="valid_sentence_command", action='store',
  55. help="Command to run when a valid sentence is detected")
  56. self._parser.add_argument("--invalid-sentence-command", type=str,
  57. dest="invalid_sentence_command", action='store',
  58. help="Command to run when an invalid sentence is detected")
  59. # Read the configuration files
  60. self.options = self._read_json_file(self.default_opt_file)
  61. self.options.update(self._read_json_file(self.local_opt_file))
  62. self.options = Namespace(**self.options)
  63. # Parse command-line arguments, overriding config file as appropriate
  64. self._parser.parse_args(namespace=self.options)
  65. # Read the plugins file
  66. self.plugins = self._read_json_file(self.plugins_file)
  67. def _make_dir(self, directory):
  68. if not os.path.exists(directory):
  69. os.makedirs(directory)
  70. def _read_json_file(self, filename):
  71. try:
  72. with open(filename, 'r') as f:
  73. return json.load(f, object_pairs_hook=OrderedDict)
  74. except FileNotFoundError:
  75. return {}
  76. class Hasher:
  77. """Keep track of hashes for Kaylee"""
  78. def __init__(self, config):
  79. self.config = config
  80. try:
  81. with open(self.config.hash_file, 'r') as f:
  82. self.hashes = json.load(f)
  83. except IOError:
  84. # No stored hash
  85. self.hashes = {}
  86. def __getitem__(self, hashname):
  87. try:
  88. return self.hashes[hashname]
  89. except (KeyError, TypeError):
  90. return None
  91. def __setitem__(self, hashname, value):
  92. self.hashes[hashname] = value
  93. def get_hash_object(self):
  94. """Returns an object to compute a new hash"""
  95. return hashlib.sha256()
  96. def store(self):
  97. """Store the current hashes into a the hash file"""
  98. with open(self.config.hash_file, 'w') as f:
  99. json.dump(self.hashes, f)
  100. class LanguageUpdater:
  101. """
  102. Handles updating the language using the online lmtool.
  103. This class provides methods to check if the corpus has changed, and to
  104. update the language to match the new corpus using the lmtool. This allows
  105. us to automatically update the language if the corpus has changed, saving
  106. the user from having to do this manually.
  107. """
  108. def __init__(self, config):
  109. self.config = config
  110. self.hasher = Hasher(config)
  111. def update_language_if_changed(self):
  112. """Test if the language has changed, and if it has, update it"""
  113. if self.language_has_changed():
  114. self.update_language()
  115. self.save_language_hash()
  116. def language_has_changed(self):
  117. """Use hashes to test if the language has changed"""
  118. self.stored_hash = self.hasher['language']
  119. # Calculate the hash the language file has right now
  120. hasher = self.hasher.get_hash_object()
  121. with open(self.config.strings_file, 'rb') as sfile:
  122. buf = sfile.read()
  123. hasher.update(buf)
  124. self.new_hash = hasher.hexdigest()
  125. return self.new_hash != self.stored_hash
  126. def update_language(self):
  127. """Update the language using the online lmtool"""
  128. print('Updating language using online lmtool')
  129. host = 'http://www.speech.cs.cmu.edu'
  130. url = host + '/cgi-bin/tools/lmtool/run'
  131. # Submit the corpus to the lmtool
  132. response_text = ""
  133. with open(self.config.strings_file, 'rb') as corpus:
  134. files = {'corpus': corpus}
  135. values = {'formtype': 'simple'}
  136. r = requests.post(url, files=files, data=values)
  137. response_text = r.text
  138. # Parse response to get URLs of the files we need
  139. path_re = r'.*<title>Index of (.*?)</title>.*'
  140. number_re = r'.*TAR([0-9]*?)\.tgz.*'
  141. for line in response_text.split('\n'):
  142. # If we found the directory, keep it and don't break
  143. if re.search(path_re, line):
  144. path = host + re.sub(path_re, r'\1', line)
  145. # If we found the number, keep it and break
  146. elif re.search(number_re, line):
  147. number = re.sub(number_re, r'\1', line)
  148. break
  149. lm_url = path + '/' + number + '.lm'
  150. dic_url = path + '/' + number + '.dic'
  151. self._download_file(lm_url, self.config.lang_file)
  152. self._download_file(dic_url, self.config.dic_file)
  153. def save_language_hash(self):
  154. self.hasher['language'] = self.new_hash
  155. self.hasher.store()
  156. def _download_file(self, url, path):
  157. r = requests.get(url, stream=True)
  158. if r.status_code == 200:
  159. with open(path, 'wb') as f:
  160. for chunk in r:
  161. f.write(chunk)