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.

languageupdater.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 hashlib
  6. import json
  7. import re
  8. import requests
  9. class LanguageUpdater:
  10. def __init__(self, config):
  11. self.config = config
  12. def update_language_if_changed(self):
  13. """Test if the language has changed, and if it has, update it"""
  14. if self.language_has_changed():
  15. self.update_language()
  16. self.save_language_hash()
  17. def language_has_changed(self):
  18. """Use SHA256 hashes to test if the language has changed"""
  19. # Load the stored hash from the hash file
  20. try:
  21. with open(self.config.hash_file, 'r') as f:
  22. hashes = json.load(f)
  23. self.stored_hash = hashes['language']
  24. except (IOError, KeyError, TypeError):
  25. # No stored hash
  26. self.stored_hash = ''
  27. # Calculate the hash the language file has right now
  28. hasher = hashlib.sha256()
  29. with open(self.config.strings_file, 'rb') as sfile:
  30. buf = sfile.read()
  31. hasher.update(buf)
  32. self.new_hash = hasher.hexdigest()
  33. return self.new_hash != self.stored_hash
  34. def update_language(self):
  35. """Update the language using the online lmtool"""
  36. print('Updating language using online lmtool')
  37. host = 'http://www.speech.cs.cmu.edu'
  38. url = host + '/cgi-bin/tools/lmtool/run'
  39. # Submit the corpus to the lmtool
  40. response_text = ""
  41. with open(self.config.strings_file, 'rb') as corpus:
  42. files = {'corpus': corpus}
  43. values = {'formtype': 'simple'}
  44. r = requests.post(url, files=files, data=values)
  45. response_text = r.text
  46. # Parse response to get URLs of the files we need
  47. path_re = r'.*<title>Index of (.*?)</title>.*'
  48. number_re = r'.*TAR([0-9]*?)\.tgz.*'
  49. for line in response_text.split('\n'):
  50. # If we found the directory, keep it and don't break
  51. if re.search(path_re, line):
  52. path = host + re.sub(path_re, r'\1', line)
  53. # If we found the number, keep it and break
  54. elif re.search(number_re, line):
  55. number = re.sub(number_re, r'\1', line)
  56. break
  57. lm_url = path + '/' + number + '.lm'
  58. dic_url = path + '/' + number + '.dic'
  59. self._download_file(lm_url, self.config.lang_file)
  60. self._download_file(dic_url, self.config.dic_file)
  61. def save_language_hash(self):
  62. new_hashes = {'language': self.new_hash}
  63. with open(self.config.hash_file, 'w') as f:
  64. json.dump(new_hashes, f)
  65. def _download_file(self, url, path):
  66. r = requests.get(url, stream=True)
  67. if r.status_code == 200:
  68. with open(path, 'wb') as f:
  69. for chunk in r:
  70. f.write(chunk)