/
LanguageTranslator.swift
548 lines (487 loc) · 21.4 KB
/
LanguageTranslator.swift
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/**
* Copyright IBM Corporation 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
// swiftlint:disable file_length
import Foundation
import RestKit
/**
IBM Watson™ Language Translator translates text from one language to another. The service offers multiple IBM
provided translation models that you can customize based on your unique terminology and language. Use Language
Translator to take news from across the globe and present it in your language, communicate with your customers in their
own language, and more.
*/
public class LanguageTranslator {
/// The base URL to use when contacting the service.
public var serviceURL = "https://gateway.watsonplatform.net/language-translator/api"
/// The default HTTP headers for all requests to the service.
public var defaultHeaders = [String: String]()
private let session = URLSession(configuration: URLSessionConfiguration.default)
private var authMethod: AuthenticationMethod
private let domain = "com.ibm.watson.developer-cloud.LanguageTranslatorV3"
private let version: String
/**
Create a `LanguageTranslator` object.
- parameter username: The username used to authenticate with the service.
- parameter password: The password used to authenticate with the service.
- parameter version: The release date of the version of the API to use. Specify the date
in "YYYY-MM-DD" format.
*/
public init(username: String, password: String, version: String) {
self.authMethod = Shared.getAuthMethod(username: username, password: password)
self.version = version
Shared.configureRestRequest()
}
/**
Create a `LanguageTranslator` object.
- parameter version: The release date of the version of the API to use. Specify the date
in "YYYY-MM-DD" format.
- parameter apiKey: An API key for IAM that can be used to obtain access tokens for the service.
- parameter iamUrl: The URL for the IAM service.
*/
public init(version: String, apiKey: String, iamUrl: String? = nil) {
self.authMethod = Shared.getAuthMethod(apiKey: apiKey, iamURL: iamUrl)
self.version = version
Shared.configureRestRequest()
}
/**
Create a `LanguageTranslator` object.
- parameter version: The release date of the version of the API to use. Specify the date
in "YYYY-MM-DD" format.
- parameter accessToken: An access token for the service.
*/
public init(version: String, accessToken: String) {
self.authMethod = IAMAccessToken(accessToken: accessToken)
self.version = version
Shared.configureRestRequest()
}
public func accessToken(_ newToken: String) {
if self.authMethod is IAMAccessToken {
self.authMethod = IAMAccessToken(accessToken: newToken)
}
}
/**
If the response or data represents an error returned by the Language Translator service,
then return NSError with information about the error that occured. Otherwise, return nil.
- parameter data: Raw data returned from the service that may represent an error.
- parameter response: the URL response returned from the service.
*/
private func errorResponseDecoder(data: Data, response: HTTPURLResponse) -> Error {
let code = response.statusCode
do {
let json = try JSONDecoder().decode([String: JSON].self, from: data)
var userInfo: [String: Any] = [:]
if case let .some(.string(message)) = json["error"] {
userInfo[NSLocalizedDescriptionKey] = message
}
return NSError(domain: domain, code: code, userInfo: userInfo)
} catch {
return NSError(domain: domain, code: code, userInfo: nil)
}
}
/**
Translate.
Translates the input text from the source language to the target language.
- parameter request: The translate request containing the text, and either a model ID or source and target
language pair.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func translate(
request: TranslateRequest,
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (TranslationResult) -> Void)
{
// construct body
guard let body = try? JSONEncoder().encode(request) else {
failure?(RestError.serializationError)
return
}
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
headerParameters["Content-Type"] = "application/json"
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
// construct REST request
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "POST",
url: serviceURL + "/v3/translate",
headerParameters: headerParameters,
queryItems: queryParameters,
messageBody: body
)
// execute REST request
request.responseObject {
(response: RestResponse<TranslationResult>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
/**
List identifiable languages.
Lists the languages that the service can identify. Returns the language code (for example, `en` for English or `es`
for Spanish) and name of each language.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func listIdentifiableLanguages(
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (IdentifiableLanguages) -> Void)
{
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
// construct REST request
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "GET",
url: serviceURL + "/v3/identifiable_languages",
headerParameters: headerParameters,
queryItems: queryParameters
)
// execute REST request
request.responseObject {
(response: RestResponse<IdentifiableLanguages>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
/**
Identify language.
Identifies the language of the input text.
- parameter text: Input text in UTF-8 format.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func identify(
text: String,
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (IdentifiedLanguages) -> Void)
{
// construct body
// convert body parameter to NSData with UTF-8 encoding
guard let body = text.data(using: .utf8) else {
let message = "text could not be encoded to NSData with NSUTF8StringEncoding."
let userInfo = [NSLocalizedDescriptionKey: message]
let error = NSError(domain: domain, code: 0, userInfo: userInfo)
failure?(error)
return
}
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
headerParameters["Content-Type"] = "text/plain"
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
// construct REST request
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "POST",
url: serviceURL + "/v3/identify",
headerParameters: headerParameters,
queryItems: queryParameters,
messageBody: body
)
// execute REST request
request.responseObject {
(response: RestResponse<IdentifiedLanguages>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
/**
List models.
Lists available translation models.
- parameter source: Specify a language code to filter results by source language.
- parameter target: Specify a language code to filter results by target language.
- parameter defaultModels: If the default parameter isn't specified, the service will return all models (default
and non-default) for each language pair. To return only default models, set this to `true`. To return only
non-default models, set this to `false`. There is exactly one default model per language pair, the IBM provided
base model.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func listModels(
source: String? = nil,
target: String? = nil,
defaultModels: Bool? = nil,
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (TranslationModels) -> Void)
{
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
if let source = source {
let queryParameter = URLQueryItem(name: "source", value: source)
queryParameters.append(queryParameter)
}
if let target = target {
let queryParameter = URLQueryItem(name: "target", value: target)
queryParameters.append(queryParameter)
}
if let defaultModels = defaultModels {
let queryParameter = URLQueryItem(name: "default", value: "\(defaultModels)")
queryParameters.append(queryParameter)
}
// construct REST request
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "GET",
url: serviceURL + "/v3/models",
headerParameters: headerParameters,
queryItems: queryParameters
)
// execute REST request
request.responseObject {
(response: RestResponse<TranslationModels>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
/**
Create model.
Uploads Translation Memory eXchange (TMX) files to customize a translation model.
You can either customize a model with a forced glossary or with a corpus that contains parallel sentences. To
create a model that is customized with a parallel corpus <b>and</b> a forced glossary, proceed in two steps:
customize with a parallel corpus first and then customize the resulting model with a glossary. Depending on the
type of customization and the size of the uploaded corpora, training can range from minutes for a glossary to
several hours for a large parallel corpus. You can upload a single forced glossary file and this file must be less
than <b>10 MB</b>. You can upload multiple parallel corpora tmx files. The cumulative file size of all uploaded
files is limited to <b>250 MB</b>. To successfully train with a parallel corpus you must have at least <b>5,000
parallel sentences</b> in your corpus.
You can have a <b>maxium of 10 custom models per language pair</b>.
- parameter baseModelID: The model ID of the model to use as the base for customization. To see available models,
use the `List models` method. Usually all IBM provided models are customizable. In addition, all your models that
have been created via parallel corpus customization, can be further customized with a forced glossary.
- parameter name: An optional model name that you can use to identify the model. Valid characters are letters,
numbers, dashes, underscores, spaces and apostrophes. The maximum length is 32 characters.
- parameter forcedGlossary: A TMX file with your customizations. The customizations in the file completely
overwrite the domain translaton data, including high frequency or high confidence phrase translations. You can
upload only one glossary with a file size less than 10 MB per call. A forced glossary should contain single words
or short phrases.
- parameter parallelCorpus: A TMX file with parallel sentences for source and target language. You can upload
multiple parallel_corpus files in one request. All uploaded parallel_corpus files combined, your parallel corpus
must contain at least 5,000 parallel sentences to train successfully.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func createModel(
baseModelID: String,
name: String? = nil,
forcedGlossary: URL? = nil,
parallelCorpus: URL? = nil,
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (TranslationModel) -> Void)
{
// construct body
let multipartFormData = MultipartFormData()
if let forcedGlossary = forcedGlossary {
do {
try multipartFormData.append(file: forcedGlossary, withName: "forced_glossary")
} catch {
failure?(error)
return
}
}
if let parallelCorpus = parallelCorpus {
do {
try multipartFormData.append(file: parallelCorpus, withName: "parallel_corpus")
} catch {
failure?(error)
return
}
}
guard let body = try? multipartFormData.toData() else {
failure?(RestError.encodingError)
return
}
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
headerParameters["Content-Type"] = multipartFormData.contentType
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
queryParameters.append(URLQueryItem(name: "base_model_id", value: baseModelID))
if let name = name {
let queryParameter = URLQueryItem(name: "name", value: name)
queryParameters.append(queryParameter)
}
// construct REST request
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "POST",
url: serviceURL + "/v3/models",
headerParameters: headerParameters,
queryItems: queryParameters,
messageBody: body
)
// execute REST request
request.responseObject {
(response: RestResponse<TranslationModel>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
/**
Delete model.
Deletes a custom translation model.
- parameter modelID: Model ID of the model to delete.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func deleteModel(
modelID: String,
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (DeleteModelResult) -> Void)
{
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
// construct REST request
let path = "/v3/models/\(modelID)"
guard let encodedPath = path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
failure?(RestError.encodingError)
return
}
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "DELETE",
url: serviceURL + encodedPath,
headerParameters: headerParameters,
queryItems: queryParameters
)
// execute REST request
request.responseObject {
(response: RestResponse<DeleteModelResult>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
/**
Get model details.
Gets information about a translation model, including training status for custom models. Use this API call to poll
the status of your customization request. A successfully completed training will have a status of `available`.
- parameter modelID: Model ID of the model to get.
- parameter headers: A dictionary of request headers to be sent with this request.
- parameter failure: A function executed if an error occurs.
- parameter success: A function executed with the successful result.
*/
public func getModel(
modelID: String,
headers: [String: String]? = nil,
failure: ((Error) -> Void)? = nil,
success: @escaping (TranslationModel) -> Void)
{
// construct header parameters
var headerParameters = defaultHeaders
if let headers = headers {
headerParameters.merge(headers) { (_, new) in new }
}
headerParameters["Accept"] = "application/json"
// construct query parameters
var queryParameters = [URLQueryItem]()
queryParameters.append(URLQueryItem(name: "version", value: version))
// construct REST request
let path = "/v3/models/\(modelID)"
guard let encodedPath = path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
failure?(RestError.encodingError)
return
}
let request = RestRequest(
session: session,
authMethod: authMethod,
errorResponseDecoder: errorResponseDecoder,
method: "GET",
url: serviceURL + encodedPath,
headerParameters: headerParameters,
queryItems: queryParameters
)
// execute REST request
request.responseObject {
(response: RestResponse<TranslationModel>) in
switch response.result {
case .success(let retval): success(retval)
case .failure(let error): failure?(error)
}
}
}
}