Skip to content

Commit

Permalink
Trying to fix PS #289, can't test right now
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychokiller1888 committed Aug 21, 2020
1 parent f0b7d4d commit 3e51c5b
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions core/asr/model/PocketSphinxAsr.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e51c5b

Please sign in to comment.