eric6/Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV1Engine.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2014 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Google V1 translation engine.
8 """
9
10 from __future__ import unicode_literals
11
12 import json
13 import re
14
15 from PyQt5.QtCore import QByteArray, QUrl, QTimer
16
17 import Utilities
18
19 from .TranslationEngine import TranslationEngine
20
21
22 class GoogleV1Engine(TranslationEngine):
23 """
24 Class implementing the translation engine for the old Google
25 translation service.
26 """
27 TranslatorUrl = "https://translate.googleapis.com/translate_a/single"
28 TextToSpeechUrl = "https://translate.google.com/translate_tts"
29 TextToSpeechLimit = 100
30
31 def __init__(self, plugin, parent=None):
32 """
33 Constructor
34
35 @param plugin reference to the plugin object (TranslatorPlugin)
36 @param parent reference to the parent object (QObject)
37 """
38 super(GoogleV1Engine, self).__init__(plugin, parent)
39
40 QTimer.singleShot(0, self.availableTranslationsLoaded.emit)
41
42 def engineName(self):
43 """
44 Public method to return the name of the engine.
45
46 @return engine name (string)
47 """
48 return "googlev1"
49
50 def supportedLanguages(self):
51 """
52 Public method to get the supported languages.
53
54 @return list of supported language codes (list of string)
55 """
56 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
57 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
58 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
59 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
60 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW",
61 ]
62
63 def hasTTS(self):
64 """
65 Public method indicating the Text-to-Speech capability.
66
67 @return flag indicating the Text-to-Speech capability (boolean)
68 """
69 return False # doesn't work reliably
70
71 def getTranslation(self, requestObject, text, originalLanguage,
72 translationLanguage):
73 """
74 Public method to translate the given text.
75
76 @param requestObject reference to the request object
77 (TranslatorRequest)
78 @param text text to be translated (string)
79 @param originalLanguage language code of the original (string)
80 @param translationLanguage language code of the translation (string)
81 @return tuple of translated text (string) and flag indicating
82 success (boolean)
83 """
84 params = QByteArray(
85 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format(
86 originalLanguage, translationLanguage).encode("utf-8"))
87 encodedText = QByteArray(Utilities.html_encode(text).encode("utf-8"))\
88 .toPercentEncoding()
89 request = params + encodedText
90 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request)
91 if ok:
92 try:
93 # clean up the response
94 response = re.sub(r',{2,}', ',', response)
95 responseDict = json.loads(response)
96 except ValueError:
97 return self.tr("Invalid response received"), False
98
99 if isinstance(responseDict, dict):
100 sentences = responseDict["sentences"]
101 result = ""
102 for sentence in sentences:
103 result += sentence["trans"].replace("\n", "<br/>")
104
105 if self.plugin.getPreferences("GoogleEnableDictionary") and \
106 "dict" in responseDict:
107 dictionary = responseDict["dict"]
108 for value in dictionary:
109 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format(
110 text, value["pos"])
111 for entry in value["entry"]:
112 if "previous_word" in entry:
113 previous = entry["previous_word"] + " "
114 else:
115 previous = ""
116 word = entry["word"]
117 reverse = entry["reverse_translation"]
118 result += "<br/>{0}<b>{1}</b> - {2}".format(
119 previous, word, ", ".join(reverse))
120 if value != dictionary[-1]:
121 result += "<br/>"
122 elif isinstance(responseDict, list):
123 sentences = responseDict[0]
124 result = "".join([s[0] for s in sentences])\
125 .replace("\n", "<br/>")
126 if self.plugin.getPreferences("GoogleEnableDictionary") and \
127 len(responseDict) > 2:
128 if not responseDict[1]:
129 result = self.tr("No translation found.")
130 ok = False
131 else:
132 for wordTypeList in responseDict[1]:
133 result += "<hr/><u><b>{0}</b> - {1}</u>".format(
134 wordTypeList[0], wordTypeList[-2])
135 for wordsList in wordTypeList[2]:
136 reverse = wordsList[0]
137 words = wordsList[1]
138 result += "<br/><b>{0}</b> - {1}".format(
139 reverse, ", ".join(words))
140 else:
141 result = responseDict
142 else:
143 result = response
144 return result, ok
145
146 def getTextToSpeechData(self, requestObject, text, language):
147 """
148 Public method to pronounce the given text.
149
150 @param requestObject reference to the request object
151 (TranslatorRequest)
152 @param text text to be pronounced (string)
153 @param language language code of the text (string)
154 @return tuple with pronounce data (QByteArray) or error string (string)
155 and success flag (boolean)
156 """
157 text = text.split("\n\n", 1)[0]
158 if len(text) > self.TextToSpeechLimit:
159 return (self.tr("Only texts up to {0} characters are allowed.")
160 .format(self.TextToSpeechLimit), False)
161
162 url = QUrl(self.TextToSpeechUrl +
163 "?client=tw-ob&ie=utf-8&tl={0}&q={1}".format(
164 language, text))
165 return requestObject.get(url)

eric ide

mercurial