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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2013 Jezra
  4. # Copyright 2015 Clayton G. Hobbs
  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. # Prepare request
  40. files = {'corpus': open(self.config.strings_file, 'rb')}
  41. values = {'formtype': 'simple'}
  42. # Send corpus to the server
  43. r = requests.post(url, files=files, data=values)
  44. # Parse response to get URLs of the files we need
  45. for line in r.text.split('\n'):
  46. # If we found the directory, keep it and don't break
  47. if re.search(r'.*<title>Index of (.*?)</title>.*', line):
  48. path = host + re.sub(r'.*<title>Index of (.*?)</title>.*', r'\1', line)
  49. # If we found the number, keep it and break
  50. elif re.search(r'.*TAR[0-9]*?\.tgz.*', line):
  51. number = re.sub(r'.*TAR([0-9]*?)\.tgz.*', r'\1', line)
  52. break
  53. lm_url = path + '/' + number + '.lm'
  54. dic_url = path + '/' + number + '.dic'
  55. self._download_file(lm_url, self.config.lang_file)
  56. self._download_file(dic_url, self.config.dic_file)
  57. def save_language_hash(self):
  58. new_hashes = {'language': self.new_hash}
  59. with open(self.config.hash_file, 'w') as f:
  60. json.dump(new_hashes, f)
  61. def _download_file(self, url, path):
  62. r = requests.get(url, stream=True)
  63. if r.status_code == 200:
  64. with open(path, 'wb') as f:
  65. for chunk in r:
  66. f.write(chunk)