From e0693d697e65e6d620433c7476f8bca5f15cc7b7 Mon Sep 17 00:00:00 2001 From: mammo0 Date: Thu, 11 Mar 2021 12:33:54 +0100 Subject: [PATCH] download the transliteration packages of polyglot during boot --- app/init.py | 29 +++++++++++++++++++++++++++-- install_models.py | 3 ++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/init.py b/app/init.py index c540c1c..8b4ef65 100644 --- a/app/init.py +++ b/app/init.py @@ -2,9 +2,12 @@ import os from pathlib import Path from argostranslate import settings, package, translate import os, glob, shutil, zipfile +from app.language import languages +import polyglot def boot(): - check_and_install_models() + check_and_install_models() + check_and_install_transliteration() def check_and_install_models(force=False): if len(package.get_installed_packages()) < 2 or force: @@ -23,4 +26,26 @@ def check_and_install_models(force=False): package.install_from_path(download_path) print("Loaded support for %s languages (%s models total)!" % (len(translate.load_installed_languages()), len(available_packages))) - \ No newline at end of file + + +def check_and_install_transliteration(force=False): + # 'en' is not a supported transliteration language + transliteration_languages = [l.code for l in languages if l.code != "en"] + + # check installed + install_needed = [] + if not force: + t_packages_path = Path(polyglot.polyglot_path) / "transliteration2" + for lang in transliteration_languages: + if not (t_packages_path / lang / f"transliteration.{lang}.tar.bz2").exists(): + install_needed.append(lang) + else: + install_needed = transliteration_languages + + # install the needed transliteration packages + if install_needed: + from polyglot.downloader import Downloader + downloader = Downloader() + + for lang in install_needed: + downloader.download(f"transliteration2.{lang}") diff --git a/install_models.py b/install_models.py index 5492f1a..4768c3c 100755 --- a/install_models.py +++ b/install_models.py @@ -1,6 +1,7 @@ #!/usr/bin/env python -from app.init import check_and_install_models +from app.init import check_and_install_models, check_and_install_transliteration if __name__ == "__main__": check_and_install_models(force=True) + check_and_install_transliteration(force=True)