forked from Animenosekai/translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
276 lines (223 loc) · 9.23 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
Module containing various models for holding informations.
"""
from io import BytesIO
from json import dumps
from typing import Union
from translatepy.language import Language
# TODO: Feat: add raw model attribute, which will return a raw request response
# class Result:
# """
# A class which holds the result for any method
# """
# def __init__(self, service, source, source_language) -> None:
# pass
class TranslationResult:
"""
Class that holds the result of a Translation.
"""
def __init__(self, service, source, source_language, destination_language, result) -> None:
self.service = service
self.source = str(source)
self.source_language = Language(source_language)
self.destination_language = Language(destination_language)
self.result = str(result)
def __str__(self) -> str:
return self.result
def __repr__(self) -> str:
return "TranslationResult(service={service}, source={source}, source_language={source_language}, destination_language={destination_language}, result={result})".format(
service=self.service,
source=self.source,
source_language=self.source_language,
destination_language=self.destination_language,
result=self.result
)
def as_json(self, **kwargs) -> str:
return dumps({
"success": True,
"service": str(self.service),
"source": str(self.source),
"sourceLanguage": str((self.source_language.id) if isinstance(self.source_language, Language) else self.source_language),
"destinationLanguage": str((self.destination_language.id) if isinstance(self.destination_language, Language) else self.destination_language),
"result": str(self.result)
}, **kwargs)
class TransliterationResult:
"""
Class that holds the result of a Transliteration.
"""
def __init__(self, service, source, source_language, destination_language, result):
self.service = service
self.source = str(source)
self.source_language = Language(source_language)
self.destination_language = Language(destination_language)
self.result = str(result)
def __str__(self) -> str:
return self.result
def __repr__(self) -> str:
return "TransliterationResult(service={service}, source={source}, source_language={source_language}, destination_language={destination_language}, result={result})".format(
service=self.service,
source=self.source,
source_language=self.source_language,
destination_language=self.destination_language,
result=self.result
)
def as_json(self, **kwargs) -> str:
return dumps({
"success": True,
"service": str(self.service),
"source": str(self.source),
"sourceLanguage": str((self.source_language.id) if isinstance(self.source_language, Language) else self.source_language),
"destinationLanguage": str((self.destination_language.id) if isinstance(self.destination_language, Language) else self.destination_language),
"result": str(self.result),
}, **kwargs)
class SpellcheckResult:
"""
Class that holds the result of a Spellchecking.
"""
def __init__(self, service, source, source_language, result):
self.service = service
self.source = str(source)
self.source_language = Language(source_language)
self.result = str(result)
def __str__(self) -> str:
return self.result
def __repr__(self) -> str:
return "SpellcheckResult(service={service}, source={source}, source_language={source_language}, result={result})".format(
service=self.service,
source=self.source,
source_language=self.source_language,
result=self.result
)
def as_json(self, **kwargs) -> str:
return dumps({
"success": True,
"service": str(self.service),
"source": str(self.source),
"sourceLanguage": str((self.source_language.id) if isinstance(self.source_language, Language) else self.source_language),
"result": str(self.result),
}, **kwargs)
class LanguageResult:
"""
Class that holds the result of a Language.
"""
def __init__(self, service, source, result):
self.service = service
self.source = str(source)
self.result = Language(result)
def __str__(self) -> str:
return self.result
def __repr__(self) -> str:
return "LanguageResult(service={service}, source={source}, result={result})".format(
service=self.service,
source=self.source,
result=self.result
)
def as_json(self, **kwargs) -> str:
return dumps({
"success": True,
"service": str(self.service),
"source": str(self.source),
"result": str(self.result),
}, **kwargs)
class ExampleResult:
"""
Class that holds the result of a Example.
"""
def __init__(self, service, source, source_language, destination_language, result): # source_result, destination_result):
self.service = service
self.source = str(source)
self.source_language = Language(source_language)
self.destination_language = Language(destination_language)
self.result = result
# self.source_result = source_result
# self.destination_result = destination_result
def __str__(self) -> str:
# return self.source_result + self.destination_result
return self.result
def __repr__(self) -> str:
return "ExampleResult(service={service}, source={source}, source_language={source_language}, destination_language={destination_language}, result={result})".format(
service=self.service,
source=self.source,
source_language=self.source_language,
destination_language=self.destination_language,
result=self.result
)
def as_json(self, **kwargs) -> str:
return dumps({
"success": True,
"service": str(self.service),
"source": str(self.source),
"sourceLanguage": str((self.source_language.id) if isinstance(self.source_language, Language) else self.source_language),
"destinationLanguage": str((self.destination_language.id) if isinstance(self.destination_language, Language) else self.destination_language),
"result": str(self.result),
}, **kwargs)
class DictionaryResult:
"""
Class that holds the result of a Dictionary.
"""
def __init__(self, service, source, source_language, destination_language, result):
self.service = service
self.source = str(source)
self.source_language = Language(source_language)
self.destination_language = Language(destination_language)
self.result = result
def __str__(self) -> str:
return self.result
def __repr__(self) -> str:
return "DictionaryResult(service={service}, source={source}, source_language={source_language}, destination_language={destination_language}, result={result})".format(
service=self.service,
source=self.source,
source_language=self.source_language,
destination_language=self.destination_language,
result=self.result
)
def as_json(self, **kwargs) -> str:
return dumps({
"success": True,
"service": str(self.service),
"source": str(self.source),
"sourceLanguage": str((self.source_language.id) if isinstance(self.source_language, Language) else self.source_language),
"destinationLanguage": str((self.destination_language.id) if isinstance(self.destination_language, Language) else self.destination_language),
"result": str(self.result),
}, **kwargs)
class TextToSpechResult:
"""
Class that holds the result of a text to spech.
"""
def __init__(self, service, source, source_language, speed, gender, result):
self.service = service
self.source = str(source)
self.source_language = Language(source_language)
self.speed = int(speed),
self.gender = str(gender)
self.result = result
def write_to_file(self, file: Union[str, BytesIO]):
"""
Writes the spoken text to an MP3 file.
Args:
file: The output file
"""
if hasattr(file, "write"):
file.write(self.result)
return
with open(str(file), "wb") as fp:
fp.write(self.result)
return
def __str__(self) -> str:
return self.result
def __repr__(self) -> str:
return "TextToSpechResult(service={service}, source={source}, source_language={source_language}, speed={speed}, gender={gender}, result=<RawAudioFile>)".format(
service=self.service,
source=self.source,
source_language=self.source_language,
speed=self.speed,
gender=self.gender
)
class Speed():
FULL = 100
HALF = 50
QUARTER = 25
SLOW = HALF
class Gender():
MALE = "male"
FEMALE = "female"