Plugins/UiExtensionPlugins/Translator/TranslatorEngines/GoogleV1Engine.py

changeset 6018
1c858879d3d0
child 6048
82ad8ec9548c
equal deleted inserted replaced
6017:dab01678626d 6018:1c858879d3d0
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2014 - 2017 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
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 def engineName(self):
41 """
42 Public method to return the name of the engine.
43
44 @return engine name (string)
45 """
46 return "googlev1"
47
48 def supportedLanguages(self):
49 """
50 Public method to get the supported languages.
51
52 @return list of supported language codes (list of string)
53 """
54 return ["ar", "be", "bg", "bs", "ca", "cs", "da", "de", "el", "en",
55 "es", "et", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id",
56 "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "mt",
57 "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr",
58 "sv", "th", "tl", "tr", "uk", "vi", "zh-CN", "zh-TW",
59 ]
60
61 def hasTTS(self):
62 """
63 Public method indicating the Text-to-Speech capability.
64
65 @return flag indicating the Text-to-Speech capability (boolean)
66 """
67 return False # doesn't work reliably
68
69 def getTranslation(self, requestObject, text, originalLanguage,
70 translationLanguage):
71 """
72 Public method to translate the given text.
73
74 @param requestObject reference to the request object
75 (TranslatorRequest)
76 @param text text to be translated (string)
77 @param originalLanguage language code of the original (string)
78 @param translationLanguage language code of the translation (string)
79 @return tuple of translated text (string) and flag indicating
80 success (boolean)
81 """
82 params = QByteArray(
83 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format(
84 originalLanguage, translationLanguage).encode("utf-8"))
85 encodedText = QByteArray(Utilities.html_encode(text).encode("utf-8"))\
86 .toPercentEncoding()
87 request = params + encodedText
88 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request)
89 if ok:
90 try:
91 # clean up the response
92 response = re.sub(r',{2,}', ',', response)
93 responseDict = json.loads(response)
94 except ValueError:
95 return self.tr("Invalid response received"), False
96
97 if isinstance(responseDict, dict):
98 sentences = responseDict["sentences"]
99 result = ""
100 for sentence in sentences:
101 result += sentence["trans"].replace("\n", "<br/>")
102
103 if self.plugin.getPreferences("GoogleEnableDictionary") and \
104 "dict" in responseDict:
105 dictionary = responseDict["dict"]
106 for value in dictionary:
107 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format(
108 text, value["pos"])
109 for entry in value["entry"]:
110 if "previous_word" in entry:
111 previous = entry["previous_word"] + " "
112 else:
113 previous = ""
114 word = entry["word"]
115 reverse = entry["reverse_translation"]
116 result += "<br/>{0}<b>{1}</b> - {2}".format(
117 previous, word, ", ".join(reverse))
118 if value != dictionary[-1]:
119 result += "<br/>"
120 elif isinstance(responseDict, list):
121 sentences = responseDict[0]
122 result = "".join([s[0] for s in sentences])\
123 .replace("\n", "<br/>")
124 if self.plugin.getPreferences("GoogleEnableDictionary") and \
125 len(responseDict) > 2:
126 if not responseDict[1]:
127 result = self.tr("No translation found.")
128 ok = False
129 else:
130 for wordTypeList in responseDict[1]:
131 result += "<hr/><u><b>{0}</b> - {1}</u>".format(
132 wordTypeList[0], wordTypeList[-2])
133 for wordsList in wordTypeList[2]:
134 reverse = wordsList[0]
135 words = wordsList[1]
136 result += "<br/><b>{0}</b> - {1}".format(
137 reverse, ", ".join(words))
138 else:
139 result = responseDict
140 else:
141 result = response
142 return result, ok
143
144 def getTextToSpeechData(self, requestObject, text, language):
145 """
146 Public method to pronounce the given text.
147
148 @param requestObject reference to the request object
149 (TranslatorRequest)
150 @param text text to be pronounced (string)
151 @param language language code of the text (string)
152 @return tuple with pronounce data (QByteArray) or error string (string)
153 and success flag (boolean)
154 """
155 text = text.split("\n\n", 1)[0]
156 if len(text) > self.TextToSpeechLimit:
157 return (self.tr("Only texts up to {0} characters are allowed.")
158 .format(self.TextToSpeechLimit), False)
159
160 url = QUrl(self.TextToSpeechUrl +
161 "?client=tw-ob&ie=utf-8&tl={0}&q={1}".format(
162 language, text))
163 return requestObject.get(url)

eric ide

mercurial