forked from watson-developer-cloud/unity-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageTranslatorService.cs
1025 lines (883 loc) · 44.5 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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* (C) Copyright IBM Corp. 2018, 2020.
*
* 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.Text;
using IBM.Cloud.SDK;
using IBM.Cloud.SDK.Authentication;
using IBM.Cloud.SDK.Connection;
using IBM.Cloud.SDK.Utilities;
using IBM.Watson.LanguageTranslator.V3.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using UnityEngine.Networking;
namespace IBM.Watson.LanguageTranslator.V3
{
public partial class LanguageTranslatorService : BaseService
{
private const string serviceId = "language_translator";
private const string defaultServiceUrl = "https://gateway.watsonplatform.net/language-translator/api";
#region VersionDate
private string versionDate;
/// <summary>
/// Gets and sets the versionDate of the service.
/// </summary>
public string VersionDate
{
get { return versionDate; }
set { versionDate = value; }
}
#endregion
#region DisableSslVerification
private bool disableSslVerification = false;
/// <summary>
/// Gets and sets the option to disable ssl verification
/// </summary>
public bool DisableSslVerification
{
get { return disableSslVerification; }
set { disableSslVerification = value; }
}
#endregion
/// <summary>
/// LanguageTranslatorService constructor.
/// </summary>
/// <param name="versionDate">The service version date in `yyyy-mm-dd` format.</param>
public LanguageTranslatorService(string versionDate) : this(versionDate, ConfigBasedAuthenticatorFactory.GetAuthenticator(serviceId)) {}
/// <summary>
/// LanguageTranslatorService constructor.
/// </summary>
/// <param name="versionDate">The service version date in `yyyy-mm-dd` format.</param>
/// <param name="authenticator">The service authenticator.</param>
public LanguageTranslatorService(string versionDate, Authenticator authenticator) : base(versionDate, authenticator, serviceId)
{
Authenticator = authenticator;
if (string.IsNullOrEmpty(versionDate))
{
throw new ArgumentNullException("A versionDate (format `yyyy-mm-dd`) is required to create an instance of LanguageTranslatorService");
}
else
{
VersionDate = versionDate;
}
if (string.IsNullOrEmpty(GetServiceUrl()))
{
SetServiceUrl(defaultServiceUrl);
}
}
/// <summary>
/// Translate.
///
/// Translates the input text from the source language to the target language. A target language or translation
/// model ID is required. The service attempts to detect the language of the source text if it is not specified.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="text">Input text in UTF-8 encoding. Multiple entries will result in multiple translations in
/// the response.</param>
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM provided base
/// model for English to German translation. A model ID overrides the source and target parameters and is
/// required if you use a custom model. If no model ID is specified, you must specify a target language.
/// (optional)</param>
/// <param name="source">Language code that specifies the language of the source document. (optional)</param>
/// <param name="target">Language code that specifies the target language for translation. Required if model ID
/// is not specified. (optional)</param>
/// <returns><see cref="TranslationResult" />TranslationResult</returns>
public bool Translate(Callback<TranslationResult> callback, List<string> text, string modelId = null, string source = null, string target = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `Translate`");
if (text == null)
throw new ArgumentNullException("`text` is required for `Translate`");
RequestObject<TranslationResult> req = new RequestObject<TranslationResult>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "Translate"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
JObject bodyObject = new JObject();
if (text != null && text.Count > 0)
bodyObject["text"] = JToken.FromObject(text);
if (!string.IsNullOrEmpty(modelId))
bodyObject["model_id"] = modelId;
if (!string.IsNullOrEmpty(source))
bodyObject["source"] = source;
if (!string.IsNullOrEmpty(target))
bodyObject["target"] = target;
req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject));
req.OnResponse = OnTranslateResponse;
Connector.URL = GetServiceUrl() + "/v3/translate";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnTranslateResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<TranslationResult> response = new DetailedResponse<TranslationResult>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<TranslationResult>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnTranslateResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<TranslationResult>)req).Callback != null)
((RequestObject<TranslationResult>)req).Callback(response, resp.Error);
}
/// <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="callback">The callback function that is invoked when the operation completes.</param>
/// <returns><see cref="IdentifiableLanguages" />IdentifiableLanguages</returns>
public bool ListIdentifiableLanguages(Callback<IdentifiableLanguages> callback)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListIdentifiableLanguages`");
RequestObject<IdentifiableLanguages> req = new RequestObject<IdentifiableLanguages>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "ListIdentifiableLanguages"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.OnResponse = OnListIdentifiableLanguagesResponse;
Connector.URL = GetServiceUrl() + "/v3/identifiable_languages";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnListIdentifiableLanguagesResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<IdentifiableLanguages> response = new DetailedResponse<IdentifiableLanguages>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<IdentifiableLanguages>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnListIdentifiableLanguagesResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<IdentifiableLanguages>)req).Callback != null)
((RequestObject<IdentifiableLanguages>)req).Callback(response, resp.Error);
}
/// <summary>
/// Identify language.
///
/// Identifies the language of the input text.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="text">Input text in UTF-8 format.</param>
/// <returns><see cref="IdentifiedLanguages" />IdentifiedLanguages</returns>
public bool Identify(Callback<IdentifiedLanguages> callback, string text)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `Identify`");
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException("`text` is required for `Identify`");
RequestObject<IdentifiedLanguages> req = new RequestObject<IdentifiedLanguages>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "Identify"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.Headers["Content-Type"] = "text/plain";
req.Headers["Accept"] = "application/json";
req.Send = Encoding.UTF8.GetBytes(text);
req.OnResponse = OnIdentifyResponse;
Connector.URL = GetServiceUrl() + "/v3/identify";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnIdentifyResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<IdentifiedLanguages> response = new DetailedResponse<IdentifiedLanguages>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<IdentifiedLanguages>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnIdentifyResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<IdentifiedLanguages>)req).Callback != null)
((RequestObject<IdentifiedLanguages>)req).Callback(response, resp.Error);
}
/// <summary>
/// List models.
///
/// Lists available translation models.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <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="_default">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. (optional)</param>
/// <returns><see cref="TranslationModels" />TranslationModels</returns>
public bool ListModels(Callback<TranslationModels> callback, string source = null, string target = null, bool? _default = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListModels`");
RequestObject<TranslationModels> req = new RequestObject<TranslationModels>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "ListModels"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
if (!string.IsNullOrEmpty(source))
{
req.Parameters["source"] = source;
}
if (!string.IsNullOrEmpty(target))
{
req.Parameters["target"] = target;
}
if (_default != null)
{
req.Parameters["default"] = (bool)_default ? "true" : "false";
}
req.OnResponse = OnListModelsResponse;
Connector.URL = GetServiceUrl() + "/v3/models";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnListModelsResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<TranslationModels> response = new DetailedResponse<TranslationModels>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<TranslationModels>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnListModelsResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<TranslationModels>)req).Callback != null)
((RequestObject<TranslationModels>)req).Callback(response, resp.Error);
}
/// <summary>
/// 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>maximum of 10 custom models per language pair</b>.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <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. 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.</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. A forced glossary should contain
/// single words or short phrases. (optional)</param>
/// <param name="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. (optional)</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>
/// <returns><see cref="TranslationModel" />TranslationModel</returns>
public bool CreateModel(Callback<TranslationModel> callback, string baseModelId, System.IO.MemoryStream forcedGlossary = null, System.IO.MemoryStream parallelCorpus = null, string name = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `CreateModel`");
if (string.IsNullOrEmpty(baseModelId))
throw new ArgumentNullException("`baseModelId` is required for `CreateModel`");
RequestObject<TranslationModel> req = new RequestObject<TranslationModel>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "CreateModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.Forms = new Dictionary<string, RESTConnector.Form>();
if (forcedGlossary != null)
{
req.Forms["forced_glossary"] = new RESTConnector.Form(forcedGlossary, "filename", "application/octet-stream");
}
if (parallelCorpus != null)
{
req.Forms["parallel_corpus"] = new RESTConnector.Form(parallelCorpus, "filename", "application/octet-stream");
}
if (!string.IsNullOrEmpty(baseModelId))
{
req.Parameters["base_model_id"] = baseModelId;
}
if (!string.IsNullOrEmpty(name))
{
req.Parameters["name"] = name;
}
req.OnResponse = OnCreateModelResponse;
Connector.URL = GetServiceUrl() + "/v3/models";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnCreateModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<TranslationModel> response = new DetailedResponse<TranslationModel>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<TranslationModel>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnCreateModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<TranslationModel>)req).Callback != null)
((RequestObject<TranslationModel>)req).Callback(response, resp.Error);
}
/// <summary>
/// Delete model.
///
/// Deletes a custom translation model.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="modelId">Model ID of the model to delete.</param>
/// <returns><see cref="DeleteModelResult" />DeleteModelResult</returns>
public bool DeleteModel(Callback<DeleteModelResult> callback, string modelId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `DeleteModel`");
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException("`modelId` is required for `DeleteModel`");
RequestObject<DeleteModelResult> req = new RequestObject<DeleteModelResult>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbDELETE,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "DeleteModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.OnResponse = OnDeleteModelResponse;
Connector.URL = GetServiceUrl() + string.Format("/v3/models/{0}", modelId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnDeleteModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<DeleteModelResult> response = new DetailedResponse<DeleteModelResult>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<DeleteModelResult>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnDeleteModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<DeleteModelResult>)req).Callback != null)
((RequestObject<DeleteModelResult>)req).Callback(response, resp.Error);
}
/// <summary>
/// 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`.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="modelId">Model ID of the model to get.</param>
/// <returns><see cref="TranslationModel" />TranslationModel</returns>
public bool GetModel(Callback<TranslationModel> callback, string modelId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `GetModel`");
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException("`modelId` is required for `GetModel`");
RequestObject<TranslationModel> req = new RequestObject<TranslationModel>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "GetModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.OnResponse = OnGetModelResponse;
Connector.URL = GetServiceUrl() + string.Format("/v3/models/{0}", modelId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnGetModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<TranslationModel> response = new DetailedResponse<TranslationModel>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<TranslationModel>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnGetModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<TranslationModel>)req).Callback != null)
((RequestObject<TranslationModel>)req).Callback(response, resp.Error);
}
/// <summary>
/// List documents.
///
/// Lists documents that have been submitted for translation.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <returns><see cref="DocumentList" />DocumentList</returns>
public bool ListDocuments(Callback<DocumentList> callback)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListDocuments`");
RequestObject<DocumentList> req = new RequestObject<DocumentList>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "ListDocuments"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.OnResponse = OnListDocumentsResponse;
Connector.URL = GetServiceUrl() + "/v3/documents";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnListDocumentsResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<DocumentList> response = new DetailedResponse<DocumentList>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<DocumentList>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnListDocumentsResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<DocumentList>)req).Callback != null)
((RequestObject<DocumentList>)req).Callback(response, resp.Error);
}
/// <summary>
/// Translate document.
///
/// Submit a document for translation. You can submit the document contents in the `file` parameter, or you can
/// reference a previously submitted document by document ID.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="file">The contents of the source file to translate.
///
/// [Supported file
/// types](https://cloud.ibm.com/docs/language-translator?topic=language-translator-document-translator-tutorial#supported-file-formats)
///
/// Maximum file size: **20 MB**.</param>
/// <param name="filename">The filename for file.</param>
/// <param name="fileContentType">The content type of file. (optional)</param>
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM provided base
/// model for English to German translation. A model ID overrides the source and target parameters and is
/// required if you use a custom model. If no model ID is specified, you must specify a target language.
/// (optional)</param>
/// <param name="source">Language code that specifies the language of the source document. (optional)</param>
/// <param name="target">Language code that specifies the target language for translation. Required if model ID
/// is not specified. (optional)</param>
/// <param name="documentId">To use a previously submitted document as the source for a new translation, enter
/// the `document_id` of the document. (optional)</param>
/// <returns><see cref="DocumentStatus" />DocumentStatus</returns>
public bool TranslateDocument(Callback<DocumentStatus> callback, System.IO.MemoryStream file, string filename, string fileContentType = null, string modelId = null, string source = null, string target = null, string documentId = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `TranslateDocument`");
if (file == null)
throw new ArgumentNullException("`file` is required for `TranslateDocument`");
if (string.IsNullOrEmpty(filename))
throw new ArgumentNullException("`filename` is required for `TranslateDocument`");
RequestObject<DocumentStatus> req = new RequestObject<DocumentStatus>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPOST,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "TranslateDocument"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.Forms = new Dictionary<string, RESTConnector.Form>();
if (file != null)
{
req.Forms["file"] = new RESTConnector.Form(file, filename, fileContentType);
}
if (!string.IsNullOrEmpty(modelId))
{
req.Forms["model_id"] = new RESTConnector.Form(modelId);
}
if (!string.IsNullOrEmpty(source))
{
req.Forms["source"] = new RESTConnector.Form(source);
}
if (!string.IsNullOrEmpty(target))
{
req.Forms["target"] = new RESTConnector.Form(target);
}
if (!string.IsNullOrEmpty(documentId))
{
req.Forms["document_id"] = new RESTConnector.Form(documentId);
}
req.OnResponse = OnTranslateDocumentResponse;
Connector.URL = GetServiceUrl() + "/v3/documents";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnTranslateDocumentResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<DocumentStatus> response = new DetailedResponse<DocumentStatus>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<DocumentStatus>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnTranslateDocumentResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<DocumentStatus>)req).Callback != null)
((RequestObject<DocumentStatus>)req).Callback(response, resp.Error);
}
/// <summary>
/// Get document status.
///
/// Gets the translation status of a document.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="documentId">The document ID of the document.</param>
/// <returns><see cref="DocumentStatus" />DocumentStatus</returns>
public bool GetDocumentStatus(Callback<DocumentStatus> callback, string documentId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `GetDocumentStatus`");
if (string.IsNullOrEmpty(documentId))
throw new ArgumentNullException("`documentId` is required for `GetDocumentStatus`");
RequestObject<DocumentStatus> req = new RequestObject<DocumentStatus>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "GetDocumentStatus"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.OnResponse = OnGetDocumentStatusResponse;
Connector.URL = GetServiceUrl() + string.Format("/v3/documents/{0}", documentId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnGetDocumentStatusResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<DocumentStatus> response = new DetailedResponse<DocumentStatus>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<DocumentStatus>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnGetDocumentStatusResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<DocumentStatus>)req).Callback != null)
((RequestObject<DocumentStatus>)req).Callback(response, resp.Error);
}
/// <summary>
/// Delete document.
///
/// Deletes a document.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="documentId">Document ID of the document to delete.</param>
/// <returns><see cref="object" />object</returns>
public bool DeleteDocument(Callback<object> callback, string documentId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `DeleteDocument`");
if (string.IsNullOrEmpty(documentId))
throw new ArgumentNullException("`documentId` is required for `DeleteDocument`");
RequestObject<object> req = new RequestObject<object>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbDELETE,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "DeleteDocument"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;
req.OnResponse = OnDeleteDocumentResponse;
Connector.URL = GetServiceUrl() + string.Format("/v3/documents/{0}", documentId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnDeleteDocumentResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<object> response = new DetailedResponse<object>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;
try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<object>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnDeleteDocumentResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<object>)req).Callback != null)
((RequestObject<object>)req).Callback(response, resp.Error);
}
/// <summary>
/// Get translated document.
///
/// Gets the translated document associated with the given document ID.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="documentId">The document ID of the document that was submitted for translation.</param>
/// <param name="accept">The type of the response: application/powerpoint, application/mspowerpoint,
/// application/x-rtf, application/json, application/xml, application/vnd.ms-excel,
/// application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-powerpoint,
/// application/vnd.openxmlformats-officedocument.presentationml.presentation, application/msword,
/// application/vnd.openxmlformats-officedocument.wordprocessingml.document,
/// application/vnd.oasis.opendocument.spreadsheet, application/vnd.oasis.opendocument.presentation,
/// application/vnd.oasis.opendocument.text, application/pdf, application/rtf, text/html, text/json, text/plain,
/// text/richtext, text/rtf, or text/xml. A character encoding can be specified by including a `charset`
/// parameter. For example, 'text/html;charset=utf-8'. (optional)</param>
/// <returns><see cref="byte[]" />byte[]</returns>
public bool GetTranslatedDocument(Callback<byte[]> callback, string documentId, string accept = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `GetTranslatedDocument`");
if (string.IsNullOrEmpty(documentId))
throw new ArgumentNullException("`documentId` is required for `GetTranslatedDocument`");
RequestObject<byte[]> req = new RequestObject<byte[]>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}
ClearCustomRequestHeaders();
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "GetTranslatedDocument"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Parameters["version"] = VersionDate;