-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathNaturalLanguageUnderstandingService.cs
1763 lines (1535 loc) · 79.1 KB
/
NaturalLanguageUnderstandingService.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. 2019, 2021.
*
* 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.
*
*/
/**
* IBM OpenAPI SDK Code Generator Version: 3.38.0-07189efd-20210827-205025
*/
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.NaturalLanguageUnderstanding.V1.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using UnityEngine.Networking;
namespace IBM.Watson.NaturalLanguageUnderstanding.V1
{
public partial class NaturalLanguageUnderstandingService : BaseService
{
private const string defaultServiceName = "natural_language_understanding";
private const string defaultServiceUrl = "https://api.us-south.natural-language-understanding.watson.cloud.ibm.com";
#region Version
private string version;
/// <summary>
/// Gets and sets the version of the service.
/// Release date of the API version you want to use. Specify dates in YYYY-MM-DD format. The current version is
/// `2021-08-01`.
/// </summary>
public string Version
{
get { return version; }
set { version = 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>
/// NaturalLanguageUnderstandingService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-08-01`.</param>
public NaturalLanguageUnderstandingService(string version) : this(version, defaultServiceName, ConfigBasedAuthenticatorFactory.GetAuthenticator(defaultServiceName)) {}
/// <summary>
/// NaturalLanguageUnderstandingService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-08-01`.</param>
/// <param name="authenticator">The service authenticator.</param>
public NaturalLanguageUnderstandingService(string version, Authenticator authenticator) : this(version, defaultServiceName, authenticator) {}
/// <summary>
/// NaturalLanguageUnderstandingService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-08-01`.</param>
/// <param name="serviceName">The service name to be used when configuring the client instance</param>
public NaturalLanguageUnderstandingService(string version, string serviceName) : this(version, serviceName, ConfigBasedAuthenticatorFactory.GetAuthenticator(serviceName)) {}
/// <summary>
/// NaturalLanguageUnderstandingService constructor.
/// </summary>
/// <param name="version">Release date of the API version you want to use. Specify dates in YYYY-MM-DD format.
/// The current version is `2021-08-01`.</param>
/// <param name="serviceName">The service name to be used when configuring the client instance</param>
/// <param name="authenticator">The service authenticator.</param>
public NaturalLanguageUnderstandingService(string version, string serviceName, Authenticator authenticator) : base(authenticator, serviceName)
{
Authenticator = authenticator;
if (string.IsNullOrEmpty(version))
{
throw new ArgumentNullException("`version` is required");
}
else
{
Version = version;
}
if (string.IsNullOrEmpty(GetServiceUrl()))
{
SetServiceUrl(defaultServiceUrl);
}
}
/// <summary>
/// Analyze text.
///
/// Analyzes text, HTML, or a public webpage for the following features:
/// - Categories
/// - Classifications
/// - Concepts
/// - Emotion
/// - Entities
/// - Keywords
/// - Metadata
/// - Relations
/// - Semantic roles
/// - Sentiment
/// - Syntax
/// - Summarization (Experimental)
///
/// If a language for the input text is not specified with the `language` parameter, the service [automatically
/// detects the
/// language](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-detectable-languages).
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="features">Specific features to analyze the document for.</param>
/// <param name="text">The plain text to analyze. One of the `text`, `html`, or `url` parameters is required.
/// (optional)</param>
/// <param name="html">The HTML file to analyze. One of the `text`, `html`, or `url` parameters is required.
/// (optional)</param>
/// <param name="url">The webpage to analyze. One of the `text`, `html`, or `url` parameters is required.
/// (optional)</param>
/// <param name="clean">Set this to `false` to disable webpage cleaning. For more information about webpage
/// cleaning, see [Analyzing
/// webpages](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-analyzing-webpages).
/// (optional, default to true)</param>
/// <param name="xpath">An [XPath
/// query](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-analyzing-webpages#xpath)
/// to perform on `html` or `url` input. Results of the query will be appended to the cleaned webpage text
/// before it is analyzed. To analyze only the results of the XPath query, set the `clean` parameter to `false`.
/// (optional)</param>
/// <param name="fallbackToRaw">Whether to use raw HTML content if text cleaning fails. (optional, default to
/// true)</param>
/// <param name="returnAnalyzedText">Whether or not to return the analyzed text. (optional, default to
/// false)</param>
/// <param name="language">ISO 639-1 code that specifies the language of your text. This overrides automatic
/// language detection. Language support differs depending on the features you include in your analysis. For
/// more information, see [Language
/// support](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-language-support).
/// (optional)</param>
/// <param name="limitTextCharacters">Sets the maximum number of characters that are processed by the service.
/// (optional)</param>
/// <returns><see cref="AnalysisResults" />AnalysisResults</returns>
public bool Analyze(Callback<AnalysisResults> callback, Features features, string text = null, string html = null, string url = null, bool? clean = null, string xpath = null, bool? fallbackToRaw = null, bool? returnAnalyzedText = null, string language = null, long? limitTextCharacters = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `Analyze`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (features == null)
throw new ArgumentNullException("`features` is required for `Analyze`");
RequestObject<AnalysisResults> req = new RequestObject<AnalysisResults>
{
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("natural-language-understanding", "V1", "Analyze"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
JObject bodyObject = new JObject();
if (features != null)
bodyObject["features"] = JToken.FromObject(features);
if (!string.IsNullOrEmpty(text))
bodyObject["text"] = text;
if (!string.IsNullOrEmpty(html))
bodyObject["html"] = html;
if (!string.IsNullOrEmpty(url))
bodyObject["url"] = url;
if (clean != null)
bodyObject["clean"] = JToken.FromObject(clean);
if (!string.IsNullOrEmpty(xpath))
bodyObject["xpath"] = xpath;
if (fallbackToRaw != null)
bodyObject["fallback_to_raw"] = JToken.FromObject(fallbackToRaw);
if (returnAnalyzedText != null)
bodyObject["return_analyzed_text"] = JToken.FromObject(returnAnalyzedText);
if (!string.IsNullOrEmpty(language))
bodyObject["language"] = language;
if (limitTextCharacters != null)
bodyObject["limit_text_characters"] = JToken.FromObject(limitTextCharacters);
req.Send = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(bodyObject));
req.OnResponse = OnAnalyzeResponse;
Connector.URL = GetServiceUrl() + "/v1/analyze";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnAnalyzeResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<AnalysisResults> response = new DetailedResponse<AnalysisResults>();
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<AnalysisResults>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnAnalyzeResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<AnalysisResults>)req).Callback != null)
((RequestObject<AnalysisResults>)req).Callback(response, resp.Error);
}
/// <summary>
/// List models.
///
/// Lists Watson Knowledge Studio [custom entities and relations
/// models](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-customizing)
/// that are deployed to your Natural Language Understanding service.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <returns><see cref="ListModelsResults" />ListModelsResults</returns>
public bool ListModels(Callback<ListModelsResults> callback)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListModels`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<ListModelsResults> req = new RequestObject<ListModelsResults>
{
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("natural-language-understanding", "V1", "ListModels"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnListModelsResponse;
Connector.URL = GetServiceUrl() + "/v1/models";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnListModelsResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<ListModelsResults> response = new DetailedResponse<ListModelsResults>();
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<ListModelsResults>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnListModelsResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<ListModelsResults>)req).Callback != null)
((RequestObject<ListModelsResults>)req).Callback(response, resp.Error);
}
/// <summary>
/// Delete model.
///
/// Deletes a custom 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="DeleteModelResults" />DeleteModelResults</returns>
public bool DeleteModel(Callback<DeleteModelResults> callback, string modelId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `DeleteModel`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException("`modelId` is required for `DeleteModel`");
RequestObject<DeleteModelResults> req = new RequestObject<DeleteModelResults>
{
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("natural-language-understanding", "V1", "DeleteModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnDeleteModelResponse;
Connector.URL = GetServiceUrl() + string.Format("/v1/models/{0}", modelId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnDeleteModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<DeleteModelResults> response = new DetailedResponse<DeleteModelResults>();
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<DeleteModelResults>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnDeleteModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<DeleteModelResults>)req).Callback != null)
((RequestObject<DeleteModelResults>)req).Callback(response, resp.Error);
}
/// <summary>
/// Create sentiment model.
///
/// (Beta) Creates a custom sentiment model by uploading training data and associated metadata. The model begins
/// the training and deploying process and is ready to use when the `status` is `available`.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="language">The 2-letter language code of this model.</param>
/// <param name="trainingData">Training data in CSV format. For more information, see [Sentiment training data
/// requirements](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-custom-sentiment#sentiment-training-data-requirements).</param>
/// <param name="name">An optional name for the model. (optional)</param>
/// <param name="description">An optional description of the model. (optional)</param>
/// <param name="modelVersion">An optional version string. (optional)</param>
/// <param name="workspaceId">ID of the Watson Knowledge Studio workspace that deployed this model to Natural
/// Language Understanding. (optional)</param>
/// <param name="versionDescription">The description of the version. (optional)</param>
/// <returns><see cref="SentimentModel" />SentimentModel</returns>
public bool CreateSentimentModel(Callback<SentimentModel> callback, string language, System.IO.MemoryStream trainingData, string name = null, string description = null, string modelVersion = null, string workspaceId = null, string versionDescription = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `CreateSentimentModel`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(language))
throw new ArgumentNullException("`language` is required for `CreateSentimentModel`");
if (trainingData == null)
throw new ArgumentNullException("`trainingData` is required for `CreateSentimentModel`");
RequestObject<SentimentModel> req = new RequestObject<SentimentModel>
{
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("natural-language-understanding", "V1", "CreateSentimentModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Forms = new Dictionary<string, RESTConnector.Form>();
if (!string.IsNullOrEmpty(language))
{
req.Forms["language"] = new RESTConnector.Form(language);
}
if (trainingData != null)
{
req.Forms["training_data"] = new RESTConnector.Form(trainingData, "filename", "text/csv");
}
if (!string.IsNullOrEmpty(name))
{
req.Forms["name"] = new RESTConnector.Form(name);
}
if (!string.IsNullOrEmpty(description))
{
req.Forms["description"] = new RESTConnector.Form(description);
}
if (!string.IsNullOrEmpty(modelVersion))
{
req.Forms["model_version"] = new RESTConnector.Form(modelVersion);
}
if (!string.IsNullOrEmpty(workspaceId))
{
req.Forms["workspace_id"] = new RESTConnector.Form(workspaceId);
}
if (!string.IsNullOrEmpty(versionDescription))
{
req.Forms["version_description"] = new RESTConnector.Form(versionDescription);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnCreateSentimentModelResponse;
Connector.URL = GetServiceUrl() + "/v1/models/sentiment";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnCreateSentimentModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<SentimentModel> response = new DetailedResponse<SentimentModel>();
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<SentimentModel>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnCreateSentimentModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<SentimentModel>)req).Callback != null)
((RequestObject<SentimentModel>)req).Callback(response, resp.Error);
}
/// <summary>
/// List sentiment models.
///
/// (Beta) Returns all custom sentiment models associated with this service instance.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <returns><see cref="ListSentimentModelsResponse" />ListSentimentModelsResponse</returns>
public bool ListSentimentModels(Callback<ListSentimentModelsResponse> callback)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListSentimentModels`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<ListSentimentModelsResponse> req = new RequestObject<ListSentimentModelsResponse>
{
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("natural-language-understanding", "V1", "ListSentimentModels"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnListSentimentModelsResponse;
Connector.URL = GetServiceUrl() + "/v1/models/sentiment";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnListSentimentModelsResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<ListSentimentModelsResponse> response = new DetailedResponse<ListSentimentModelsResponse>();
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<ListSentimentModelsResponse>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnListSentimentModelsResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<ListSentimentModelsResponse>)req).Callback != null)
((RequestObject<ListSentimentModelsResponse>)req).Callback(response, resp.Error);
}
/// <summary>
/// Get sentiment model details.
///
/// (Beta) Returns the status of the sentiment model with the given model ID.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="modelId">ID of the model.</param>
/// <returns><see cref="SentimentModel" />SentimentModel</returns>
public bool GetSentimentModel(Callback<SentimentModel> callback, string modelId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `GetSentimentModel`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException("`modelId` is required for `GetSentimentModel`");
RequestObject<SentimentModel> req = new RequestObject<SentimentModel>
{
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("natural-language-understanding", "V1", "GetSentimentModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnGetSentimentModelResponse;
Connector.URL = GetServiceUrl() + string.Format("/v1/models/sentiment/{0}", modelId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnGetSentimentModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<SentimentModel> response = new DetailedResponse<SentimentModel>();
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<SentimentModel>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnGetSentimentModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<SentimentModel>)req).Callback != null)
((RequestObject<SentimentModel>)req).Callback(response, resp.Error);
}
/// <summary>
/// Update sentiment model.
///
/// (Beta) Overwrites the training data associated with this custom sentiment model and retrains the model. The
/// new model replaces the current deployment.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="modelId">ID of the model.</param>
/// <param name="language">The 2-letter language code of this model.</param>
/// <param name="trainingData">Training data in CSV format. For more information, see [Sentiment training data
/// requirements](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-custom-sentiment#sentiment-training-data-requirements).</param>
/// <param name="name">An optional name for the model. (optional)</param>
/// <param name="description">An optional description of the model. (optional)</param>
/// <param name="modelVersion">An optional version string. (optional)</param>
/// <param name="workspaceId">ID of the Watson Knowledge Studio workspace that deployed this model to Natural
/// Language Understanding. (optional)</param>
/// <param name="versionDescription">The description of the version. (optional)</param>
/// <returns><see cref="SentimentModel" />SentimentModel</returns>
public bool UpdateSentimentModel(Callback<SentimentModel> callback, string modelId, string language, System.IO.MemoryStream trainingData, string name = null, string description = null, string modelVersion = null, string workspaceId = null, string versionDescription = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `UpdateSentimentModel`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException("`modelId` is required for `UpdateSentimentModel`");
if (string.IsNullOrEmpty(language))
throw new ArgumentNullException("`language` is required for `UpdateSentimentModel`");
if (trainingData == null)
throw new ArgumentNullException("`trainingData` is required for `UpdateSentimentModel`");
RequestObject<SentimentModel> req = new RequestObject<SentimentModel>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbPUT,
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("natural-language-understanding", "V1", "UpdateSentimentModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Forms = new Dictionary<string, RESTConnector.Form>();
if (!string.IsNullOrEmpty(language))
{
req.Forms["language"] = new RESTConnector.Form(language);
}
if (trainingData != null)
{
req.Forms["training_data"] = new RESTConnector.Form(trainingData, "filename", "text/csv");
}
if (!string.IsNullOrEmpty(name))
{
req.Forms["name"] = new RESTConnector.Form(name);
}
if (!string.IsNullOrEmpty(description))
{
req.Forms["description"] = new RESTConnector.Form(description);
}
if (!string.IsNullOrEmpty(modelVersion))
{
req.Forms["model_version"] = new RESTConnector.Form(modelVersion);
}
if (!string.IsNullOrEmpty(workspaceId))
{
req.Forms["workspace_id"] = new RESTConnector.Form(workspaceId);
}
if (!string.IsNullOrEmpty(versionDescription))
{
req.Forms["version_description"] = new RESTConnector.Form(versionDescription);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnUpdateSentimentModelResponse;
Connector.URL = GetServiceUrl() + string.Format("/v1/models/sentiment/{0}", modelId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnUpdateSentimentModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<SentimentModel> response = new DetailedResponse<SentimentModel>();
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<SentimentModel>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnUpdateSentimentModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<SentimentModel>)req).Callback != null)
((RequestObject<SentimentModel>)req).Callback(response, resp.Error);
}
/// <summary>
/// Delete sentiment model.
///
/// (Beta) Un-deploys the custom sentiment model with the given model ID and deletes all associated customer
/// data, including any training data or binary artifacts.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="modelId">ID of the model.</param>
/// <returns><see cref="DeleteModelResults" />DeleteModelResults</returns>
public bool DeleteSentimentModel(Callback<DeleteModelResults> callback, string modelId)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `DeleteSentimentModel`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(modelId))
throw new ArgumentNullException("`modelId` is required for `DeleteSentimentModel`");
RequestObject<DeleteModelResults> req = new RequestObject<DeleteModelResults>
{
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("natural-language-understanding", "V1", "DeleteSentimentModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnDeleteSentimentModelResponse;
Connector.URL = GetServiceUrl() + string.Format("/v1/models/sentiment/{0}", modelId);
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnDeleteSentimentModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<DeleteModelResults> response = new DetailedResponse<DeleteModelResults>();
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<DeleteModelResults>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnDeleteSentimentModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<DeleteModelResults>)req).Callback != null)
((RequestObject<DeleteModelResults>)req).Callback(response, resp.Error);
}
/// <summary>
/// Create categories model.
///
/// (Beta) Creates a custom categories model by uploading training data and associated metadata. The model
/// begins the training and deploying process and is ready to use when the `status` is `available`.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="language">The 2-letter language code of this model.</param>
/// <param name="trainingData">Training data in JSON format. For more information, see [Categories training data
/// requirements](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-categories##categories-training-data-requirements).</param>
/// <param name="trainingDataContentType">The content type of trainingData. (optional)</param>
/// <param name="name">An optional name for the model. (optional)</param>
/// <param name="description">An optional description of the model. (optional)</param>
/// <param name="modelVersion">An optional version string. (optional)</param>
/// <param name="workspaceId">ID of the Watson Knowledge Studio workspace that deployed this model to Natural
/// Language Understanding. (optional)</param>
/// <param name="versionDescription">The description of the version. (optional)</param>
/// <returns><see cref="CategoriesModel" />CategoriesModel</returns>
public bool CreateCategoriesModel(Callback<CategoriesModel> callback, string language, System.IO.MemoryStream trainingData, string trainingDataContentType = null, string name = null, string description = null, string modelVersion = null, string workspaceId = null, string versionDescription = null)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `CreateCategoriesModel`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
if (string.IsNullOrEmpty(language))
throw new ArgumentNullException("`language` is required for `CreateCategoriesModel`");
if (trainingData == null)
throw new ArgumentNullException("`trainingData` is required for `CreateCategoriesModel`");
RequestObject<CategoriesModel> req = new RequestObject<CategoriesModel>
{
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("natural-language-understanding", "V1", "CreateCategoriesModel"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}
req.Forms = new Dictionary<string, RESTConnector.Form>();
if (!string.IsNullOrEmpty(language))
{
req.Forms["language"] = new RESTConnector.Form(language);
}
if (trainingData != null)
{
req.Forms["training_data"] = new RESTConnector.Form(trainingData, "filename", trainingDataContentType);
}
if (!string.IsNullOrEmpty(name))
{
req.Forms["name"] = new RESTConnector.Form(name);
}
if (!string.IsNullOrEmpty(description))
{
req.Forms["description"] = new RESTConnector.Form(description);
}
if (!string.IsNullOrEmpty(modelVersion))
{
req.Forms["model_version"] = new RESTConnector.Form(modelVersion);
}
if (!string.IsNullOrEmpty(workspaceId))
{
req.Forms["workspace_id"] = new RESTConnector.Form(workspaceId);
}
if (!string.IsNullOrEmpty(versionDescription))
{
req.Forms["version_description"] = new RESTConnector.Form(versionDescription);
}
if (!string.IsNullOrEmpty(Version))
{
req.Parameters["version"] = Version;
}
req.OnResponse = OnCreateCategoriesModelResponse;
Connector.URL = GetServiceUrl() + "/v1/models/categories";
Authenticator.Authenticate(Connector);
return Connector.Send(req);
}
private void OnCreateCategoriesModelResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<CategoriesModel> response = new DetailedResponse<CategoriesModel>();
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<CategoriesModel>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("NaturalLanguageUnderstandingService.OnCreateCategoriesModelResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}
if (((RequestObject<CategoriesModel>)req).Callback != null)
((RequestObject<CategoriesModel>)req).Callback(response, resp.Error);
}
/// <summary>
/// List categories models.
///
/// (Beta) Returns all custom categories models associated with this service instance.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <returns><see cref="CategoriesModelList" />CategoriesModelList</returns>
public bool ListCategoriesModels(Callback<CategoriesModelList> callback)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListCategoriesModels`");
if (string.IsNullOrEmpty(Version))
throw new ArgumentNullException("`Version` is required");
RequestObject<CategoriesModelList> req = new RequestObject<CategoriesModelList>
{
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("natural-language-understanding", "V1", "ListCategoriesModels"))
{
req.Headers.Add(kvp.Key, kvp.Value);