~metalune/simplytranslate-devel

engines: Add Reverso as a translation engine v1 SUPERSEDED

: 1
 Add Reverso as a translation engine

 1 files changed, 72 insertions(+), 0 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/27453/mbox | git am -3
Learn more about email & git

[PATCH engines] Add Reverso as a translation engine Export this patch

From: Austin Huang <austinhuang@disroot.org>

---
 simplytranslate_engines/reverso.py | 72 ++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 simplytranslate_engines/reverso.py

diff --git a/simplytranslate_engines/reverso.py b/simplytranslate_engines/reverso.py
new file mode 100644
index 0000000..af283d0
--- /dev/null
+++ b/simplytranslate_engines/reverso.py
@@ -0,0 +1,72 @@
import requests


class ReversoTranslateEngine:
    name = "reverso"
    display_name = "Reverso"

    def get_supported_languages(self):
        return {
            "Arabic": "ara",
            "Chinese (Simplified)": "chi",  # marketed as just "Chinese"
            "Dutch": "dut",
            "English": "eng",
            "French": "fra",
            "German": "ger",
            "Hebrew": "heb",
            "Italian": "ita",
            "Japanese": "jpn",
            "Korean": "kor",
            "Polish": "pol",
            "Portuguese": "por",
            "Romanian": "rum",
            "Russian": "rus",
            "Spanish": "spa",
            "Swedish": "swe",
            "Turkish": "tur",
            "Ukrainian": "ukr",
        }

    def call_api(self, text, to_language, from_language):
        r = requests.post(
            "https://api.reverso.net/translate/v1/translation",
            json={
                "format": "text",
                "from": from_language,
                "to": to_language,
                "input": text,
                "options": {
                    "sentenceSplitter": True,
                    "origin": "translation.web",
                    "contextResults": True,
                    "languageDetection": True,
                },
            },
            headers={
                "Content-Type": "application/json",
                "User-Agent": "",  # Either empty or a browser User-Agent
            },
        ).json()
        return r

    def detect_language(self, text):
        # Any language pair works here, does not affect result
        r = self.call_api(text, "eng", "fra")
        return r["languageDetection"]["detectedLanguage"]

    def get_tts(self, text, language):
        return None

    def translate(self, text, to_language, from_language="auto"):
        if from_language == "auto":
            from_language = self.detect_language(text)
        r = self.call_api(text, to_language, from_language)
        return r["translation"][0]


if __name__ == "__main__":
    print(
        ReversoTranslateEngine().translate(
            "there is an impostor among us", "rus", "eng"
        )
    )
-- 
2.33.0