From 9deefbbc849c01ff3010dfc15072d962e4727c97 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sun, 11 Dec 2022 00:45:14 -0500 Subject: [PATCH] Builds/runs natively on Apple M1 --- app/__init__.py | 10 ------- app/detect.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ app/language.py | 2 +- requirements.txt | 4 +-- 4 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 app/detect.py diff --git a/app/__init__.py b/app/__init__.py index 208da59..982fb0b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,13 +1,3 @@ import os - -# override polyglot path -import polyglot -from appdirs import user_data_dir - -polyglot.polyglot_path = os.path.join( - user_data_dir(appname="LibreTranslate", appauthor="uav4geo"), "polyglot_data" -) - - from .main import main from .manage import manage diff --git a/app/detect.py b/app/detect.py new file mode 100644 index 0000000..1b43087 --- /dev/null +++ b/app/detect.py @@ -0,0 +1,72 @@ +# Originally adapted from https://github.com/aboSamoor/polyglot/blob/master/polyglot/base.py + +import pycld2 as cld2 + +class UnknownLanguage(Exception): + pass + +class Language(object): + def __init__(self, choice): + name, code, confidence, bytesize = choice + self.code = code + self.name = name + self.confidence = float(confidence) + self.read_bytes = int(bytesize) + + def __str__(self): + return ("name: {:<12}code: {:<9}confidence: {:>5.1f} " + "read bytes:{:>6}".format(self.name, self.code, + self.confidence, self.read_bytes)) + + @staticmethod + def from_code(code): + return Language(("", code, 100, 0)) + + +class Detector(object): + """ Detect the language used in a snippet of text.""" + + def __init__(self, text, quiet=False): + """ Detector of the language used in `text`. + Args: + text (string): unicode string. + """ + self.__text = text + self.reliable = True + """False if the detector used Best Effort strategy in detection.""" + self.quiet = quiet + """If true, exceptions will be silenced.""" + self.detect(text) + + @staticmethod + def supported_languages(): + """Returns a list of the languages that can be detected by pycld2.""" + return [name.capitalize() for name,code in cld2.LANGUAGES if not name.startswith("X_")] + + def detect(self, text): + """Decide which language is used to write the text. + The method tries first to detect the language with high reliability. If + that is not possible, the method switches to best effort strategy. + Args: + text (string): A snippet of text, the longer it is the more reliable we + can detect the language used to write the text. + """ + reliable, index, top_3_choices = cld2.detect(text, bestEffort=False) + + if not reliable: + self.reliable = False + reliable, index, top_3_choices = cld2.detect(text, bestEffort=True) + + if not self.quiet: + if not reliable: + raise UnknownLanguage("Try passing a longer snippet of text") + + self.languages = [Language(x) for x in top_3_choices] + self.language = self.languages[0] + return self.language + + def __str__(self): + text = "Prediction is reliable: {}\n".format(self.reliable) + text += u"\n".join(["Language {}: {}".format(i+1, str(l)) + for i,l in enumerate(self.languages)]) + return text \ No newline at end of file diff --git a/app/language.py b/app/language.py index 2e358c2..05696b6 100644 --- a/app/language.py +++ b/app/language.py @@ -1,7 +1,7 @@ import string from argostranslate import translate -from polyglot.detect.base import Detector, UnknownLanguage +from app.detect import Detector, UnknownLanguage __languages = None diff --git a/requirements.txt b/requirements.txt index 39a300f..062976f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,10 +5,8 @@ flask-swagger-ui==4.11.1 Flask-Limiter==2.6.3 waitress==2.1.2 expiringdict==1.2.2 -pyicu>=2.8 -pycld2==0.41 +LTpycld2==0.42 morfessor==2.0.6 -polyglot==16.7.4 appdirs==1.4.4 APScheduler==3.9.1 translatehtml==1.5.2