~metalune/simplytranslate-devel

simplytranslate_engines: Adding Definitions to Google v1 SUPERSEDED

ManeraKai: 1
 simplytranslate_engines: Adding Definitions to Google

 5 files changed, 160 insertions(+), 26 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~metalune/simplytranslate-devel/patches/28980/mbox | git am -3
Learn more about email & git

[PATCH] simplytranslate_engines: Adding Definitions to Google Export this patch

---
 simplytranslate_engines/deepl.py           |   5 +-
 simplytranslate_engines/googletranslate.py | 151 +++++++++++++++++++--
 simplytranslate_engines/icibatranslate.py  |   9 +-
 simplytranslate_engines/libretranslate.py  |  12 +-
 simplytranslate_engines/reverso.py         |   9 +-
 5 files changed, 160 insertions(+), 26 deletions(-)

diff --git a/simplytranslate_engines/deepl.py b/simplytranslate_engines/deepl.py
index 32eb823..3629b7a 100644
--- a/simplytranslate_engines/deepl.py
+++ b/simplytranslate_engines/deepl.py
@@ -53,11 +53,12 @@ class DeeplEngine:
        return None

    def translate(self, text, to_language, from_language="auto"):
        myMap = {}

        # NOTE: this takes insanely long to process
        translation = self.deepl.translate(text, to_language, from_language)

        return translation
        myMap['translated-text'] = translation
        return myMap

if __name__ == "__main__":
    engine = DeeplEngine()
diff --git a/simplytranslate_engines/googletranslate.py b/simplytranslate_engines/googletranslate.py
index c4680c4..49f7c71 100644
--- a/simplytranslate_engines/googletranslate.py
+++ b/simplytranslate_engines/googletranslate.py
@@ -1,7 +1,9 @@
import lxml.html as lxml
from urllib.parse import urlencode
from urllib.parse import urlencode, quote
import requests
import json
import re


class GoogleTranslateEngine:
    name = "google"
@@ -182,22 +184,145 @@ class GoogleTranslateEngine:
    """

    def translate(self, text, to_language, from_language="auto"):
        r = requests.get(
            "https://translate.google.com/m",
            params = {
                "tl": to_language,
                "hl": to_language,
                "q": text
            }
        )
        myMap = {}
        try:
            url = "https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&rt=c"

        doc = lxml.fromstring(r.text)
        for container in doc.find_class("result-container"):
            return container.text_content()
            req = json.dumps(
                [[text, from_language, to_language, True], [None]])
            req = [[["MkEWBc", req, None, "generic"]]]
            req = "f.req=" + quote(json.dumps(req))  # URL encode this

        return ""
            r = requests.post(url,
                            headers={
                                'Content-Type': 'application/x-www-form-urlencoded'},
                            data=req)

            num_match = re.search(r"\n(\d+)\n", r.text)
            front_pad = num_match.span()[1]
            end_num = front_pad + int(num_match.groups()[0]) - 1

            data = json.loads(r.text[front_pad:end_num])
            data = data[0][2]
            data = json.loads(data)

            translation_data = data[1][0][0]
            translation_data = translation_data[len(translation_data)-1]

            myList = []
            for item in translation_data:
                myList.append(item[0])

            translation = " ".join(myList).replace("\n ", "\n")

            myMap["translated-text"] = translation
            try:
                myMap["definitions"] = {}
                for x in range(0, len(data[3][1][0])):
                    definition_type = data[3][1][0][x][0]
                    myMap["definitions"][definition_type] = []
                    for i in range(0, len(data[3][1][0][x][1])):
                        myMap["definitions"][definition_type].append({})
                        definition_box = data[3][1][0][x][1][i]

                        try:
                            dictionary = definition_box[4][0][0]
                            myMap["definitions"][definition_type][i]["dictionary"] = dictionary
                        except:
                            pass

                        try:
                            definition = definition_box[0]
                            myMap["definitions"][definition_type][i]["definition"] = definition
                        except:
                            pass

                        try:
                            use_in_sentence = definition_box[1]
                            if use_in_sentence is not None:
                                myMap["definitions"][definition_type][i]["use-in-sentence"] = use_in_sentence
                        except:
                            pass

                        try:
                            synonyms = definition_box[5]
                            myMap["definitions"][definition_type][i]["synonyms"] = {}
                            for synonym_box in synonyms:
                                synonym_type = ""
                                try:
                                    synonym_type = synonym_box[1][0][0]
                                except:
                                    pass
                                myMap["definitions"][definition_type][i]["synonyms"][synonym_type] = [
                                ]

                                try:
                                    synonym_list = synonym_box[0]
                                    for synonym_type_word in synonym_list:
                                        try:
                                            synonym_type_word_string = synonym_type_word[0]
                                            myMap["definitions"][definition_type][i]["synonyms"][synonym_type].append(
                                                synonym_type_word_string)
                                        except:
                                            pass
                                except:
                                    pass

                        except:
                            pass
            except:
                pass

            try:
                translation_box = data[3][5][0]
                myMap["translations"] = {}
                for x in range(0, len(translation_box)):
                    try:
                        translation_type = translation_box[x][0]
                        myMap["translations"][translation_type] = {}
                        translation_names_box = translation_box[x][1]
                        for i in range(0, len(translation_names_box)):
                            myMap["translations"][translation_type][translation_names_box[i][0]] = {}
                            frequency = str(translation_names_box[i][3])
                            if frequency == "3":
                                frequency = "1"
                            elif frequency == "1":
                                frequency = "3"

                            myMap["translations"][translation_type][translation_names_box[i][0]]["words"] = []
                            for z in range(0, len(translation_names_box[i][2])):
                                myMap["translations"][translation_type][translation_names_box[i][0]]["words"].append(
                                    translation_names_box[i][2][z])

                            myMap["translations"][translation_type][translation_names_box[i]
                                                                                [0]]["frequency"] = frequency+"/3"
                    except:
                        pass

            except:
                pass
        except :
            pass

        if myMap['translated-text'] is None:
            r = requests.get(
                "https://translate.google.com/m",
                params = {
                    "tl": to_language,
                    "hl": to_language,
                    "q": text
                }
            )

            doc = lxml.fromstring(r.text)
            for container in doc.find_class("result-container"):
                myMap['translated-text'] = container.text_content()

        return myMap

if __name__ == "__main__":
    print(GoogleTranslateEngine().translate("Hello Weird World!!\n\n\nHi!", "fr", "en"))

    data  = GoogleTranslateEngine().translate_advanced("House", "fr", "en")
    json_object = json.dumps(data, ensure_ascii=False, indent=4)
    print(json_object)
\ No newline at end of file
diff --git a/simplytranslate_engines/icibatranslate.py b/simplytranslate_engines/icibatranslate.py
index b5e63b6..b209f86 100644
--- a/simplytranslate_engines/icibatranslate.py
+++ b/simplytranslate_engines/icibatranslate.py
@@ -15,10 +15,10 @@ class IcibaTranslateEngine:
            # names in that engine file are used; Otherwise official names
            # as researched on Wikipedia are used. They're validated against
            # the Chinese names to the best of my ability.
            

            # Missing "cni", "kbh", "tmh"
            # due to conflict between ISO-639 table and Chinese label
            

            # one "#" means on iciba but not on google
            "Achinese": "ace", #
            "Achuar-Shiwiar": "acu", #
@@ -213,6 +213,7 @@ class IcibaTranslateEngine:
        return None

    def translate(self, text, to_language, from_language="auto"):
        myMap = {}
        r = requests.post(
            "https://ifanyi.iciba.com/index.php",
            params={
@@ -228,7 +229,9 @@ class IcibaTranslateEngine:
                "q": text
            }).json()

        return r['content']['out']
        myMap['translated-text'] = r['content']['out']
        return myMap


if __name__ == "__main__":
    print(IcibaTranslateEngine().translate("hello", "fr", "en"))
diff --git a/simplytranslate_engines/libretranslate.py b/simplytranslate_engines/libretranslate.py
index 6788cf6..e15b79b 100644
--- a/simplytranslate_engines/libretranslate.py
+++ b/simplytranslate_engines/libretranslate.py
@@ -46,6 +46,7 @@ class LibreTranslateEngine:
        return None

    def translate(self, text, to_language, from_language="auto"):
        myMap = {}
        form = {
            "q": text,
            "source": from_language,
@@ -59,11 +60,12 @@ class LibreTranslateEngine:

        response = json.loads(r.text)
        if "translatedText" in response:
            return response["translatedText"]
        elif "error" in response:
            return response["error"]
        else:
            return "odd, something went wrong"
            myMap['translated-text'] = response["translatedText"]
        # elif "error" in response:
        #     return
        # else:
        return myMap


if __name__ == "__main__":
    print(LibreTranslateEngine("https://libretranslate.de").translate("dies ist ein sehr einfacher und politisch korrekter test", "en"))
diff --git a/simplytranslate_engines/reverso.py b/simplytranslate_engines/reverso.py
index c14642b..7a20637 100644
--- a/simplytranslate_engines/reverso.py
+++ b/simplytranslate_engines/reverso.py
@@ -62,12 +62,15 @@ class ReversoTranslateEngine:
        return None

    def translate(self, text, to_language, from_language="auto"):
        myMap = {}
        if from_language == "auto":
            from_language = self.detect_language(text)
        if from_language == to_language:
            return text
        r = self.call_api(text, to_language, from_language)
        return r["translation"][0]
            myMap["translated-text"] = text
        else:
            r = self.call_api(text, to_language, from_language)
            myMap["translated-text"] = r["translation"][0]
        return myMap


if __name__ == "__main__":
-- 
2.25.1