| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.AI.TextAnalytics.Models; |
| | 5 | | using Azure.Core; |
| | 6 | | using Azure.Core.Pipeline; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | namespace Azure.AI.TextAnalytics |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// The client to use for interacting with the Azure Cognitive Service, Text Analytics. |
| | 17 | | /// </summary> |
| | 18 | | public partial class TextAnalyticsClient |
| | 19 | | { |
| | 20 | | private readonly Uri _baseUri; |
| | 21 | | private readonly HttpPipeline _pipeline; |
| | 22 | | private readonly ServiceRestClient _serviceRestClient; |
| | 23 | | private readonly ClientDiagnostics _clientDiagnostics; |
| | 24 | | private readonly string _apiVersion; |
| | 25 | | private readonly TextAnalyticsClientOptions _options; |
| 384 | 26 | | private readonly string DefaultCognitiveScope = "https://cognitiveservices.azure.com/.default"; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Protected constructor to allow mocking. |
| | 30 | | /// </summary> |
| 184 | 31 | | protected TextAnalyticsClient() |
| | 32 | | { |
| 184 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Initializes a new instance of the <see cref="TextAnalyticsClient"/> |
| | 37 | | /// class for the specified service instance. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="endpoint">A <see cref="Uri"/> to the service the client |
| | 40 | | /// sends requests to. Endpoint can be found in the Azure portal.</param> |
| | 41 | | /// <param name="credential">A <see cref="TokenCredential"/> used to |
| | 42 | | /// authenticate requests to the service, such as DefaultAzureCredential.</param> |
| | 43 | | public TextAnalyticsClient(Uri endpoint, TokenCredential credential) |
| 8 | 44 | | : this(endpoint, credential, new TextAnalyticsClientOptions()) |
| | 45 | | { |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Initializes a new instance of the <see cref="TextAnalyticsClient"/> |
| | 50 | | /// class for the specified service instance. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="endpoint">A <see cref="Uri"/> to the service the client |
| | 53 | | /// sends requests to. Endpoint can be found in the Azure portal.</param> |
| | 54 | | /// <param name="credential">A <see cref="TokenCredential"/> used to |
| | 55 | | /// authenticate requests to the service, such as DefaultAzureCredential.</param> |
| | 56 | | /// <param name="options"><see cref="TextAnalyticsClientOptions"/> that allow |
| | 57 | | /// callers to configure how requests are sent to the service.</param> |
| 12 | 58 | | public TextAnalyticsClient(Uri endpoint, TokenCredential credential, TextAnalyticsClientOptions options) |
| | 59 | | { |
| 12 | 60 | | Argument.AssertNotNull(endpoint, nameof(endpoint)); |
| 8 | 61 | | Argument.AssertNotNull(credential, nameof(credential)); |
| 4 | 62 | | Argument.AssertNotNull(options, nameof(options)); |
| | 63 | |
|
| 4 | 64 | | _baseUri = endpoint; |
| 4 | 65 | | _apiVersion = options.GetVersionString(); |
| 4 | 66 | | _pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, DefaultCognit |
| 4 | 67 | | _clientDiagnostics = new ClientDiagnostics(options); |
| 4 | 68 | | _options = options; |
| 4 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Initializes a new instance of the <see cref="AzureKeyCredential"/> |
| | 73 | | /// class for the specified service instance. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="endpoint">A <see cref="Uri"/> to the service the client |
| | 76 | | /// sends requests to. Endpoint can be found in the Azure portal.</param> |
| | 77 | | /// <param name="credential">The API key used to access |
| | 78 | | /// the service. This will allow you to update the API key |
| | 79 | | /// without creating a new client.</param> |
| | 80 | | public TextAnalyticsClient(Uri endpoint, AzureKeyCredential credential) |
| 8 | 81 | | : this(endpoint, credential, new TextAnalyticsClientOptions()) |
| | 82 | | { |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Initializes a new instance of the <see cref="AzureKeyCredential"/> |
| | 87 | | /// class for the specified service instance. |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="endpoint">A <see cref="Uri"/> to the service the client |
| | 90 | | /// sends requests to. Endpoint can be found in the Azure portal.</param> |
| | 91 | | /// <param name="credential">The API key used to access |
| | 92 | | /// the service. This will allow you to update the API key |
| | 93 | | /// without creating a new client.</param> |
| | 94 | | /// <param name="options"><see cref="TextAnalyticsClientOptions"/> that allow |
| | 95 | | /// callers to configure how requests are sent to the service.</param> |
| 188 | 96 | | public TextAnalyticsClient(Uri endpoint, AzureKeyCredential credential, TextAnalyticsClientOptions options) |
| | 97 | | { |
| 188 | 98 | | Argument.AssertNotNull(endpoint, nameof(endpoint)); |
| 184 | 99 | | Argument.AssertNotNull(credential, nameof(credential)); |
| 180 | 100 | | Argument.AssertNotNull(options, nameof(options)); |
| | 101 | |
|
| 180 | 102 | | _baseUri = endpoint; |
| 180 | 103 | | _apiVersion = options.GetVersionString(); |
| 180 | 104 | | _pipeline = HttpPipelineBuilder.Build(options, new AzureKeyCredentialPolicy(credential, AuthorizationHeader) |
| 180 | 105 | | _clientDiagnostics = new ClientDiagnostics(options); |
| 180 | 106 | | _options = options; |
| | 107 | |
|
| | 108 | | // codegen |
| 180 | 109 | | _serviceRestClient = new ServiceRestClient(_clientDiagnostics, _pipeline, endpoint.AbsoluteUri); |
| 180 | 110 | | } |
| | 111 | |
|
| | 112 | | #region Detect Language |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Runs a predictive model to determine the language the passed-in |
| | 116 | | /// document is written in, and returns the detected language as well |
| | 117 | | /// as a score indicating the model's confidence that the inferred |
| | 118 | | /// language is correct. Scores close to 1 indicate high certainty in |
| | 119 | | /// the result. 120 languages are supported. |
| | 120 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 121 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 122 | | /// </summary> |
| | 123 | | /// <param name="document">The document to analyze.</param> |
| | 124 | | /// <param name="countryHint">Indicates the country of origin of the |
| | 125 | | /// document to assist the Text Analytics model in predicting the language |
| | 126 | | /// it is written in. If unspecified, this value will be set to the |
| | 127 | | /// default country hint in <see cref="TextAnalyticsClientOptions"/> |
| | 128 | | /// in the request sent to the service. |
| | 129 | | /// To remove this behavior, set to <see cref="DetectLanguageInput.None"/>.</param> |
| | 130 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 131 | | /// controlling the request lifetime.</param> |
| | 132 | | /// <returns>A result containing the detected language or an error if |
| | 133 | | /// the model could not analyze the document.</returns> |
| | 134 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 135 | | /// status code.</exception> |
| | 136 | | public virtual async Task<Response<DetectedLanguage>> DetectLanguageAsync(string document, string countryHint = |
| | 137 | | { |
| 20 | 138 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| 16 | 139 | | countryHint ??= _options.DefaultCountryHint; |
| | 140 | |
|
| 16 | 141 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(DetectL |
| 16 | 142 | | scope.AddAttribute("document", document); |
| 16 | 143 | | scope.Start(); |
| | 144 | |
|
| | 145 | | try |
| | 146 | | { |
| 16 | 147 | | var documents = new List<LanguageInput>() { ConvertToLanguageInput(document, countryHint) }; |
| | 148 | |
|
| 16 | 149 | | Response<LanguageResult> result = await _serviceRestClient.LanguagesAsync(new LanguageBatchInput(documen |
| 14 | 150 | | Response response = result.GetRawResponse(); |
| | 151 | |
|
| 14 | 152 | | if (result.Value.Errors.Count > 0) |
| | 153 | | { |
| | 154 | | // only one document, so we can ignore the id and grab the first error message. |
| 2 | 155 | | var error = Transforms.ConvertToError(result.Value.Errors[0].Error); |
| 2 | 156 | | throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToStr |
| | 157 | | } |
| | 158 | |
|
| 12 | 159 | | return Response.FromValue(Transforms.ConvertToDetectedLanguage(result.Value.Documents[0]), response); |
| | 160 | | } |
| 4 | 161 | | catch (Exception e) |
| | 162 | | { |
| 4 | 163 | | scope.Failed(e); |
| 4 | 164 | | throw; |
| | 165 | | } |
| 12 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Runs a predictive model to determine the language the passed-in |
| | 170 | | /// document is written in, and returns the detected language as well |
| | 171 | | /// as a score indicating the model's confidence that the inferred |
| | 172 | | /// language is correct. Scores close to 1 indicate high certainty in |
| | 173 | | /// the result. 120 languages are supported. |
| | 174 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 175 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 176 | | /// </summary> |
| | 177 | | /// <param name="document">The document to analyze.</param> |
| | 178 | | /// <param name="countryHint">Indicates the country of origin of the |
| | 179 | | /// document to assist the Text Analytics model in predicting the language |
| | 180 | | /// it is written in. If unspecified, this value will be set to the |
| | 181 | | /// default country hint in <see cref="TextAnalyticsClientOptions"/> |
| | 182 | | /// in the request sent to the service. |
| | 183 | | /// To remove this behavior, set to <see cref="DetectLanguageInput.None"/>.</param> |
| | 184 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 185 | | /// controlling the request lifetime.</param> |
| | 186 | | /// <returns>A result containing the detected language or an error if |
| | 187 | | /// the model could not analyze the document.</returns> |
| | 188 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 189 | | /// status code.</exception> |
| | 190 | | public virtual Response<DetectedLanguage> DetectLanguage(string document, string countryHint = default, Cancella |
| | 191 | | { |
| 20 | 192 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| 16 | 193 | | countryHint ??= _options.DefaultCountryHint; |
| | 194 | |
|
| 16 | 195 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(DetectL |
| 16 | 196 | | scope.AddAttribute("document", document); |
| 16 | 197 | | scope.Start(); |
| | 198 | |
|
| | 199 | | try |
| | 200 | | { |
| 16 | 201 | | var documents = new List<LanguageInput>() { ConvertToLanguageInput(document, countryHint) }; |
| 16 | 202 | | Response<LanguageResult> result = _serviceRestClient.Languages(new LanguageBatchInput(documents), cancel |
| 14 | 203 | | Response response = result.GetRawResponse(); |
| | 204 | |
|
| 14 | 205 | | if (result.Value.Errors.Count > 0) |
| | 206 | | { |
| | 207 | | // only one document, so we can ignore the id and grab the first error message. |
| 2 | 208 | | var error = Transforms.ConvertToError(result.Value.Errors[0].Error); |
| 2 | 209 | | throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCode.ToStr |
| | 210 | | } |
| | 211 | |
|
| 12 | 212 | | return Response.FromValue(Transforms.ConvertToDetectedLanguage(result.Value.Documents[0]), response); |
| | 213 | | } |
| 4 | 214 | | catch (Exception e) |
| | 215 | | { |
| 4 | 216 | | scope.Failed(e); |
| 4 | 217 | | throw; |
| | 218 | | } |
| 12 | 219 | | } |
| | 220 | |
|
| | 221 | | /// <summary> |
| | 222 | | /// Runs a predictive model to determine the language the passed-in |
| | 223 | | /// documents are written in, and returns, for each one, the detected |
| | 224 | | /// language as well as a score indicating the model's confidence that |
| | 225 | | /// the inferred language is correct. Scores close to 1 indicate high |
| | 226 | | /// certainty in the result. 120 languages are supported. |
| | 227 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 228 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 229 | | /// </summary> |
| | 230 | | /// <param name="documents">A collection of documents to analyze.</param> |
| | 231 | | /// <param name="countryHint">Indicates the country of origin of all of |
| | 232 | | /// the documents to assist the Text Analytics model in predicting |
| | 233 | | /// the language they are written in. If unspecified, this value will be |
| | 234 | | /// set to the default country hint in <see cref="TextAnalyticsClientOptions"/> |
| | 235 | | /// in the request sent to the service. |
| | 236 | | /// To remove this behavior, set to <see cref="DetectLanguageInput.None"/>.</param> |
| | 237 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 238 | | /// select the version of the predictive model to run, and whether |
| | 239 | | /// statistics are returned in the response.</param> |
| | 240 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 241 | | /// controlling the request lifetime.</param> |
| | 242 | | /// <returns>A result containing the detected language or an error if |
| | 243 | | /// the model could not analyze the documents.</returns> |
| | 244 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 245 | | /// status code.</exception> |
| | 246 | | public virtual async Task<Response<DetectLanguageResultCollection>> DetectLanguageBatchAsync(IEnumerable<string> |
| | 247 | | { |
| 10 | 248 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 6 | 249 | | LanguageBatchInput detectLanguageInputs = ConvertToLanguageInputs(documents, countryHint); |
| 6 | 250 | | options ??= new TextAnalyticsRequestOptions(); |
| | 251 | |
|
| 6 | 252 | | return await DetectLanguageBatchAsync(detectLanguageInputs, options, cancellationToken).ConfigureAwait(false |
| 6 | 253 | | } |
| | 254 | |
|
| | 255 | | /// <summary> |
| | 256 | | /// Runs a predictive model to determine the language the passed-in |
| | 257 | | /// documents are written in, and returns, for each one, the detected |
| | 258 | | /// language as well as a score indicating the model's confidence that |
| | 259 | | /// the inferred language is correct. Scores close to 1 indicate high |
| | 260 | | /// certainty in the result. 120 languages are supported. |
| | 261 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 262 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 263 | | /// </summary> |
| | 264 | | /// <param name="documents">A collection of documents to analyze.</param> |
| | 265 | | /// <param name="countryHint">Indicates the country of origin of all of |
| | 266 | | /// the documents to assist the Text Analytics model in predicting |
| | 267 | | /// the language they are written in. If unspecified, this value will be |
| | 268 | | /// set to the default country hint in <see cref="TextAnalyticsClientOptions"/> |
| | 269 | | /// in the request sent to the service. |
| | 270 | | /// To remove this behavior, set to <see cref="DetectLanguageInput.None"/>.</param> |
| | 271 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 272 | | /// select the version of the predictive model to run, and whether |
| | 273 | | /// statistics are returned in the response.</param> |
| | 274 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 275 | | /// controlling the request lifetime.</param> |
| | 276 | | /// <returns>A result containing the detected language or an error if |
| | 277 | | /// the model could not analyze the documents.</returns> |
| | 278 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 279 | | /// status code.</exception> |
| | 280 | | public virtual Response<DetectLanguageResultCollection> DetectLanguageBatch(IEnumerable<string> documents, strin |
| | 281 | | { |
| 10 | 282 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 6 | 283 | | LanguageBatchInput detectLanguageInputs = ConvertToLanguageInputs(documents, countryHint); |
| 6 | 284 | | options ??= new TextAnalyticsRequestOptions(); |
| | 285 | |
|
| 6 | 286 | | return DetectLanguageBatch(detectLanguageInputs, options, cancellationToken); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | /// <summary> |
| | 290 | | /// Runs a predictive model to determine the language the passed-in |
| | 291 | | /// documents are written in, and returns, for each one, the detected |
| | 292 | | /// language as well as a score indicating the model's confidence that |
| | 293 | | /// the inferred language is correct. Scores close to 1 indicate high |
| | 294 | | /// certainty in the result. 120 languages are supported. |
| | 295 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 296 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 297 | | /// </summary> |
| | 298 | | /// <param name="documents">A collection of documents to analyze.</param> |
| | 299 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 300 | | /// select the version of the predictive model to run, and whether |
| | 301 | | /// statistics are returned in the response.</param> |
| | 302 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 303 | | /// controlling the request lifetime.</param> |
| | 304 | | /// <returns>A result containing the detected language or an error if |
| | 305 | | /// the model could not analyze the documents.</returns> |
| | 306 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 307 | | /// status code.</exception> |
| | 308 | | public virtual async Task<Response<DetectLanguageResultCollection>> DetectLanguageBatchAsync(IEnumerable<DetectL |
| | 309 | | { |
| 6 | 310 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 4 | 311 | | LanguageBatchInput detectLanguageInputs = ConvertToLanguageInputs(documents); |
| 4 | 312 | | options ??= new TextAnalyticsRequestOptions(); |
| | 313 | |
|
| 4 | 314 | | return await DetectLanguageBatchAsync(detectLanguageInputs, options, cancellationToken).ConfigureAwait(false |
| 4 | 315 | | } |
| | 316 | |
|
| | 317 | | /// <summary> |
| | 318 | | /// Runs a predictive model to determine the language the passed-in |
| | 319 | | /// documents are written in, and returns, for each one, the detected |
| | 320 | | /// language as well as a score indicating the model's confidence that |
| | 321 | | /// the inferred language is correct. Scores close to 1 indicate high |
| | 322 | | /// certainty in the result. 120 languages are supported. |
| | 323 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 324 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 325 | | /// </summary> |
| | 326 | | /// <param name="documents">A collection of documents to analyze.</param> |
| | 327 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 328 | | /// select the version of the predictive model to run, and whether |
| | 329 | | /// statistics are returned in the response.</param> |
| | 330 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 331 | | /// controlling the request lifetime.</param> |
| | 332 | | /// <returns>A result containing the detected language or an error if |
| | 333 | | /// the model could not analyze the document.</returns> |
| | 334 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 335 | | /// status code.</exception> |
| | 336 | | public virtual Response<DetectLanguageResultCollection> DetectLanguageBatch(IEnumerable<DetectLanguageInput> doc |
| | 337 | | { |
| 6 | 338 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 4 | 339 | | options ??= new TextAnalyticsRequestOptions(); |
| 4 | 340 | | LanguageBatchInput detectLanguageInputs = ConvertToLanguageInputs(documents); |
| | 341 | |
|
| 4 | 342 | | return DetectLanguageBatch(detectLanguageInputs, options, cancellationToken); |
| | 343 | | } |
| | 344 | |
|
| | 345 | | private async Task<Response<DetectLanguageResultCollection>> DetectLanguageBatchAsync(LanguageBatchInput batchIn |
| | 346 | | { |
| 10 | 347 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(DetectL |
| 10 | 348 | | scope.Start(); |
| | 349 | |
|
| | 350 | | try |
| | 351 | | { |
| 10 | 352 | | Response<LanguageResult> result = await _serviceRestClient.LanguagesAsync(batchInput, options.ModelVersi |
| 10 | 353 | | var response = result.GetRawResponse(); |
| | 354 | |
|
| 10 | 355 | | switch (response.Status) |
| | 356 | | { |
| | 357 | | case 200: |
| 10 | 358 | | IDictionary<string, int> map = CreateIdToIndexMap(batchInput.Documents); |
| 10 | 359 | | DetectLanguageResultCollection results = Transforms.ConvertLanguageResult(result.Value, map); |
| 10 | 360 | | return Response.FromValue(results, response); |
| | 361 | | default: |
| 0 | 362 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 363 | | } |
| | 364 | | } |
| 0 | 365 | | catch (Exception e) |
| | 366 | | { |
| 0 | 367 | | scope.Failed(e); |
| 0 | 368 | | throw; |
| | 369 | | } |
| 10 | 370 | | } |
| | 371 | |
|
| | 372 | | private Response<DetectLanguageResultCollection> DetectLanguageBatch(LanguageBatchInput batchInput, TextAnalytic |
| | 373 | | { |
| 10 | 374 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(DetectL |
| 10 | 375 | | scope.Start(); |
| | 376 | |
|
| | 377 | | try |
| | 378 | | { |
| 10 | 379 | | Response<LanguageResult> result = _serviceRestClient.Languages(batchInput, options.ModelVersion, options |
| 10 | 380 | | var response = result.GetRawResponse(); |
| | 381 | |
|
| 10 | 382 | | switch (response.Status) |
| | 383 | | { |
| | 384 | | case 200: |
| 10 | 385 | | IDictionary<string, int> map = CreateIdToIndexMap(batchInput.Documents); |
| 10 | 386 | | DetectLanguageResultCollection results = Transforms.ConvertLanguageResult(result.Value, map); |
| 10 | 387 | | return Response.FromValue(results, response); |
| | 388 | | default: |
| 0 | 389 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 390 | | } |
| | 391 | | } |
| 0 | 392 | | catch (Exception e) |
| | 393 | | { |
| 0 | 394 | | scope.Failed(e); |
| 0 | 395 | | throw; |
| | 396 | | } |
| 10 | 397 | | } |
| | 398 | |
|
| | 399 | | #endregion |
| | 400 | |
|
| | 401 | | #region Recognize Entities |
| | 402 | |
|
| | 403 | | /// <summary> |
| | 404 | | /// Runs a predictive model to identify a collection of named entities |
| | 405 | | /// in the passed-in document, and categorize those entities into types |
| | 406 | | /// such as person, location, or organization. For more information on |
| | 407 | | /// available categories, see |
| | 408 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/named-entity-types"/>. |
| | 409 | | /// For a list of languages supported by this operation, see |
| | 410 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 411 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 412 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 413 | | /// </summary> |
| | 414 | | /// <param name="document">The document to analyze.</param> |
| | 415 | | /// <param name="language">The language that the document is written in. |
| | 416 | | /// If unspecified, this value will be set to the default language in |
| | 417 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 418 | | /// service. If set to an empty string, the service will apply a model |
| | 419 | | /// where the language is explicitly set to "None".</param> |
| | 420 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 421 | | /// controlling the request lifetime.</param> |
| | 422 | | /// <returns>A result containing the collection of entities identified |
| | 423 | | /// in the document, as well as a score indicating the confidence |
| | 424 | | /// that the entity correctly matches the identified substring.</returns> |
| | 425 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 426 | | /// status code.</exception> |
| | 427 | | public virtual async Task<Response<CategorizedEntityCollection>> RecognizeEntitiesAsync(string document, string |
| | 428 | | { |
| 10 | 429 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 430 | |
|
| 6 | 431 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 6 | 432 | | scope.AddAttribute("document", document); |
| 6 | 433 | | scope.Start(); |
| | 434 | |
|
| | 435 | | try |
| | 436 | | { |
| 6 | 437 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 6 | 438 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), Entitie |
| 6 | 439 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 440 | |
|
| 6 | 441 | | switch (response.Status) |
| | 442 | | { |
| | 443 | | case 200: |
| 6 | 444 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 6 | 445 | | RecognizeEntitiesResultCollection results = await CreateRecognizeEntitiesResponseAsync(response, |
| 6 | 446 | | if (results[0].HasError) |
| | 447 | | { |
| | 448 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 449 | | TextAnalyticsError error = results[0].Error; |
| 0 | 450 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, er |
| | 451 | | } |
| 6 | 452 | | return Response.FromValue((CategorizedEntityCollection)results[0].Entities, response); |
| | 453 | | default: |
| 0 | 454 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 455 | | } |
| | 456 | | } |
| 0 | 457 | | catch (Exception e) |
| | 458 | | { |
| 0 | 459 | | scope.Failed(e); |
| 0 | 460 | | throw; |
| | 461 | | } |
| 6 | 462 | | } |
| | 463 | |
|
| | 464 | | /// <summary> |
| | 465 | | /// Runs a predictive model to identify a collection of named entities |
| | 466 | | /// in the passed-in document, and categorize those entities into types |
| | 467 | | /// such as person, location, or organization. For more information on |
| | 468 | | /// available categories, see |
| | 469 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/named-entity-types"/>. |
| | 470 | | /// For a list of languages supported by this operation, see |
| | 471 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 472 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 473 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 474 | | /// </summary> |
| | 475 | | /// <param name="document">The text to analyze.</param> |
| | 476 | | /// <param name="language">The language that the document is written in. |
| | 477 | | /// If unspecified, this value will be set to the default language in |
| | 478 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 479 | | /// service. If set to an empty string, the service will apply a model |
| | 480 | | /// where the language is explicitly set to "None".</param> |
| | 481 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 482 | | /// controlling the request lifetime.</param> |
| | 483 | | /// <returns>A result containing the collection of entities identified |
| | 484 | | /// in the document, as well as a score indicating the confidence |
| | 485 | | /// that the entity correctly matches the identified substring.</returns> |
| | 486 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 487 | | /// status code.</exception> |
| | 488 | | public virtual Response<CategorizedEntityCollection> RecognizeEntities(string document, string language = defaul |
| | 489 | | { |
| 10 | 490 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 491 | |
|
| 6 | 492 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 6 | 493 | | scope.AddAttribute("document", document); |
| 6 | 494 | | scope.Start(); |
| | 495 | |
|
| | 496 | | try |
| | 497 | | { |
| 6 | 498 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 6 | 499 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), Entitie |
| 6 | 500 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 501 | |
|
| 6 | 502 | | switch (response.Status) |
| | 503 | | { |
| | 504 | | case 200: |
| 6 | 505 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 6 | 506 | | RecognizeEntitiesResultCollection results = CreateRecognizeEntitiesResponse(response, map); |
| 6 | 507 | | if (results[0].HasError) |
| | 508 | | { |
| | 509 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 510 | | TextAnalyticsError error = results[0].Error; |
| 0 | 511 | | throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCo |
| | 512 | | } |
| 6 | 513 | | return Response.FromValue((CategorizedEntityCollection)results[0].Entities, response); |
| | 514 | | default: |
| 0 | 515 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 516 | | } |
| | 517 | | } |
| 0 | 518 | | catch (Exception e) |
| | 519 | | { |
| 0 | 520 | | scope.Failed(e); |
| 0 | 521 | | throw; |
| | 522 | | } |
| 6 | 523 | | } |
| | 524 | |
|
| | 525 | | /// <summary> |
| | 526 | | /// Runs a predictive model to identify a collection of named entities |
| | 527 | | /// in the passed-in documents, and categorize those entities into types |
| | 528 | | /// such as person, location, or organization. |
| | 529 | | /// For more information on available categories, see |
| | 530 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/named-entity-types"/>. |
| | 531 | | /// For a list of languages supported by this operation, see |
| | 532 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 533 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 534 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 535 | | /// </summary> |
| | 536 | | /// <param name="documents">The documents to analyze.</param> |
| | 537 | | /// <param name="language">The language that all the documents are |
| | 538 | | /// written in. If unspecified, this value will be set to the default |
| | 539 | | /// language in <see cref="TextAnalyticsClientOptions"/> in the request |
| | 540 | | /// sent to the service. If set to an empty string, the service will apply a model |
| | 541 | | /// where the language is explicitly set to "None".</param> |
| | 542 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 543 | | /// select the version of the predictive model to run, and whether |
| | 544 | | /// statistics are returned in the response.</param> |
| | 545 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 546 | | /// controlling the request lifetime.</param> |
| | 547 | | /// <returns>A result containing the collection of entities identified |
| | 548 | | /// for each of the documents, as well as scores indicating the confidence |
| | 549 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 550 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 551 | | /// status code.</exception> |
| | 552 | | public virtual async Task<Response<RecognizeEntitiesResultCollection>> RecognizeEntitiesBatchAsync(IEnumerable<s |
| | 553 | | { |
| 12 | 554 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 8 | 555 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 8 | 556 | | options ??= new TextAnalyticsRequestOptions(); |
| | 557 | |
|
| 8 | 558 | | return await RecognizeEntitiesBatchAsync(documentInputs, options, cancellationToken).ConfigureAwait(false); |
| 6 | 559 | | } |
| | 560 | |
|
| | 561 | | /// <summary> |
| | 562 | | /// Runs a predictive model to identify a collection of named entities |
| | 563 | | /// in the passed-in documents, and categorize those entities into types |
| | 564 | | /// such as person, location, or organization. |
| | 565 | | /// For more information on available categories, see |
| | 566 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/named-entity-types"/>. |
| | 567 | | /// For a list of languages supported by this operation, see |
| | 568 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 569 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 570 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 571 | | /// </summary> |
| | 572 | | /// <param name="documents">The documents to analyze.</param> |
| | 573 | | /// <param name="language">The language that all the documents are |
| | 574 | | /// written in. If unspecified, this value will be set to the default |
| | 575 | | /// language in <see cref="TextAnalyticsClientOptions"/> in the request |
| | 576 | | /// sent to the service. If set to an empty string, the service will apply a model |
| | 577 | | /// where the language is explicitly set to "None".</param> |
| | 578 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 579 | | /// select the version of the predictive model to run, and whether |
| | 580 | | /// statistics are returned in the response.</param> |
| | 581 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 582 | | /// controlling the request lifetime.</param> |
| | 583 | | /// <returns>A result containing the collection of entities identified |
| | 584 | | /// for each of the documents, as well as scores indicating the confidence |
| | 585 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 586 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 587 | | /// status code.</exception> |
| | 588 | | public virtual Response<RecognizeEntitiesResultCollection> RecognizeEntitiesBatch(IEnumerable<string> documents, |
| | 589 | | { |
| 12 | 590 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 8 | 591 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 8 | 592 | | options ??= new TextAnalyticsRequestOptions(); |
| | 593 | |
|
| 8 | 594 | | return RecognizeEntitiesBatch(documentInputs, options, cancellationToken); |
| | 595 | | } |
| | 596 | |
|
| | 597 | | /// <summary> |
| | 598 | | /// Runs a predictive model to identify a collection of named entities |
| | 599 | | /// in the passed-in documents, and categorize those entities into types |
| | 600 | | /// such as person, location, or organization. |
| | 601 | | /// For more information on available categories, see |
| | 602 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/named-entity-types"/>. |
| | 603 | | /// For a list of languages supported by this operation, see |
| | 604 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 605 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 606 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 607 | | /// </summary> |
| | 608 | | /// <param name="documents">The documents to analyze.</param> |
| | 609 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 610 | | /// select the version of the predictive model to run, and whether |
| | 611 | | /// statistics are returned in the response.</param> |
| | 612 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 613 | | /// controlling the request lifetime.</param> |
| | 614 | | /// <returns>A result containing the collection of entities identified |
| | 615 | | /// for each of the documents, as well as scores indicating the confidence |
| | 616 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 617 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 618 | | /// status code.</exception> |
| | 619 | | public virtual async Task<Response<RecognizeEntitiesResultCollection>> RecognizeEntitiesBatchAsync(IEnumerable<T |
| | 620 | | { |
| 18 | 621 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 16 | 622 | | options ??= new TextAnalyticsRequestOptions(); |
| | 623 | |
|
| 16 | 624 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 16 | 625 | | scope.Start(); |
| | 626 | |
|
| | 627 | | try |
| | 628 | | { |
| 16 | 629 | | using Request request = CreateDocumentInputRequest(documents, options, EntitiesRoute); |
| 16 | 630 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 631 | |
|
| 16 | 632 | | switch (response.Status) |
| | 633 | | { |
| | 634 | | case 200: |
| 14 | 635 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 14 | 636 | | RecognizeEntitiesResultCollection results = await CreateRecognizeEntitiesResponseAsync(response, |
| 14 | 637 | | return Response.FromValue(results, response); |
| | 638 | | default: |
| 2 | 639 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 640 | | } |
| | 641 | | } |
| 2 | 642 | | catch (Exception e) |
| | 643 | | { |
| 2 | 644 | | scope.Failed(e); |
| 2 | 645 | | throw; |
| | 646 | | } |
| 14 | 647 | | } |
| | 648 | |
|
| | 649 | | /// <summary> |
| | 650 | | /// Runs a predictive model to identify a collection of named entities |
| | 651 | | /// in the passed-in documents, and categorize those entities into types |
| | 652 | | /// such as person, location, or organization. |
| | 653 | | /// For more information on available categories, see |
| | 654 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/Text-Analytics/named-entity-types"/>. |
| | 655 | | /// For a list of languages supported by this operation, see |
| | 656 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 657 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 658 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 659 | | /// </summary> |
| | 660 | | /// <param name="documents">The documents to analyze.</param> |
| | 661 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 662 | | /// select the version of the predictive model to run, and whether |
| | 663 | | /// statistics are returned in the response.</param> |
| | 664 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 665 | | /// controlling the request lifetime.</param> |
| | 666 | | /// <returns>A result containing the collection of entities identified |
| | 667 | | /// for each of the documents, as well as scores indicating the confidence |
| | 668 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 669 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 670 | | /// status code.</exception> |
| | 671 | | public virtual Response<RecognizeEntitiesResultCollection> RecognizeEntitiesBatch(IEnumerable<TextDocumentInput> |
| | 672 | | { |
| 18 | 673 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 16 | 674 | | options ??= new TextAnalyticsRequestOptions(); |
| | 675 | |
|
| 16 | 676 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 16 | 677 | | scope.Start(); |
| | 678 | |
|
| | 679 | | try |
| | 680 | | { |
| 16 | 681 | | using Request request = CreateDocumentInputRequest(documents, options, EntitiesRoute); |
| 16 | 682 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 683 | |
|
| 16 | 684 | | switch (response.Status) |
| | 685 | | { |
| | 686 | | case 200: |
| 14 | 687 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 14 | 688 | | RecognizeEntitiesResultCollection results = CreateRecognizeEntitiesResponse(response, map); |
| 14 | 689 | | return Response.FromValue(results, response); |
| | 690 | | default: |
| 2 | 691 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 692 | | } |
| | 693 | | } |
| 2 | 694 | | catch (Exception e) |
| | 695 | | { |
| 2 | 696 | | scope.Failed(e); |
| 2 | 697 | | throw; |
| | 698 | | } |
| 14 | 699 | | } |
| | 700 | |
|
| | 701 | | #endregion |
| | 702 | |
|
| | 703 | | #region Analyze Sentiment |
| | 704 | |
|
| | 705 | | /// <summary> |
| | 706 | | /// Runs a predictive model to identify the positive, negative, neutral |
| | 707 | | /// or mixed sentiment contained in the document, as well as a score |
| | 708 | | /// indicating the model's confidence in the predicted sentiment. |
| | 709 | | /// For a list of languages supported by this operation, see |
| | 710 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 711 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 712 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 713 | | /// </summary> |
| | 714 | | /// <param name="document">The document to analyze.</param> |
| | 715 | | /// <param name="language">The language that the document is written in. |
| | 716 | | /// If unspecified, this value will be set to the default language in |
| | 717 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 718 | | /// service. If set to an empty string, the service will apply a model |
| | 719 | | /// where the language is explicitly set to "None".</param> |
| | 720 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 721 | | /// controlling the request lifetime.</param> |
| | 722 | | /// <returns>A result containing sentiment predictions for the document |
| | 723 | | /// and each of the sentences it contains.</returns> |
| | 724 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 725 | | /// status code.</exception> |
| | 726 | | public virtual async Task<Response<DocumentSentiment>> AnalyzeSentimentAsync(string document, string language = |
| | 727 | | { |
| 8 | 728 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 729 | |
|
| 4 | 730 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Analyze |
| 4 | 731 | | scope.AddAttribute("document", document); |
| 4 | 732 | | scope.Start(); |
| | 733 | |
|
| | 734 | | try |
| | 735 | | { |
| 4 | 736 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 4 | 737 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), Sentime |
| 4 | 738 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 739 | |
|
| 4 | 740 | | switch (response.Status) |
| | 741 | | { |
| | 742 | | case 200: |
| 4 | 743 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 4 | 744 | | AnalyzeSentimentResultCollection results = await CreateAnalyzeSentimentResponseAsync(response, m |
| 4 | 745 | | if (results[0].HasError) |
| | 746 | | { |
| | 747 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 748 | | TextAnalyticsError error = results[0].Error; |
| 0 | 749 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, er |
| | 750 | | } |
| 4 | 751 | | return Response.FromValue(results[0].DocumentSentiment, response); |
| | 752 | | default: |
| 0 | 753 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 754 | | } |
| | 755 | | } |
| 0 | 756 | | catch (Exception e) |
| | 757 | | { |
| 0 | 758 | | scope.Failed(e); |
| 0 | 759 | | throw; |
| | 760 | | } |
| 4 | 761 | | } |
| | 762 | |
|
| | 763 | | /// <summary> |
| | 764 | | /// Runs a predictive model to identify the positive, negative or neutral |
| | 765 | | /// sentiment contained in the document, as well as a score indicating the model's |
| | 766 | | /// confidence in the predicted sentiment. |
| | 767 | | /// For a list of languages supported by this operation, see |
| | 768 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 769 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 770 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 771 | | /// </summary> |
| | 772 | | /// <param name="document">The text to analyze.</param> |
| | 773 | | /// <param name="language">The language that the document is written in. |
| | 774 | | /// If unspecified, this value will be set to the default language in |
| | 775 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 776 | | /// service. If set to an empty string, the service will apply a model |
| | 777 | | /// where the language is explicitly set to "None".</param> |
| | 778 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 779 | | /// controlling the request lifetime.</param> |
| | 780 | | /// <returns>A result containing sentiment predictions for the document |
| | 781 | | /// and each of the sentences it contains.</returns> |
| | 782 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 783 | | /// status code.</exception> |
| | 784 | | public virtual Response<DocumentSentiment> AnalyzeSentiment(string document, string language = default, Cancella |
| | 785 | | { |
| 8 | 786 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 787 | |
|
| 4 | 788 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Analyze |
| 4 | 789 | | scope.AddAttribute("document", document); |
| 4 | 790 | | scope.Start(); |
| | 791 | |
|
| | 792 | | try |
| | 793 | | { |
| 4 | 794 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 4 | 795 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), Sentime |
| 4 | 796 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 797 | |
|
| 4 | 798 | | switch (response.Status) |
| | 799 | | { |
| | 800 | | case 200: |
| 4 | 801 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 4 | 802 | | AnalyzeSentimentResultCollection results = CreateAnalyzeSentimentResponse(response, map); |
| 4 | 803 | | if (results[0].HasError) |
| | 804 | | { |
| | 805 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 806 | | TextAnalyticsError error = results[0].Error; |
| 0 | 807 | | throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCo |
| | 808 | | } |
| 4 | 809 | | return Response.FromValue(results[0].DocumentSentiment, response); |
| | 810 | | default: |
| 0 | 811 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 812 | | } |
| | 813 | | } |
| 0 | 814 | | catch (Exception e) |
| | 815 | | { |
| 0 | 816 | | scope.Failed(e); |
| 0 | 817 | | throw; |
| | 818 | | } |
| 4 | 819 | | } |
| | 820 | |
|
| | 821 | | /// <summary> |
| | 822 | | /// Runs a predictive model to identify the positive, negative or neutral |
| | 823 | | /// sentiment contained in the documents, as well as scores indicating |
| | 824 | | /// the model's confidence in each of the predicted sentiments. |
| | 825 | | /// For a list of languages supported by this operation, see |
| | 826 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 827 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 828 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 829 | | /// </summary> |
| | 830 | | /// <param name="documents">The documents to analyze.</param> |
| | 831 | | /// <param name="language">The language that all of the documents are written in. |
| | 832 | | /// If unspecified, this value will be set to the default language in |
| | 833 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 834 | | /// service. If set to an empty string, the service will apply a model |
| | 835 | | /// where the language is explicitly set to "None".</param> |
| | 836 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 837 | | /// select the version of the predictive model to run, and whether |
| | 838 | | /// statistics are returned in the response.</param> |
| | 839 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 840 | | /// controlling the request lifetime.</param> |
| | 841 | | /// <returns>A result containing sentiment predictions for each of the documents |
| | 842 | | /// and predictions for each of the sentences each document contains.</returns> |
| | 843 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 844 | | /// status code.</exception> |
| | 845 | | public virtual async Task<Response<AnalyzeSentimentResultCollection>> AnalyzeSentimentBatchAsync(IEnumerable<str |
| | 846 | | { |
| 10 | 847 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 6 | 848 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 6 | 849 | | options ??= new TextAnalyticsRequestOptions(); |
| | 850 | |
|
| 6 | 851 | | return await AnalyzeSentimentBatchAsync(documentInputs, options, cancellationToken).ConfigureAwait(false); |
| 6 | 852 | | } |
| | 853 | |
|
| | 854 | | /// <summary> |
| | 855 | | /// Runs a predictive model to identify the positive, negative or neutral |
| | 856 | | /// sentiment contained in the documents, as well as scores indicating |
| | 857 | | /// the model's confidence in each of the predicted sentiments. |
| | 858 | | /// For a list of languages supported by this operation, see |
| | 859 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 860 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 861 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 862 | | /// </summary> |
| | 863 | | /// <param name="documents">The documents to analyze.</param> |
| | 864 | | /// <param name="language">The language that all of the documents are written in. |
| | 865 | | /// If unspecified, this value will be set to the default language in |
| | 866 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 867 | | /// service. If set to an empty string, the service will apply a model |
| | 868 | | /// where the language is explicitly set to "None".</param> |
| | 869 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 870 | | /// select the version of the predictive model to run, and whether |
| | 871 | | /// statistics are returned in the response.</param> |
| | 872 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 873 | | /// controlling the request lifetime.</param> |
| | 874 | | /// <returns>A result containing sentiment predictions for each of the documents |
| | 875 | | /// and predictions for each of the sentences each document contains.</returns> |
| | 876 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 877 | | /// status code.</exception> |
| | 878 | | public virtual Response<AnalyzeSentimentResultCollection> AnalyzeSentimentBatch(IEnumerable<string> documents, s |
| | 879 | | { |
| 10 | 880 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 6 | 881 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 6 | 882 | | options ??= new TextAnalyticsRequestOptions(); |
| | 883 | |
|
| 6 | 884 | | return AnalyzeSentimentBatch(documentInputs, options, cancellationToken); |
| | 885 | | } |
| | 886 | |
|
| | 887 | | /// <summary> |
| | 888 | | /// Runs a predictive model to identify the positive, negative or neutral |
| | 889 | | /// sentiment contained in the documents, as well as scores indicating |
| | 890 | | /// the model's confidence in each of the predicted sentiments. |
| | 891 | | /// For a list of languages supported by this operation, see |
| | 892 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 893 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 894 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 895 | | /// </summary> |
| | 896 | | /// <param name="documents">The documents to analyze.</param> |
| | 897 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 898 | | /// select the version of the predictive model to run, and whether |
| | 899 | | /// statistics are returned in the response.</param> |
| | 900 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 901 | | /// controlling the request lifetime.</param> |
| | 902 | | /// <returns>A result containing sentiment predictions for each of the documents |
| | 903 | | /// and predictions for each of the sentences each document contains.</returns> |
| | 904 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 905 | | /// status code.</exception> |
| | 906 | | public virtual async Task<Response<AnalyzeSentimentResultCollection>> AnalyzeSentimentBatchAsync(IEnumerable<Tex |
| | 907 | | { |
| 12 | 908 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 10 | 909 | | options ??= new TextAnalyticsRequestOptions(); |
| | 910 | |
|
| 10 | 911 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Analyze |
| 10 | 912 | | scope.Start(); |
| | 913 | |
|
| | 914 | | try |
| | 915 | | { |
| 10 | 916 | | using Request request = CreateDocumentInputRequest(documents, options, SentimentRoute); |
| 10 | 917 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 918 | |
|
| 10 | 919 | | switch (response.Status) |
| | 920 | | { |
| | 921 | | case 200: |
| 10 | 922 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 10 | 923 | | AnalyzeSentimentResultCollection results = await CreateAnalyzeSentimentResponseAsync(response, m |
| 10 | 924 | | return Response.FromValue(results, response); |
| | 925 | | default: |
| 0 | 926 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 927 | | } |
| | 928 | | } |
| 0 | 929 | | catch (Exception e) |
| | 930 | | { |
| 0 | 931 | | scope.Failed(e); |
| 0 | 932 | | throw; |
| | 933 | | } |
| 10 | 934 | | } |
| | 935 | |
|
| | 936 | | /// <summary> |
| | 937 | | /// Runs a predictive model to identify the positive, negative or neutral |
| | 938 | | /// sentiment contained in the documents, as well as scores indicating |
| | 939 | | /// the model's confidence in each of the predicted sentiments. |
| | 940 | | /// For a list of languages supported by this operation, see |
| | 941 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 942 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 943 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 944 | | /// </summary> |
| | 945 | | /// <param name="documents">The documents to analyze.</param> |
| | 946 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 947 | | /// select the version of the predictive model to run, and whether |
| | 948 | | /// statistics are returned in the response.</param> |
| | 949 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 950 | | /// controlling the request lifetime.</param> |
| | 951 | | /// <returns>A result containing sentiment predictions for each of the documents |
| | 952 | | /// and predictions for each of the sentences each document contains.</returns> |
| | 953 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 954 | | /// status code.</exception> |
| | 955 | | public virtual Response<AnalyzeSentimentResultCollection> AnalyzeSentimentBatch(IEnumerable<TextDocumentInput> d |
| | 956 | | { |
| 12 | 957 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 10 | 958 | | options ??= new TextAnalyticsRequestOptions(); |
| | 959 | |
|
| 10 | 960 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Analyze |
| 10 | 961 | | scope.Start(); |
| | 962 | |
|
| | 963 | | try |
| | 964 | | { |
| 10 | 965 | | using Request request = CreateDocumentInputRequest(documents, options, SentimentRoute); |
| 10 | 966 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 967 | |
|
| 10 | 968 | | switch (response.Status) |
| | 969 | | { |
| | 970 | | case 200: |
| 10 | 971 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 10 | 972 | | AnalyzeSentimentResultCollection results = CreateAnalyzeSentimentResponse(response, map); |
| 10 | 973 | | return Response.FromValue(results, response); |
| | 974 | | default: |
| 0 | 975 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 976 | | } |
| | 977 | | } |
| 0 | 978 | | catch (Exception e) |
| | 979 | | { |
| 0 | 980 | | scope.Failed(e); |
| 0 | 981 | | throw; |
| | 982 | | } |
| 10 | 983 | | } |
| | 984 | |
|
| | 985 | | #endregion |
| | 986 | |
|
| | 987 | | #region Extract Key Phrases |
| | 988 | |
|
| | 989 | | /// <summary> |
| | 990 | | /// Runs a model to identify a collection of significant phrases |
| | 991 | | /// found in the passed-in document. |
| | 992 | | /// For example, for the document "The food was delicious and there |
| | 993 | | /// were wonderful staff", the API returns the main talking points: "food" |
| | 994 | | /// and "wonderful staff". |
| | 995 | | /// For a list of languages supported by this operation, see |
| | 996 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 997 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 998 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 999 | | /// </summary> |
| | 1000 | | /// <param name="document">The document to analyze.</param> |
| | 1001 | | /// <param name="language">The language that the document is written in. |
| | 1002 | | /// If unspecified, this value will be set to the default language in |
| | 1003 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 1004 | | /// service. If set to an empty string, the service will apply a model |
| | 1005 | | /// where the language is explicitly set to "None".</param> |
| | 1006 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1007 | | /// controlling the request lifetime.</param> |
| | 1008 | | /// <returns>A result containing the collection of key phrases identified |
| | 1009 | | /// in the document.</returns> |
| | 1010 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1011 | | /// status code.</exception> |
| | 1012 | | public virtual async Task<Response<KeyPhraseCollection>> ExtractKeyPhrasesAsync(string document, string language |
| | 1013 | | { |
| 10 | 1014 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 1015 | |
|
| 6 | 1016 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Extract |
| 6 | 1017 | | scope.AddAttribute("document", document); |
| 6 | 1018 | | scope.Start(); |
| | 1019 | |
|
| | 1020 | | try |
| | 1021 | | { |
| 6 | 1022 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 6 | 1023 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), KeyPhra |
| 6 | 1024 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 1025 | |
|
| 6 | 1026 | | switch (response.Status) |
| | 1027 | | { |
| | 1028 | | case 200: |
| 6 | 1029 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 6 | 1030 | | ExtractKeyPhrasesResultCollection results = await CreateKeyPhraseResponseAsync(response, map, ca |
| 6 | 1031 | | if (results[0].HasError) |
| | 1032 | | { |
| | 1033 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 1034 | | TextAnalyticsError error = results[0].Error; |
| 0 | 1035 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, er |
| | 1036 | | } |
| 6 | 1037 | | return Response.FromValue((KeyPhraseCollection) results[0].KeyPhrases, response); |
| | 1038 | | default: |
| 0 | 1039 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 1040 | | } |
| | 1041 | | } |
| 0 | 1042 | | catch (Exception e) |
| | 1043 | | { |
| 0 | 1044 | | scope.Failed(e); |
| 0 | 1045 | | throw; |
| | 1046 | | } |
| 6 | 1047 | | } |
| | 1048 | |
|
| | 1049 | | /// <summary> |
| | 1050 | | /// Runs a model to identify a collection of significant phrases |
| | 1051 | | /// found in the passed-in document. |
| | 1052 | | /// For example, for the document "The food was delicious and there |
| | 1053 | | /// were wonderful staff", the API returns the main talking points: "food" |
| | 1054 | | /// and "wonderful staff". |
| | 1055 | | /// For a list of languages supported by this operation, see |
| | 1056 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1057 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1058 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1059 | | /// </summary> |
| | 1060 | | /// <param name="document">The document to analyze.</param> |
| | 1061 | | /// <param name="language">The language that the document is written in. |
| | 1062 | | /// If unspecified, this value will be set to the default language in |
| | 1063 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 1064 | | /// service. If set to an empty string, the service will apply a model |
| | 1065 | | /// where the language is explicitly set to "None".</param> |
| | 1066 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1067 | | /// controlling the request lifetime.</param> |
| | 1068 | | /// <returns>A result containing the collection of key phrases identified |
| | 1069 | | /// in the document.</returns> |
| | 1070 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1071 | | /// status code.</exception> |
| | 1072 | | public virtual Response<KeyPhraseCollection> ExtractKeyPhrases(string document, string language = default, Cance |
| | 1073 | | { |
| 10 | 1074 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 1075 | |
|
| 6 | 1076 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Extract |
| 6 | 1077 | | scope.AddAttribute("document", document); |
| 6 | 1078 | | scope.Start(); |
| | 1079 | |
|
| | 1080 | | try |
| | 1081 | | { |
| 6 | 1082 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 6 | 1083 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), KeyPhra |
| 6 | 1084 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 1085 | |
|
| 6 | 1086 | | switch (response.Status) |
| | 1087 | | { |
| | 1088 | | case 200: |
| 6 | 1089 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 6 | 1090 | | ExtractKeyPhrasesResultCollection results = CreateKeyPhraseResponse(response, map); |
| 6 | 1091 | | if (results[0].HasError) |
| | 1092 | | { |
| | 1093 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 1094 | | TextAnalyticsError error = results[0].Error; |
| 0 | 1095 | | throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCo |
| | 1096 | | } |
| 6 | 1097 | | return Response.FromValue((KeyPhraseCollection)results[0].KeyPhrases, response); |
| | 1098 | | default: |
| 0 | 1099 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 1100 | | } |
| | 1101 | | } |
| 0 | 1102 | | catch (Exception e) |
| | 1103 | | { |
| 0 | 1104 | | scope.Failed(e); |
| 0 | 1105 | | throw; |
| | 1106 | | } |
| 6 | 1107 | | } |
| | 1108 | |
|
| | 1109 | | /// <summary> |
| | 1110 | | /// Runs a model to identify a collection of significant phrases |
| | 1111 | | /// found in the passed-in documents. |
| | 1112 | | /// For example, for the document "The food was delicious and there |
| | 1113 | | /// were wonderful staff", the API returns the main talking points: "food" |
| | 1114 | | /// and "wonderful staff". |
| | 1115 | | /// For a list of languages supported by this operation, see |
| | 1116 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1117 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1118 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1119 | | /// </summary> |
| | 1120 | | /// <param name="documents">The documents to analyze.</param> |
| | 1121 | | /// <param name="language">The language that all the documents are |
| | 1122 | | /// written in. If unspecified, this value will be set to the default |
| | 1123 | | /// language in <see cref="TextAnalyticsClientOptions"/> in the request |
| | 1124 | | /// sent to the service. If set to an empty string, the service will apply a model |
| | 1125 | | /// where the language is explicitly set to "None".</param> |
| | 1126 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1127 | | /// select the version of the predictive model to run, and whether |
| | 1128 | | /// statistics are returned in the response.</param> |
| | 1129 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1130 | | /// controlling the request lifetime.</param> |
| | 1131 | | /// <returns>A result containing the collection of key phrases identified |
| | 1132 | | /// in each of the documents.</returns> |
| | 1133 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1134 | | /// status code.</exception> |
| | 1135 | | public virtual async Task<Response<ExtractKeyPhrasesResultCollection>> ExtractKeyPhrasesBatchAsync(IEnumerable<s |
| | 1136 | | { |
| 10 | 1137 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 6 | 1138 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 6 | 1139 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1140 | |
|
| 6 | 1141 | | return await ExtractKeyPhrasesBatchAsync(documentInputs, options, cancellationToken).ConfigureAwait(false); |
| 6 | 1142 | | } |
| | 1143 | |
|
| | 1144 | | /// <summary> |
| | 1145 | | /// Runs a model to identify a collection of significant phrases |
| | 1146 | | /// found in the passed-in documents. |
| | 1147 | | /// For example, for the document "The food was delicious and there |
| | 1148 | | /// were wonderful staff", the API returns the main talking points: "food" |
| | 1149 | | /// and "wonderful staff". |
| | 1150 | | /// For a list of languages supported by this operation, see |
| | 1151 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1152 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1153 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1154 | | /// </summary> |
| | 1155 | | /// <param name="documents">The documents to analyze.</param> |
| | 1156 | | /// <param name="language">The language that all the documents are |
| | 1157 | | /// written in. If unspecified, this value will be set to the default |
| | 1158 | | /// language in <see cref="TextAnalyticsClientOptions"/> in the request |
| | 1159 | | /// sent to the service. If set to an empty string, the service will apply a model |
| | 1160 | | /// where the language is explicitly set to "None".</param> |
| | 1161 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1162 | | /// select the version of the predictive model to run, and whether |
| | 1163 | | /// statistics are returned in the response.</param> |
| | 1164 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1165 | | /// controlling the request lifetime.</param> |
| | 1166 | | /// <returns>A result containing the collection of key phrases identified |
| | 1167 | | /// in each of the documents.</returns> |
| | 1168 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1169 | | /// status code.</exception> |
| | 1170 | | public virtual Response<ExtractKeyPhrasesResultCollection> ExtractKeyPhrasesBatch(IEnumerable<string> documents, |
| | 1171 | | { |
| 10 | 1172 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 6 | 1173 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 6 | 1174 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1175 | |
|
| 6 | 1176 | | return ExtractKeyPhrasesBatch(documentInputs, options, cancellationToken); |
| | 1177 | | } |
| | 1178 | |
|
| | 1179 | | /// <summary> |
| | 1180 | | /// Runs a model to identify a collection of significant phrases |
| | 1181 | | /// found in the passed-in documents. |
| | 1182 | | /// For example, for the document "The food was delicious and there |
| | 1183 | | /// were wonderful staff", the API returns the main talking points: "food" |
| | 1184 | | /// and "wonderful staff". |
| | 1185 | | /// For a list of languages supported by this operation, see |
| | 1186 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1187 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1188 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1189 | | /// </summary> |
| | 1190 | | /// <param name="documents">The documents to analyze.</param> |
| | 1191 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1192 | | /// select the version of the predictive model to run, and whether |
| | 1193 | | /// statistics are returned in the response.</param> |
| | 1194 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1195 | | /// controlling the request lifetime.</param> |
| | 1196 | | /// <returns>A result containing the collection of key phrases identified |
| | 1197 | | /// in each of the documents.</returns> |
| | 1198 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1199 | | /// status code.</exception> |
| | 1200 | | public virtual async Task<Response<ExtractKeyPhrasesResultCollection>> ExtractKeyPhrasesBatchAsync(IEnumerable<T |
| | 1201 | | { |
| 12 | 1202 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 10 | 1203 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1204 | |
|
| 10 | 1205 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Extract |
| 10 | 1206 | | scope.Start(); |
| | 1207 | |
|
| | 1208 | | try |
| | 1209 | | { |
| 10 | 1210 | | using Request request = CreateDocumentInputRequest(documents, options, KeyPhrasesRoute); |
| 10 | 1211 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 1212 | |
|
| 10 | 1213 | | switch (response.Status) |
| | 1214 | | { |
| | 1215 | | case 200: |
| 10 | 1216 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 10 | 1217 | | ExtractKeyPhrasesResultCollection results = await CreateKeyPhraseResponseAsync(response, map, ca |
| 10 | 1218 | | return Response.FromValue(results, response); |
| | 1219 | | default: |
| 0 | 1220 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 1221 | | } |
| | 1222 | | } |
| 0 | 1223 | | catch (Exception e) |
| | 1224 | | { |
| 0 | 1225 | | scope.Failed(e); |
| 0 | 1226 | | throw; |
| | 1227 | | } |
| 10 | 1228 | | } |
| | 1229 | |
|
| | 1230 | | /// <summary> |
| | 1231 | | /// Runs a model to identify a collection of significant phrases |
| | 1232 | | /// found in the passed-in documents. |
| | 1233 | | /// For example, for the document "The food was delicious and there |
| | 1234 | | /// were wonderful staff", the API returns the main talking points: "food" |
| | 1235 | | /// and "wonderful staff". |
| | 1236 | | /// For a list of languages supported by this operation, see |
| | 1237 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1238 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1239 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1240 | | /// </summary> |
| | 1241 | | /// <param name="documents">The documents to analyze.</param> |
| | 1242 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1243 | | /// select the version of the predictive model to run, and whether |
| | 1244 | | /// statistics are returned in the response.</param> |
| | 1245 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1246 | | /// controlling the request lifetime.</param> |
| | 1247 | | /// <returns>A result containing the collection of key phrases identified |
| | 1248 | | /// in each of the documents.</returns> |
| | 1249 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1250 | | /// status code.</exception> |
| | 1251 | | public virtual Response<ExtractKeyPhrasesResultCollection> ExtractKeyPhrasesBatch(IEnumerable<TextDocumentInput> |
| | 1252 | | { |
| 12 | 1253 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 10 | 1254 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1255 | |
|
| 10 | 1256 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Extract |
| 10 | 1257 | | scope.Start(); |
| | 1258 | |
|
| | 1259 | | try |
| | 1260 | | { |
| 10 | 1261 | | using Request request = CreateDocumentInputRequest(documents, options, KeyPhrasesRoute); |
| 10 | 1262 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 1263 | |
|
| 10 | 1264 | | switch (response.Status) |
| | 1265 | | { |
| | 1266 | | case 200: |
| 10 | 1267 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 10 | 1268 | | ExtractKeyPhrasesResultCollection results = CreateKeyPhraseResponse(response, map); |
| 10 | 1269 | | return Response.FromValue(results, response); |
| | 1270 | | default: |
| 0 | 1271 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 1272 | | } |
| | 1273 | | } |
| 0 | 1274 | | catch (Exception e) |
| | 1275 | | { |
| 0 | 1276 | | scope.Failed(e); |
| 0 | 1277 | | throw; |
| | 1278 | | } |
| 10 | 1279 | | } |
| | 1280 | |
|
| | 1281 | | #endregion |
| | 1282 | |
|
| | 1283 | | #region Linked Entities |
| | 1284 | |
|
| | 1285 | | /// <summary> |
| | 1286 | | /// Runs a predictive model to identify a collection of entities |
| | 1287 | | /// found in the passed-in document, and include information linking the |
| | 1288 | | /// entities to their corresponding entries in a well-known knowledge base. |
| | 1289 | | /// For a list of languages supported by this operation, see |
| | 1290 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1291 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1292 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1293 | | /// </summary> |
| | 1294 | | /// <param name="document">The document to analyze.</param> |
| | 1295 | | /// <param name="language">The language that the document is written in. |
| | 1296 | | /// If unspecified, this value will be set to the default language in |
| | 1297 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 1298 | | /// service. If set to an empty string, the service will apply a model |
| | 1299 | | /// where the language is explicitly set to "None".</param> |
| | 1300 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1301 | | /// controlling the request lifetime.</param> |
| | 1302 | | /// <returns>A result containing the collection of linked entities identified |
| | 1303 | | /// in the document, as well as scores indicating the confidence |
| | 1304 | | /// that the entity correctly matches the identified substring.</returns> |
| | 1305 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1306 | | /// status code.</exception> |
| | 1307 | | public virtual async Task<Response<LinkedEntityCollection>> RecognizeLinkedEntitiesAsync(string document, string |
| | 1308 | | { |
| 8 | 1309 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 1310 | |
|
| 4 | 1311 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 4 | 1312 | | scope.AddAttribute("document", document); |
| 4 | 1313 | | scope.Start(); |
| | 1314 | |
|
| | 1315 | | try |
| | 1316 | | { |
| 4 | 1317 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 4 | 1318 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), EntityL |
| 4 | 1319 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 1320 | |
|
| 4 | 1321 | | switch (response.Status) |
| | 1322 | | { |
| | 1323 | | case 200: |
| 4 | 1324 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 4 | 1325 | | RecognizeLinkedEntitiesResultCollection results = await CreateLinkedEntityResponseAsync(response |
| 4 | 1326 | | if (results[0].HasError) |
| | 1327 | | { |
| | 1328 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 1329 | | TextAnalyticsError error = results[0].Error; |
| 0 | 1330 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response, error.Message, er |
| | 1331 | | } |
| 4 | 1332 | | return Response.FromValue((LinkedEntityCollection)results[0].Entities, response); |
| | 1333 | | default: |
| 0 | 1334 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 1335 | | } |
| | 1336 | | } |
| 0 | 1337 | | catch (Exception e) |
| | 1338 | | { |
| 0 | 1339 | | scope.Failed(e); |
| 0 | 1340 | | throw; |
| | 1341 | | } |
| 4 | 1342 | | } |
| | 1343 | |
|
| | 1344 | | /// <summary> |
| | 1345 | | /// Runs a predictive model to identify a collection of entities |
| | 1346 | | /// found in the passed-in document, and include information linking the |
| | 1347 | | /// entities to their corresponding entries in a well-known knowledge base. |
| | 1348 | | /// For a list of languages supported by this operation, see |
| | 1349 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1350 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1351 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1352 | | /// </summary> |
| | 1353 | | /// <param name="document">The document to analyze.</param> |
| | 1354 | | /// <param name="language">The language that the document is written in. |
| | 1355 | | /// If unspecified, this value will be set to the default language in |
| | 1356 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 1357 | | /// service. If set to an empty string, the service will apply a model |
| | 1358 | | /// where the language is explicitly set to "None".</param> |
| | 1359 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1360 | | /// controlling the request lifetime.</param> |
| | 1361 | | /// <returns>A result containing the collection of linked entities identified |
| | 1362 | | /// in the document, as well as scores indicating the confidence |
| | 1363 | | /// that the entity correctly matches the identified substring.</returns> |
| | 1364 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1365 | | /// status code.</exception> |
| | 1366 | | public virtual Response<LinkedEntityCollection> RecognizeLinkedEntities(string document, string language = defau |
| | 1367 | | { |
| 8 | 1368 | | Argument.AssertNotNullOrEmpty(document, nameof(document)); |
| | 1369 | |
|
| 4 | 1370 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 4 | 1371 | | scope.AddAttribute("document", document); |
| 4 | 1372 | | scope.Start(); |
| | 1373 | |
|
| | 1374 | | try |
| | 1375 | | { |
| 4 | 1376 | | TextDocumentInput[] documents = new TextDocumentInput[] { ConvertToDocumentInput(document, language) }; |
| 4 | 1377 | | using Request request = CreateDocumentInputRequest(documents, new TextAnalyticsRequestOptions(), EntityL |
| 4 | 1378 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 1379 | |
|
| 4 | 1380 | | switch (response.Status) |
| | 1381 | | { |
| | 1382 | | case 200: |
| 4 | 1383 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 4 | 1384 | | RecognizeLinkedEntitiesResultCollection results = CreateLinkedEntityResponse(response, map); |
| 4 | 1385 | | if (results[0].HasError) |
| | 1386 | | { |
| | 1387 | | // only one document, so we can ignore the id and grab the first error message. |
| 0 | 1388 | | TextAnalyticsError error = results[0].Error; |
| 0 | 1389 | | throw _clientDiagnostics.CreateRequestFailedException(response, error.Message, error.ErrorCo |
| | 1390 | | } |
| 4 | 1391 | | return Response.FromValue((LinkedEntityCollection)results[0].Entities, response); |
| | 1392 | | default: |
| 0 | 1393 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 1394 | | } |
| | 1395 | | } |
| 0 | 1396 | | catch (Exception e) |
| | 1397 | | { |
| 0 | 1398 | | scope.Failed(e); |
| 0 | 1399 | | throw; |
| | 1400 | | } |
| 4 | 1401 | | } |
| | 1402 | |
|
| | 1403 | | /// <summary> |
| | 1404 | | /// Runs a predictive model to identify a collection of entities |
| | 1405 | | /// found in the passed-in documents, and include information linking the |
| | 1406 | | /// entities to their corresponding entries in a well-known knowledge base. |
| | 1407 | | /// For a list of languages supported by this operation, see |
| | 1408 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1409 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1410 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1411 | | /// </summary> |
| | 1412 | | /// <param name="documents">The documents to analyze.</param> |
| | 1413 | | /// <param name="language">The language that the documents are written in. |
| | 1414 | | /// If unspecified, this value will be set to the default language in |
| | 1415 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 1416 | | /// service. If set to an empty string, the service will apply a model |
| | 1417 | | /// where the language is explicitly set to "None".</param> |
| | 1418 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1419 | | /// select the version of the predictive model to run, and whether |
| | 1420 | | /// statistics are returned in the response.</param> |
| | 1421 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1422 | | /// controlling the request lifetime.</param> |
| | 1423 | | /// <returns>A result containing the collection of entities identified |
| | 1424 | | /// for each of the documents, as well as scores indicating the confidence |
| | 1425 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 1426 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1427 | | /// status code.</exception> |
| | 1428 | | public virtual async Task<Response<RecognizeLinkedEntitiesResultCollection>> RecognizeLinkedEntitiesBatchAsync(I |
| | 1429 | | { |
| 12 | 1430 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 8 | 1431 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 8 | 1432 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1433 | |
|
| 8 | 1434 | | return await RecognizeLinkedEntitiesBatchAsync(documentInputs, options, cancellationToken).ConfigureAwait(fa |
| 6 | 1435 | | } |
| | 1436 | |
|
| | 1437 | | /// <summary> |
| | 1438 | | /// Runs a predictive model to identify a collection of entities |
| | 1439 | | /// found in the passed-in documents, and include information linking the |
| | 1440 | | /// entities to their corresponding entries in a well-known knowledge base. |
| | 1441 | | /// For a list of languages supported by this operation, see |
| | 1442 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1443 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1444 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1445 | | /// </summary> |
| | 1446 | | /// <param name="documents">The documents to analyze.</param> |
| | 1447 | | /// <param name="language">The language that the documents are written in. |
| | 1448 | | /// If unspecified, this value will be set to the default language in |
| | 1449 | | /// <see cref="TextAnalyticsClientOptions"/> in the request sent to the |
| | 1450 | | /// service. If set to an empty string, the service will apply a model |
| | 1451 | | /// where the language is explicitly set to "None".</param> |
| | 1452 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1453 | | /// select the version of the predictive model to run, and whether |
| | 1454 | | /// statistics are returned in the response.</param> |
| | 1455 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1456 | | /// controlling the request lifetime.</param> |
| | 1457 | | /// <returns>A result containing the collection of entities identified |
| | 1458 | | /// for each of the documents, as well as scores indicating the confidence |
| | 1459 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 1460 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1461 | | /// status code.</exception> |
| | 1462 | | public virtual Response<RecognizeLinkedEntitiesResultCollection> RecognizeLinkedEntitiesBatch(IEnumerable<string |
| | 1463 | | { |
| 12 | 1464 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 8 | 1465 | | List<TextDocumentInput> documentInputs = ConvertToDocumentInputs(documents, language); |
| 8 | 1466 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1467 | |
|
| 8 | 1468 | | return RecognizeLinkedEntitiesBatch(documentInputs, options, cancellationToken); |
| | 1469 | | } |
| | 1470 | |
|
| | 1471 | | /// <summary> |
| | 1472 | | /// Runs a predictive model to identify a collection of entities |
| | 1473 | | /// found in the passed-in documents, and include information linking the |
| | 1474 | | /// entities to their corresponding entries in a well-known knowledge base. |
| | 1475 | | /// For a list of languages supported by this operation, see |
| | 1476 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1477 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1478 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1479 | | /// </summary> |
| | 1480 | | /// <param name="documents">The documents to analyze.</param> |
| | 1481 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1482 | | /// select the version of the predictive model to run, and whether |
| | 1483 | | /// statistics are returned in the response.</param> |
| | 1484 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1485 | | /// controlling the request lifetime.</param> |
| | 1486 | | /// <returns>A result containing the collection of entities identified |
| | 1487 | | /// for each of the documents, as well as scores indicating the confidence |
| | 1488 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 1489 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1490 | | /// status code.</exception> |
| | 1491 | | public virtual async Task<Response<RecognizeLinkedEntitiesResultCollection>> RecognizeLinkedEntitiesBatchAsync(I |
| | 1492 | | { |
| 14 | 1493 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 12 | 1494 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1495 | |
|
| 12 | 1496 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 12 | 1497 | | scope.Start(); |
| | 1498 | |
|
| | 1499 | | try |
| | 1500 | | { |
| 12 | 1501 | | using Request request = CreateDocumentInputRequest(documents, options, EntityLinkingRoute); |
| 12 | 1502 | | Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| | 1503 | |
|
| 12 | 1504 | | switch (response.Status) |
| | 1505 | | { |
| | 1506 | | case 200: |
| 10 | 1507 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 10 | 1508 | | RecognizeLinkedEntitiesResultCollection results = await CreateLinkedEntityResponseAsync(response |
| 10 | 1509 | | return Response.FromValue(results, response); |
| | 1510 | | default: |
| 2 | 1511 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(response).ConfigureAwait(false) |
| | 1512 | | } |
| | 1513 | | } |
| 2 | 1514 | | catch (Exception e) |
| | 1515 | | { |
| 2 | 1516 | | scope.Failed(e); |
| 2 | 1517 | | throw; |
| | 1518 | | } |
| 10 | 1519 | | } |
| | 1520 | |
|
| | 1521 | | /// <summary> |
| | 1522 | | /// Runs a predictive model to identify a collection of entities |
| | 1523 | | /// found in the passed-in documents, and include information linking the |
| | 1524 | | /// entities to their corresponding entries in a well-known knowledge base. |
| | 1525 | | /// For a list of languages supported by this operation, see |
| | 1526 | | /// <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/language-support"/>. |
| | 1527 | | /// For document length limits, maximum batch size, and supported text encoding, see |
| | 1528 | | /// <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits"/>. |
| | 1529 | | /// </summary> |
| | 1530 | | /// <param name="documents">The documents to analyze.</param> |
| | 1531 | | /// <param name="options"><see cref="TextAnalyticsRequestOptions"/> used to |
| | 1532 | | /// select the version of the predictive model to run, and whether |
| | 1533 | | /// statistics are returned in the response.</param> |
| | 1534 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> |
| | 1535 | | /// controlling the request lifetime.</param> |
| | 1536 | | /// <returns>A result containing the collection of entities identified |
| | 1537 | | /// for each of the documents, as well as scores indicating the confidence |
| | 1538 | | /// that a given entity correctly matches the identified substring.</returns> |
| | 1539 | | /// <exception cref="RequestFailedException">Service returned a non-success |
| | 1540 | | /// status code.</exception> |
| | 1541 | | public virtual Response<RecognizeLinkedEntitiesResultCollection> RecognizeLinkedEntitiesBatch(IEnumerable<TextDo |
| | 1542 | | { |
| 14 | 1543 | | Argument.AssertNotNullOrEmpty(documents, nameof(documents)); |
| 12 | 1544 | | options ??= new TextAnalyticsRequestOptions(); |
| | 1545 | |
|
| 12 | 1546 | | using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TextAnalyticsClient)}.{nameof(Recogni |
| 12 | 1547 | | scope.Start(); |
| | 1548 | |
|
| | 1549 | | try |
| | 1550 | | { |
| 12 | 1551 | | using Request request = CreateDocumentInputRequest(documents, options, EntityLinkingRoute); |
| 12 | 1552 | | Response response = _pipeline.SendRequest(request, cancellationToken); |
| | 1553 | |
|
| 12 | 1554 | | switch (response.Status) |
| | 1555 | | { |
| | 1556 | | case 200: |
| 10 | 1557 | | IDictionary<string, int> map = CreateIdToIndexMap(documents); |
| 10 | 1558 | | RecognizeLinkedEntitiesResultCollection results = CreateLinkedEntityResponse(response, map); |
| 10 | 1559 | | return Response.FromValue(results, response); |
| | 1560 | | default: |
| 2 | 1561 | | throw _clientDiagnostics.CreateRequestFailedException(response); |
| | 1562 | | } |
| | 1563 | | } |
| 2 | 1564 | | catch (Exception e) |
| | 1565 | | { |
| 2 | 1566 | | scope.Failed(e); |
| 2 | 1567 | | throw; |
| | 1568 | | } |
| 10 | 1569 | | } |
| | 1570 | |
|
| | 1571 | | #endregion |
| | 1572 | |
|
| | 1573 | | #region Common |
| | 1574 | |
|
| | 1575 | | private static IDictionary<string, int> CreateIdToIndexMap<T>(IEnumerable<T> documents) |
| | 1576 | | { |
| 148 | 1577 | | var map = new Dictionary<string, int>(documents.Count()); |
| | 1578 | |
|
| 148 | 1579 | | int i = 0; |
| 904 | 1580 | | foreach (T item in documents) |
| | 1581 | | { |
| 304 | 1582 | | string id = item switch |
| 304 | 1583 | | { |
| 544 | 1584 | | TextDocumentInput tdi => tdi.Id, |
| 368 | 1585 | | LanguageInput ldi => ldi.Id, |
| 0 | 1586 | | _ => throw new NotSupportedException(), |
| 304 | 1587 | | }; |
| | 1588 | |
|
| 304 | 1589 | | map[id] = i++; |
| | 1590 | | } |
| | 1591 | |
|
| 148 | 1592 | | return map; |
| | 1593 | | } |
| | 1594 | |
|
| | 1595 | | internal virtual TextDocumentInput ConvertToDocumentInput(string document, string language, int id = 0) |
| 212 | 1596 | | => new TextDocumentInput($"{id}", document) { Language = language ?? _options.DefaultLanguage }; |
| | 1597 | |
|
| | 1598 | | private List<TextDocumentInput> ConvertToDocumentInputs(IEnumerable<string> documents, string language) |
| 216 | 1599 | | => documents.Select((document, i) => ConvertToDocumentInput(document, language, i)).ToList(); |
| | 1600 | |
|
| | 1601 | | private LanguageInput ConvertToLanguageInput(string document, string countryHint, int id = 0) |
| 64 | 1602 | | => new LanguageInput($"{id}", document) { CountryHint = countryHint ?? _options.DefaultCountryHint }; |
| | 1603 | |
|
| | 1604 | | private LanguageBatchInput ConvertToLanguageInputs(IEnumerable<string> documents, string countryHint) |
| 44 | 1605 | | => new LanguageBatchInput(documents.Select((document, i) => ConvertToLanguageInput(document, countryHint, i) |
| | 1606 | |
|
| | 1607 | | private LanguageBatchInput ConvertToLanguageInputs(IEnumerable<DetectLanguageInput> documents) |
| 40 | 1608 | | => new LanguageBatchInput(documents.Select((document) => new LanguageInput(document.Id, document.Text) { Cou |
| | 1609 | |
|
| | 1610 | | private Request CreateDocumentInputRequest(IEnumerable<TextDocumentInput> documents, TextAnalyticsRequestOptions |
| | 1611 | | { |
| 136 | 1612 | | Request request = _pipeline.CreateRequest(); |
| | 1613 | |
|
| 136 | 1614 | | ReadOnlyMemory<byte> content = TextAnalyticsServiceSerializer.SerializeDocumentInputs(documents, _options.De |
| | 1615 | |
|
| 136 | 1616 | | request.Method = RequestMethod.Post; |
| 136 | 1617 | | BuildUriForRoute(route, request.Uri, options); |
| | 1618 | |
|
| 136 | 1619 | | request.Headers.Add(HttpHeader.Common.JsonContentType); |
| 136 | 1620 | | request.Content = RequestContent.Create(content); |
| | 1621 | |
|
| 136 | 1622 | | return request; |
| | 1623 | | } |
| | 1624 | |
|
| | 1625 | | private IDictionary<string,string> CreateAdditionalInformation(TextAnalyticsError error) |
| | 1626 | | { |
| 4 | 1627 | | if (string.IsNullOrEmpty(error.Target)) |
| 4 | 1628 | | return null; |
| 0 | 1629 | | return new Dictionary<string, string> { { "Target", error.Target } }; |
| | 1630 | | } |
| | 1631 | |
|
| | 1632 | | #endregion |
| | 1633 | | } |
| | 1634 | | } |