forked from watson-developer-cloud/dotnet-standard-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageTranslatorService.cs
430 lines (391 loc) · 19.7 KB
/
LanguageTranslatorService.cs
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
/**
* Copyright 2018 IBM Corp. All Rights Reserved.
*
* 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.
*
*/
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using IBM.WatsonDeveloperCloud.Http;
using IBM.WatsonDeveloperCloud.Http.Extensions;
using IBM.WatsonDeveloperCloud.LanguageTranslator.v2.Model;
using IBM.WatsonDeveloperCloud.Service;
using IBM.WatsonDeveloperCloud.Util;
using System;
namespace IBM.WatsonDeveloperCloud.LanguageTranslator.v2
{
public partial class LanguageTranslatorService : WatsonService, ILanguageTranslatorService
{
const string SERVICE_NAME = "language_translator";
const string URL = "https://gateway.watsonplatform.net/language-translator/api";
public LanguageTranslatorService() : base(SERVICE_NAME, URL)
{
Console.WriteLine("Language Translator v3 is now available. The v2 Language Translator API will no longer be available after July 31, 2018. To take advantage of the latest service enhancements, migrate to the v3 API. View the [Migrating to Language Translator v3](https://console.bluemix.net/docs/services/language-translator/migrating.html) page for more information.");
if(!string.IsNullOrEmpty(this.Endpoint))
this.Endpoint = URL;
}
public LanguageTranslatorService(string userName, string password) : this()
{
Console.WriteLine("Language Translator v3 is now available. The v2 Language Translator API will no longer be available after July 31, 2018. To take advantage of the latest service enhancements, migrate to the v3 API. View the [Migrating to Language Translator v3](https://console.bluemix.net/docs/services/language-translator/migrating.html) page for more information.");
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException(nameof(userName));
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(nameof(password));
this.SetCredential(userName, password);
}
public LanguageTranslatorService(TokenOptions options) : this()
{
Console.WriteLine("Language Translator v3 is now available. The v2 Language Translator API will no longer be available after July 31, 2018. To take advantage of the latest service enhancements, migrate to the v3 API. View the [Migrating to Language Translator v3](https://console.bluemix.net/docs/services/language-translator/migrating.html) page for more information.");
if (string.IsNullOrEmpty(options.IamApiKey) && string.IsNullOrEmpty(options.IamAccessToken))
throw new ArgumentNullException(nameof(options.IamAccessToken) + ", " + nameof(options.IamApiKey));
if (!string.IsNullOrEmpty(options.ServiceUrl))
{
this.Endpoint = options.ServiceUrl;
}
else
{
options.ServiceUrl = this.Endpoint;
}
_tokenManager = new TokenManager(options);
}
public LanguageTranslatorService(IClient httpClient) : this()
{
Console.WriteLine("Language Translator v3 is now available. The v2 Language Translator API will no longer be available after July 31, 2018. To take advantage of the latest service enhancements, migrate to the v3 API. View the [Migrating to Language Translator v3](https://console.bluemix.net/docs/services/language-translator/migrating.html) page for more information.");
if (httpClient == null)
throw new ArgumentNullException(nameof(httpClient));
this.Client = httpClient;
}
/// <summary>
/// Translate.
///
/// Translates the input text from the source language to the target language.
/// </summary>
/// <param name="request">The translate request containing the text, and either a model ID or source and target
/// language pair.</param>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="TranslationResult" />TranslationResult</returns>
public TranslationResult Translate(TranslateRequest request, Dictionary<string, object> customData = null)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
TranslationResult result = null;
try
{
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.PostAsync($"{this.Endpoint}/v2/translate");
restRequest.WithBody<TranslateRequest>(request);
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<TranslationResult>().Result;
if(result == null)
result = new TranslationResult();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
/// <summary>
/// Identify language.
///
/// Identifies the language of the input text.
/// </summary>
/// <param name="text">Input text in UTF-8 format.</param>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="IdentifiedLanguages" />IdentifiedLanguages</returns>
public IdentifiedLanguages Identify(string text, Dictionary<string, object> customData = null)
{
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException(nameof(text));
IdentifiedLanguages result = null;
try
{
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.PostAsync($"{this.Endpoint}/v2/identify");
restRequest.WithBodyContent(new StringContent(text, Encoding.UTF8, HttpMediaType.TEXT_PLAIN));
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<IdentifiedLanguages>().Result;
if(result == null)
result = new IdentifiedLanguages();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="IdentifiableLanguages" />IdentifiableLanguages</returns>
public IdentifiableLanguages ListIdentifiableLanguages(Dictionary<string, object> customData = null)
{
IdentifiableLanguages result = null;
try
{
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.GetAsync($"{this.Endpoint}/v2/identifiable_languages");
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<IdentifiableLanguages>().Result;
if(result == null)
result = new IdentifiableLanguages();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
/// <summary>
/// Create model.
///
/// Uploads a TMX glossary file on top of a domain to customize a translation model.
///
/// Depending on the size of the file, training can range from minutes for a glossary to several hours for a
/// large parallel corpus. Glossary files must be less than 10 MB. The cumulative file size of all uploaded
/// glossary and corpus files is limited to 250 MB.
/// </summary>
/// <param name="baseModelId">The model ID of the model to use as the base for customization. To see available
/// models, use the `List models` method.</param>
/// <param name="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.
/// (optional)</param>
/// <param name="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. (optional)</param>
/// <param name="parallelCorpus">A TMX file that contains entries that are treated as a parallel corpus instead
/// of a glossary. (optional)</param>
/// <param name="monolingualCorpus">A UTF-8 encoded plain text file that is used to customize the target
/// language model. (optional)</param>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="TranslationModel" />TranslationModel</returns>
public TranslationModel CreateModel(string baseModelId, string name = null, System.IO.FileStream forcedGlossary = null, System.IO.FileStream parallelCorpus = null, System.IO.FileStream monolingualCorpus = null, Dictionary<string, object> customData = null)
{
if (string.IsNullOrEmpty(baseModelId))
throw new ArgumentNullException(nameof(baseModelId));
TranslationModel result = null;
try
{
var formData = new MultipartFormDataContent();
if (forcedGlossary != null)
{
var forcedGlossaryContent = new ByteArrayContent((forcedGlossary as Stream).ReadAllBytes());
System.Net.Http.Headers.MediaTypeHeaderValue contentType;
System.Net.Http.Headers.MediaTypeHeaderValue.TryParse("application/octet-stream", out contentType);
forcedGlossaryContent.Headers.ContentType = contentType;
formData.Add(forcedGlossaryContent, "forced_glossary", forcedGlossary.Name);
}
if (parallelCorpus != null)
{
var parallelCorpusContent = new ByteArrayContent((parallelCorpus as Stream).ReadAllBytes());
System.Net.Http.Headers.MediaTypeHeaderValue contentType;
System.Net.Http.Headers.MediaTypeHeaderValue.TryParse("application/octet-stream", out contentType);
parallelCorpusContent.Headers.ContentType = contentType;
formData.Add(parallelCorpusContent, "parallel_corpus", parallelCorpus.Name);
}
if (monolingualCorpus != null)
{
var monolingualCorpusContent = new ByteArrayContent((monolingualCorpus as Stream).ReadAllBytes());
System.Net.Http.Headers.MediaTypeHeaderValue contentType;
System.Net.Http.Headers.MediaTypeHeaderValue.TryParse("text/plain", out contentType);
monolingualCorpusContent.Headers.ContentType = contentType;
formData.Add(monolingualCorpusContent, "monolingual_corpus", monolingualCorpus.Name);
}
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.PostAsync($"{this.Endpoint}/v2/models");
if (!string.IsNullOrEmpty(baseModelId))
restRequest.WithArgument("base_model_id", baseModelId);
if (!string.IsNullOrEmpty(name))
restRequest.WithArgument("name", name);
restRequest.WithBodyContent(formData);
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<TranslationModel>().Result;
if(result == null)
result = new TranslationModel();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
/// <summary>
/// Delete model.
///
/// Deletes a custom translation model.
/// </summary>
/// <param name="modelId">Model ID of the model to delete.</param>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="DeleteModelResult" />DeleteModelResult</returns>
public DeleteModelResult DeleteModel(string modelId, Dictionary<string, object> customData = null)
{
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException(nameof(modelId));
DeleteModelResult result = null;
try
{
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.DeleteAsync($"{this.Endpoint}/v2/models/{modelId}");
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<DeleteModelResult>().Result;
if(result == null)
result = new DeleteModelResult();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
/// <summary>
/// Get model details.
///
/// Gets information about a translation model, including training status for custom models.
/// </summary>
/// <param name="modelId">Model ID of the model to get.</param>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="TranslationModel" />TranslationModel</returns>
public TranslationModel GetModel(string modelId, Dictionary<string, object> customData = null)
{
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException(nameof(modelId));
TranslationModel result = null;
try
{
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.GetAsync($"{this.Endpoint}/v2/models/{modelId}");
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<TranslationModel>().Result;
if(result == null)
result = new TranslationModel();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
/// <summary>
/// List models.
///
/// Lists available translation models.
/// </summary>
/// <param name="source">Specify a language code to filter results by source language. (optional)</param>
/// <param name="target">Specify a language code to filter results by target language. (optional)</param>
/// <param name="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`. (optional)</param>
/// <param name="customData">Custom data object to pass data including custom request headers.</param>
/// <returns><see cref="TranslationModels" />TranslationModels</returns>
public TranslationModels ListModels(string source = null, string target = null, bool? defaultModels = null, Dictionary<string, object> customData = null)
{
TranslationModels result = null;
try
{
IClient client;
if(_tokenManager == null)
{
client = this.Client.WithAuthentication(this.UserName, this.Password);
}
else
{
client = this.Client.WithAuthentication(_tokenManager.GetToken());
}
var restRequest = client.GetAsync($"{this.Endpoint}/v2/models");
if (!string.IsNullOrEmpty(source))
restRequest.WithArgument("source", source);
if (!string.IsNullOrEmpty(target))
restRequest.WithArgument("target", target);
if (defaultModels != null)
restRequest.WithArgument("default", defaultModels);
if (customData != null)
restRequest.WithCustomData(customData);
result = restRequest.As<TranslationModels>().Result;
if(result == null)
result = new TranslationModels();
result.CustomData = restRequest.CustomData;
}
catch(AggregateException ae)
{
throw ae.Flatten();
}
return result;
}
}
}