From 3e51c5bef0442c0c89e0aea4d02a8bf4ac21dbe6 Mon Sep 17 00:00:00 2001 From: Psycho Date: Fri, 21 Aug 2020 06:31:14 +0200 Subject: [PATCH] Trying to fix PS #289, can't test right now --- core/asr/model/PocketSphinxAsr.py | 42 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/core/asr/model/PocketSphinxAsr.py b/core/asr/model/PocketSphinxAsr.py index de09e57f6..87b7cc41f 100644 --- a/core/asr/model/PocketSphinxAsr.py +++ b/core/asr/model/PocketSphinxAsr.py @@ -1,8 +1,9 @@ -import shutil -import tarfile from pathlib import Path from typing import Optional +import shutil +import tarfile + from core.asr.model.ASRResult import ASRResult from core.asr.model.Asr import Asr from core.asr.model.Recorder import Recorder @@ -73,26 +74,33 @@ def timeout(self): pass - def downloadLanguage(self) -> bool: - self.logInfo(f'Downloading language model for "{self.LanguageManager.activeLanguage}"') + def downloadLanguage(self, forceLang: str = '') -> bool: + lang = forceLang or self.LanguageManager.activeLanguageAndCountryCode + self.logInfo(f'Downloading language model for "{lang}"') venv = Path(self.Commons.rootDir(), 'venv/lib/python3.7/site-packages/pocketsphinx/') for url in self.LANGUAGE_PACK: - url = url.replace('%lang%', self.LanguageManager.activeLanguageAndCountryCode.lower()) + url = url.replace('%lang%', lang.lower()) filename = Path(url).name download = Path(venv, 'model', filename) - self.Commons.downloadFile(url=f'{url}?raw=true', dest=str(download)) - - if download.suffix == '.tar': - dest = Path(venv, 'model', self.LanguageManager.activeLanguageAndCountryCode.lower()) - - if dest.exists(): - shutil.rmtree(dest) - - tar = tarfile.open(str(download)) - tar.extractall(str(dest)) - - download.unlink() + result = self.Commons.downloadFile(url=f'{url}?raw=true', dest=str(download)) + if not result: + if forceLang: + return False + else: + # TODO be universal + self.downloadLanguage(forceLang='eu-US') + else: + if download.suffix == '.tar': + dest = Path(venv, 'model', lang.lower()) + + if dest.exists(): + shutil.rmtree(dest) + + tar = tarfile.open(str(download)) + tar.extractall(str(dest)) + + download.unlink() self.logInfo('Downloaded and installed') return True