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

Tue, 10 Sep 2019 19:30:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 10 Sep 2019 19:30:07 +0200
changeset 7229
53054eb5b15a
parent 6942
2602857055c5
child 7360
9190402e4505
permissions
-rw-r--r--

Removed obsolete "from __future__ import ..." statements.

6018
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6412
diff changeset
3 # Copyright (c) 2014 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
6018
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the translation engine base class.
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
6412
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
11 from PyQt5.QtCore import pyqtSignal, QObject
6018
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 class TranslationEngine(QObject):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 Class implementing the translation engine base class containing
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 default methods.
6412
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
18
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
19 @signal availableTranslationsLoaded() emitted to indicate the availability
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
20 of the list of supported translation languages
6018
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
6412
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
22 availableTranslationsLoaded = pyqtSignal()
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
23
6018
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 def __init__(self, plugin, parent=None):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Constructor
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @param plugin reference to the plugin object (TranslatorPlugin)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param parent reference to the parent object (QObject)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 super(TranslationEngine, self).__init__(parent)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 self.plugin = plugin
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 def engineName(self):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 Public method to get the name of the engine.
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @return engine name (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 return ""
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 def supportedLanguages(self):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 Public method to get the supported languages.
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @return list of supported language codes (list of string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 return []
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
6411
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
51 def supportedTargetLanguages(self, original):
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
52 """
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
53 Public method to get a list of supported target languages for an
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
54 original language.
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
55
6412
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
56 Note: The default implementation return the list of supported languages
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
57 (i.e. the same as those for the source) with the given original
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
58 removed.
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
59
6411
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
60 @param original original language
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
61 @type str
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
62 @return list of supported target languages for the given original
6412
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
63 @rtype list of str
6411
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
64 """
6412
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
65 targetLanguages = self.supportedLanguages()[:]
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
66 try:
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
67 targetLanguages.remove(original)
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
68 except ValueError:
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
69 # original is not in the list of target languages
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
70 pass
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
71
d71b094845e7 Translator: improved the translator by making the loading of available translations an asynchroneous operation.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6411
diff changeset
72 return targetLanguages
6411
7fd9b7ecbcfe Translator: added a translation engine for the IBM Watson Language Translator.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
73
6018
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 def hasTTS(self):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 Public method indicating the Text-to-Speech capability.
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 @return flag indicating the Text-to-Speech capability (boolean)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 return False
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 def getTextToSpeechData(self, requestObject, text, language):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 Public method to pronounce the given text.
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 @param requestObject reference to the request object
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 (TranslatorRequest)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 @param text text to be pronounced (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 @param language language code of the text (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 @return tuple with pronounce data (QByteArray) or error string (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 and success flag (boolean)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 return self.tr("No pronounce data available"), False
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 def getTranslation(self, requestObject, text, originalLanguage,
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 translationLanguage):
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 Public method to translate the given text.
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 @param requestObject reference to the request object
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 (TranslatorRequest)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 @param text text to be translated (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 @param originalLanguage language code of the original (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 @param translationLanguage language code of the translation (string)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 @return tuple of translated text (string) and flag indicating
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 success (boolean)
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 """
1c858879d3d0 Added the translator plug-in to the core plug-ins.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 return self.tr("No translation available"), False

eric ide

mercurial