From 6f2126aa28d7715a8b65b4c7e427c72e32a6c28b Mon Sep 17 00:00:00 2001 From: schneefux Date: Sun, 28 Sep 2014 20:51:50 +0200 Subject: Use config dir for non-temporary writable files This is also a (partial) fix for issue jasperproject/jasper-client#119 --- client/dictionary_persona.dic | 22 ---------- client/languagemodel_persona.lm | 97 ----------------------------------------- client/populate.py | 7 ++- client/stt.py | 4 +- jasper.py | 50 +++++++++++++++------ static/dictionary_persona.dic | 22 ++++++++++ static/languagemodel_persona.lm | 97 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+), 136 deletions(-) delete mode 100644 client/dictionary_persona.dic delete mode 100644 client/languagemodel_persona.lm create mode 100644 static/dictionary_persona.dic create mode 100644 static/languagemodel_persona.lm diff --git a/client/dictionary_persona.dic b/client/dictionary_persona.dic deleted file mode 100644 index 8b813ac..0000000 --- a/client/dictionary_persona.dic +++ /dev/null @@ -1,22 +0,0 @@ -BE B IY -BEING B IY IH NG -BUT B AH T -DID D IH D -FIRST F ER S T -IN IH N -IS IH Z -IT IH T -JASPER JH AE S P ER -NOW N AW -OF AH V -ON AA N -ON(2) AO N -RIGHT R AY T -SAY S EY -WHAT W AH T -WHAT(2) HH W AH T -WHICH W IH CH -WHICH(2) HH W IH CH -WITH W IH DH -WITH(2) W IH TH -WORK W ER K \ No newline at end of file diff --git a/client/languagemodel_persona.lm b/client/languagemodel_persona.lm deleted file mode 100644 index f290a31..0000000 --- a/client/languagemodel_persona.lm +++ /dev/null @@ -1,97 +0,0 @@ -Language model created by QuickLM on Wed Sep 4 14:21:33 EDT 2013 -Copyright (c) 1996-2010 Carnegie Mellon University and Alexander I. Rudnicky - -The model is in standard ARPA format, designed by Doug Paul while he was at MITRE. - -The code that was used to produce this language model is available in Open Source. -Please visit http://www.speech.cs.cmu.edu/tools/ for more information - -The (fixed) discount mass is 0.5. The backoffs are computed using the ratio method. -This model based on a corpus of 18 sentences and 20 words - -\data\ -ngram 1=20 -ngram 2=36 -ngram 3=18 - -\1-grams: --0.7782 -0.3010 --0.7782 -0.2218 --2.0334 BE -0.2218 --2.0334 BEING -0.2218 --2.0334 BUT -0.2218 --2.0334 DID -0.2218 --2.0334 FIRST -0.2218 --2.0334 IN -0.2218 --2.0334 IS -0.2218 --2.0334 IT -0.2218 --2.0334 JASPER -0.2218 --2.0334 NOW -0.2218 --2.0334 OF -0.2218 --2.0334 ON -0.2218 --2.0334 RIGHT -0.2218 --2.0334 SAY -0.2218 --2.0334 WHAT -0.2218 --2.0334 WHICH -0.2218 --2.0334 WITH -0.2218 --2.0334 WORK -0.2218 - -\2-grams: --1.5563 BE 0.0000 --1.5563 BEING 0.0000 --1.5563 BUT 0.0000 --1.5563 DID 0.0000 --1.5563 FIRST 0.0000 --1.5563 IN 0.0000 --1.5563 IS 0.0000 --1.5563 IT 0.0000 --1.5563 JASPER 0.0000 --1.5563 NOW 0.0000 --1.5563 OF 0.0000 --1.5563 ON 0.0000 --1.5563 RIGHT 0.0000 --1.5563 SAY 0.0000 --1.5563 WHAT 0.0000 --1.5563 WHICH 0.0000 --1.5563 WITH 0.0000 --1.5563 WORK 0.0000 --0.3010 BE -0.3010 --0.3010 BEING -0.3010 --0.3010 BUT -0.3010 --0.3010 DID -0.3010 --0.3010 FIRST -0.3010 --0.3010 IN -0.3010 --0.3010 IS -0.3010 --0.3010 IT -0.3010 --0.3010 JASPER -0.3010 --0.3010 NOW -0.3010 --0.3010 OF -0.3010 --0.3010 ON -0.3010 --0.3010 RIGHT -0.3010 --0.3010 SAY -0.3010 --0.3010 WHAT -0.3010 --0.3010 WHICH -0.3010 --0.3010 WITH -0.3010 --0.3010 WORK -0.3010 - -\3-grams: --0.3010 BE --0.3010 BEING --0.3010 BUT --0.3010 DID --0.3010 FIRST --0.3010 IN --0.3010 IS --0.3010 IT --0.3010 JASPER --0.3010 NOW --0.3010 OF --0.3010 ON --0.3010 RIGHT --0.3010 SAY --0.3010 WHAT --0.3010 WHICH --0.3010 WITH --0.3010 WORK - -\end\ \ No newline at end of file diff --git a/client/populate.py b/client/populate.py index d1cbb80..e871f26 100644 --- a/client/populate.py +++ b/client/populate.py @@ -1,10 +1,11 @@ # -*- coding: utf-8-*- +import os import re from getpass import getpass import yaml from pytz import timezone import feedparser - +import jasperpath def run(): profile = {} @@ -105,7 +106,9 @@ def run(): # write to profile print("Writing to profile...") - outputFile = open("profile.yml", "w") + if not os.path.exists(jasperpath.CONFIG_PATH): + os.makedirs(jasperpath.CONFIG_PATH) + outputFile = open(jasperpath.config("profile.yml"), "w") yaml.dump(profile, outputFile, default_flow_style=False) print("Done.") diff --git a/client/stt.py b/client/stt.py index b4cc038..344696c 100644 --- a/client/stt.py +++ b/client/stt.py @@ -42,8 +42,8 @@ class PocketSphinxSTT(AbstractSTTEngine): SLUG = 'sphinx' - def __init__(self, lmd="languagemodel.lm", dictd="dictionary.dic", - lmd_persona="languagemodel_persona.lm", dictd_persona="dictionary_persona.dic", + def __init__(self, lmd=jasperpath.config("languagemodel.lm"), dictd=jasperpath.config("dictionary.dic"), + lmd_persona=jasperpath.data("languagemodel_persona.lm"), dictd_persona=jasperpath.data("dictionary_persona.dic"), lmd_music=None, dictd_music=None, hmm_dir="/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k"): """ diff --git a/jasper.py b/jasper.py index 93fd3c0..4217bbe 100755 --- a/jasper.py +++ b/jasper.py @@ -34,11 +34,41 @@ os.chdir(jasperpath.LIB_PATH) class Jasper(object): def __init__(self): self._logger = logging.getLogger(__name__) + + # Create config dir if it does not exist yet + if not os.path.exists(jasperpath.CONFIG_PATH): + try: + os.makedirs(jasperpath.CONFIG_PATH) + except OSError: + self._logger.error("Could not create config dir: '%s'", jasperpath.CONFIG_PATH, exc_info=True) + raise + + # Check if config dir is writable + if not os.access(jasperpath.CONFIG_PATH, os.W_OK): + self._logger.critical("Config dir %s is not writable. Jasper won't work correctly.") + + # FIXME: For backwards compatibility, move old config file to newly created config dir + old_configfile = os.path.join(jasperpath.LIB_PATH, 'profile.yml') + new_configfile = jasperpath.config('profile.yml') + if os.path.exists(old_configfile): + if os.path.exists(new_configfile): + self._logger.warning("Deprecated profile file found: '%s'. Please remove it.", old_configfile) + else: + self._logger.warning("Deprecated profile file found: '%s'. Trying to copy it to new location '%s'.", old_configfile, new_configfile) + try: + shutil.copy2(old_configfile, new_configfile) + except shutil.Error: + self._logger.error("Unable to copy config file. Please copy it manually.", exc_info=True) + raise + # Read config - config_file = os.path.abspath(os.path.join(jasperpath.LIB_PATH, 'profile.yml')) - self._logger.debug("Trying to read config file: '%s'", config_file) - with open(config_file, "r") as f: - self.config = yaml.safe_load(f) + self._logger.debug("Trying to read config file: '%s'", new_configfile) + try: + with open(new_configfile, "r") as f: + self.config = yaml.safe_load(f) + except OSError: + self._logger.error("Can't open config file: '%s'", new_configfile) + raise try: api_key = self.config['keys']['GOOGLE_SPEECH'] @@ -59,7 +89,7 @@ class Jasper(object): tts_engine_class = tts.get_engine_by_slug(tts_engine_slug) # Compile dictionary - sentences, dictionary, languagemodel = [os.path.abspath(os.path.join(jasperpath.LIB_PATH, filename)) for filename in ("sentences.txt", "dictionary.dic", "languagemodel.lm")] + sentences, dictionary, languagemodel = [jasperpath.config(filename) for filename in ("sentences.txt", "dictionary.dic", "languagemodel.lm")] vocabcompiler.compile(sentences, dictionary, languagemodel) # Initialize Mic @@ -93,14 +123,8 @@ if __name__ == "__main__": try: app = Jasper() - except IOError: - logger.exception("Can't read profile file.") - sys.exit(1) - except OSError: - logger.exception("Language model or associated files missing.") - sys.exit(1) - except Exception(): - logger.exception("Unknown error occured") + except Exception: + logger.exception("Error occured!", exc_info=True) sys.exit(1) app.run() diff --git a/static/dictionary_persona.dic b/static/dictionary_persona.dic new file mode 100644 index 0000000..8b813ac --- /dev/null +++ b/static/dictionary_persona.dic @@ -0,0 +1,22 @@ +BE B IY +BEING B IY IH NG +BUT B AH T +DID D IH D +FIRST F ER S T +IN IH N +IS IH Z +IT IH T +JASPER JH AE S P ER +NOW N AW +OF AH V +ON AA N +ON(2) AO N +RIGHT R AY T +SAY S EY +WHAT W AH T +WHAT(2) HH W AH T +WHICH W IH CH +WHICH(2) HH W IH CH +WITH W IH DH +WITH(2) W IH TH +WORK W ER K \ No newline at end of file diff --git a/static/languagemodel_persona.lm b/static/languagemodel_persona.lm new file mode 100644 index 0000000..f290a31 --- /dev/null +++ b/static/languagemodel_persona.lm @@ -0,0 +1,97 @@ +Language model created by QuickLM on Wed Sep 4 14:21:33 EDT 2013 +Copyright (c) 1996-2010 Carnegie Mellon University and Alexander I. Rudnicky + +The model is in standard ARPA format, designed by Doug Paul while he was at MITRE. + +The code that was used to produce this language model is available in Open Source. +Please visit http://www.speech.cs.cmu.edu/tools/ for more information + +The (fixed) discount mass is 0.5. The backoffs are computed using the ratio method. +This model based on a corpus of 18 sentences and 20 words + +\data\ +ngram 1=20 +ngram 2=36 +ngram 3=18 + +\1-grams: +-0.7782 -0.3010 +-0.7782 -0.2218 +-2.0334 BE -0.2218 +-2.0334 BEING -0.2218 +-2.0334 BUT -0.2218 +-2.0334 DID -0.2218 +-2.0334 FIRST -0.2218 +-2.0334 IN -0.2218 +-2.0334 IS -0.2218 +-2.0334 IT -0.2218 +-2.0334 JASPER -0.2218 +-2.0334 NOW -0.2218 +-2.0334 OF -0.2218 +-2.0334 ON -0.2218 +-2.0334 RIGHT -0.2218 +-2.0334 SAY -0.2218 +-2.0334 WHAT -0.2218 +-2.0334 WHICH -0.2218 +-2.0334 WITH -0.2218 +-2.0334 WORK -0.2218 + +\2-grams: +-1.5563 BE 0.0000 +-1.5563 BEING 0.0000 +-1.5563 BUT 0.0000 +-1.5563 DID 0.0000 +-1.5563 FIRST 0.0000 +-1.5563 IN 0.0000 +-1.5563 IS 0.0000 +-1.5563 IT 0.0000 +-1.5563 JASPER 0.0000 +-1.5563 NOW 0.0000 +-1.5563 OF 0.0000 +-1.5563 ON 0.0000 +-1.5563 RIGHT 0.0000 +-1.5563 SAY 0.0000 +-1.5563 WHAT 0.0000 +-1.5563 WHICH 0.0000 +-1.5563 WITH 0.0000 +-1.5563 WORK 0.0000 +-0.3010 BE -0.3010 +-0.3010 BEING -0.3010 +-0.3010 BUT -0.3010 +-0.3010 DID -0.3010 +-0.3010 FIRST -0.3010 +-0.3010 IN -0.3010 +-0.3010 IS -0.3010 +-0.3010 IT -0.3010 +-0.3010 JASPER -0.3010 +-0.3010 NOW -0.3010 +-0.3010 OF -0.3010 +-0.3010 ON -0.3010 +-0.3010 RIGHT -0.3010 +-0.3010 SAY -0.3010 +-0.3010 WHAT -0.3010 +-0.3010 WHICH -0.3010 +-0.3010 WITH -0.3010 +-0.3010 WORK -0.3010 + +\3-grams: +-0.3010 BE +-0.3010 BEING +-0.3010 BUT +-0.3010 DID +-0.3010 FIRST +-0.3010 IN +-0.3010 IS +-0.3010 IT +-0.3010 JASPER +-0.3010 NOW +-0.3010 OF +-0.3010 ON +-0.3010 RIGHT +-0.3010 SAY +-0.3010 WHAT +-0.3010 WHICH +-0.3010 WITH +-0.3010 WORK + +\end\ \ No newline at end of file -- cgit v1.3.1