/
TextAnalyticsClient.java
627 lines (599 loc) · 33.5 KB
/
TextAnalyticsClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.textanalytics;
import com.azure.ai.textanalytics.util.AnalyzeSentimentResultCollection;
import com.azure.ai.textanalytics.models.CategorizedEntity;
import com.azure.ai.textanalytics.models.CategorizedEntityCollection;
import com.azure.ai.textanalytics.models.DetectLanguageInput;
import com.azure.ai.textanalytics.util.DetectLanguageResultCollection;
import com.azure.ai.textanalytics.models.DetectedLanguage;
import com.azure.ai.textanalytics.models.DocumentSentiment;
import com.azure.ai.textanalytics.util.ExtractKeyPhrasesResultCollection;
import com.azure.ai.textanalytics.models.KeyPhrasesCollection;
import com.azure.ai.textanalytics.models.LinkedEntity;
import com.azure.ai.textanalytics.models.LinkedEntityCollection;
import com.azure.ai.textanalytics.util.RecognizeEntitiesResultCollection;
import com.azure.ai.textanalytics.util.RecognizeLinkedEntitiesResultCollection;
import com.azure.ai.textanalytics.models.TextAnalyticsError;
import com.azure.ai.textanalytics.models.TextAnalyticsException;
import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions;
import com.azure.ai.textanalytics.models.TextDocumentInput;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import java.util.Objects;
import static com.azure.ai.textanalytics.implementation.Utility.inputDocumentsValidation;
/**
* This class provides a synchronous client that contains all the operations that apply to Azure Text Analytics.
* Operations allowed by the client are language detection, entities recognition, linked entities recognition,
* key phrases extraction, and sentiment analysis of a document or a list of documents.
*
* <p><strong>Instantiating a synchronous Text Analytics Client</strong></p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.instantiation}
*
* <p>View {@link TextAnalyticsClientBuilder this} for additional ways to construct the client.</p>
*
* @see TextAnalyticsClientBuilder
*/
@ServiceClient(builder = TextAnalyticsClientBuilder.class)
public final class TextAnalyticsClient {
private final TextAnalyticsAsyncClient client;
/**
* Create a {@code TextAnalyticsClient client} that sends requests to the Text Analytics service's endpoint.
* Each service call goes through the {@link TextAnalyticsClientBuilder#pipeline http pipeline}.
*
* @param client The {@link TextAnalyticsClient} that the client routes its request through.
*/
TextAnalyticsClient(TextAnalyticsAsyncClient client) {
this.client = client;
}
/**
* Get default country hint code.
*
* @return The default country hint code
*/
public String getDefaultCountryHint() {
return client.getDefaultCountryHint();
}
/**
* Get default language when the builder is setup.
*
* @return The default language
*/
public String getDefaultLanguage() {
return client.getDefaultLanguage();
}
/**
* Returns the detected language and a confidence score between zero and one. Scores close to one indicate 100%
* certainty that the identified language is true.
*
* This method will use the default country hint that sets up in
* {@link TextAnalyticsClientBuilder#defaultCountryHint(String)}. If none is specified, service will use 'US' as
* the country hint.
*
* <p><strong>Code Sample</strong></p>
* <p>Detects the language of single document.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.detectLanguage#String}
*
* @param document The document to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
*
* @return The {@link DetectedLanguage detected language} of the document.
*
* @throws NullPointerException if {@code document} is {@code null}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public DetectedLanguage detectLanguage(String document) {
return detectLanguage(document, client.getDefaultCountryHint());
}
/**
* Returns the detected language and a confidence score between zero and one.
* Scores close to one indicate 100% certainty that the identified language is true.
*
* <p><strong>Code Sample</strong></p>
* <p>Detects the language of documents with a provided country hint.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.detectLanguage#String-String}
*
* @param document The document to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param countryHint Accepts two letter country codes specified by ISO 3166-1 alpha-2. Defaults to "US" if not
* specified. To remove this behavior you can reset this parameter by setting this value to empty string
* {@code countryHint} = "" or "none".
*
* @return The {@link DetectedLanguage detected language} of the document.
*
* @throws NullPointerException if {@code document} is {@code null}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public DetectedLanguage detectLanguage(String document, String countryHint) {
return client.detectLanguage(document, countryHint).block();
}
/**
* Detects Language for a batch of document with the provided country hint and request options.
*
* <p><strong>Code Sample</strong></p>
* <p>Detects the language in a list of documents with a provided country hint and request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.detectLanguageBatch#Iterable-String-TextAnalyticsRequestOptions}
*
* @param documents The list of documents to detect languages for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param countryHint Accepts two letter country codes specified by ISO 3166-1 alpha-2. Defaults to "US" if not
* specified. To remove this behavior you can reset this parameter by setting this value to empty string
* {@code countryHint} = "" or "none".
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
*
* @return A {@link DetectLanguageResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public DetectLanguageResultCollection detectLanguageBatch(
Iterable<String> documents, String countryHint, TextAnalyticsRequestOptions options) {
inputDocumentsValidation(documents);
return client.detectLanguageBatch(documents, countryHint, options).block();
}
/**
* Detects Language for a batch of {@link DetectLanguageInput document} with provided request options.
*
* <p><strong>Code Sample</strong></p>
* <p>Detects the languages with http response in a list of {@link DetectLanguageInput document} with provided
* request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.detectLanguageBatch#Iterable-TextAnalyticsRequestOptions-Context}
*
* @param documents The list of {@link DetectLanguageInput documents} to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
* @param context Additional context that is passed through the Http pipeline during the service call.
*
* @return A {@link Response} that contains a {@link DetectLanguageResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<DetectLanguageResultCollection> detectLanguageBatchWithResponse(
Iterable<DetectLanguageInput> documents, TextAnalyticsRequestOptions options, Context context) {
inputDocumentsValidation(documents);
return client.detectLanguageAsyncClient.detectLanguageBatchWithContext(documents, options, context).block();
}
// Categorized Entity
/**
* Returns a list of general categorized entities in the provided document.
*
* For a list of supported entity types, check: <a href="https://aka.ms/taner">this</a>
*
* This method will use the default language that sets up in
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognize the entities of documents</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeCategorizedEntities#String}
*
* @param document the document to recognize entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
*
* @return A {@link CategorizedEntityCollection} contains a list of
* {@link CategorizedEntity recognized categorized entities} and warnings.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public CategorizedEntityCollection recognizeEntities(String document) {
return recognizeEntities(document, client.getDefaultLanguage());
}
/**
* Returns a list of general categorized entities in the provided document with provided language code.
*
* For a list of supported entity types, check: <a href="https://aka.ms/taner">this</a>
* For a list of enabled languages, check: <a href="https://aka.ms/talangs">this</a>
*
* <p><strong>Code Sample</strong></p>
* <p>Recognizes the entities in a document with a provided language code.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeCategorizedEntities#String-String}
*
* @param document The document to recognize entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language. If not set, uses "en" for English as default.
*
* @return The {@link CategorizedEntityCollection} contains a list of
* {@link CategorizedEntity recognized categorized entities} and warnings.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public CategorizedEntityCollection recognizeEntities(String document, String language) {
Objects.requireNonNull(document, "'document' cannot be null.");
return client.recognizeEntities(document, language).block();
}
/**
* Returns a list of general categorized entities for the provided list of documents with provided language code
* and request options.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognizes the entities in a list of documents with a provided language code and request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeCategorizedEntitiesBatch#Iterable-String-TextAnalyticsRequestOptions}
*
* @param documents A list of documents to recognize entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language. If not set, uses "en" for English as default.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
*
* @return A {@link RecognizeEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public RecognizeEntitiesResultCollection recognizeEntitiesBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
inputDocumentsValidation(documents);
return client.recognizeEntitiesBatch(documents, language, options).block();
}
/**
* Returns a list of general categorized entities for the provided list of {@link TextDocumentInput document} with
* provided request options.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognizes the entities with http response in a list of {@link TextDocumentInput document} with provided
* request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeEntitiesBatch#Iterable-TextAnalyticsRequestOptions-Context}
*
* @param documents A list of {@link TextDocumentInput documents} to recognize entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
* @param context Additional context that is passed through the Http pipeline during the service call.
*
* @return A {@link Response} that contains a {@link RecognizeEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<RecognizeEntitiesResultCollection> recognizeEntitiesBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options, Context context) {
inputDocumentsValidation(documents);
return client.recognizeEntityAsyncClient.recognizeEntitiesBatchWithContext(documents, options, context).block();
}
// Linked Entities
/**
* Returns a list of recognized entities with links to a well-known knowledge base for the provided document.
* See <a href="https://aka.ms/talangs">this</a> for supported languages in Text Analytics API.
*
* This method will use the default language that sets up in
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognize the linked entities of documents</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeLinkedEntities#String}
*
* @param document the document to recognize linked entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
*
* @return A {@link LinkedEntityCollection} contains a list of {@link LinkedEntity recognized linked entities}.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public LinkedEntityCollection recognizeLinkedEntities(String document) {
return recognizeLinkedEntities(document, client.getDefaultLanguage());
}
/**
* Returns a list of recognized entities with links to a well-known knowledge base for the provided document with
* language code.
*
* See <a href="https://aka.ms/talangs">this</a> for supported languages in Text Analytics API.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognizes the linked entities in a document with a provided language code.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeLinkedEntities#String-String}
*
* @param document The document to recognize linked entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language for the document. If not set, uses "en" for
* English as default.
*
* @return A {@link LinkedEntityCollection} contains a list of {@link LinkedEntity recognized linked entities}.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public LinkedEntityCollection recognizeLinkedEntities(String document, String language) {
Objects.requireNonNull(document, "'document' cannot be null.");
return client.recognizeLinkedEntities(document, language).block();
}
/**
* Returns a list of recognized entities with links to a well-known knowledge base for the list of documents with
* provided language code and request options.
*
* See <a href="https://aka.ms/talangs">this</a> for supported languages in Text Analytics API.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognizes the linked entities in a list of documents with a provided language code and request options.
* </p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeLinkedEntitiesBatch#Iterable-String-TextAnalyticsRequestOptions}
*
* @param documents A list of documents to recognize linked entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language for the documents. If not set, uses "en" for
* English as default.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
*
* @return A {@link RecognizeLinkedEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public RecognizeLinkedEntitiesResultCollection recognizeLinkedEntitiesBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
inputDocumentsValidation(documents);
return client.recognizeLinkedEntitiesBatch(documents, language, options).block();
}
/**
* Returns a list of recognized entities with links to a well-known knowledge base for the list of
* {@link TextDocumentInput document} and request options.
*
* See <a href="https://aka.ms/talangs">this</a> for supported languages in Text Analytics API.
*
* <p><strong>Code Sample</strong></p>
* <p>Recognizes the linked entities with http response in a list of {@link TextDocumentInput} with request options.
* </p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.recognizeLinkedEntitiesBatch#Iterable-TextAnalyticsRequestOptions-Context}
*
* @param documents A list of {@link TextDocumentInput documents} to recognize linked entities for.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
* @param context Additional context that is passed through the Http pipeline during the service call.
*
* @return A {@link Response} that contains a {@link RecognizeLinkedEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<RecognizeLinkedEntitiesResultCollection>
recognizeLinkedEntitiesBatchWithResponse(Iterable<TextDocumentInput> documents,
TextAnalyticsRequestOptions options, Context context) {
inputDocumentsValidation(documents);
return client.recognizeLinkedEntityAsyncClient.recognizeLinkedEntitiesBatchWithContext(
documents, options, context).block();
}
// Key Phrase
/**
* Returns a list of strings denoting the key phrases in the document.
*
* This method will use the default language that sets up in
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p><strong>Code Sample</strong></p>
* <p>Extracts key phrases of documents</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.extractKeyPhrases#String}
*
* @param document The document to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
*
* @return A {@link KeyPhrasesCollection} contains a list of extracted key phrases.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public KeyPhrasesCollection extractKeyPhrases(String document) {
return extractKeyPhrases(document, client.getDefaultLanguage());
}
/**
* Returns a list of strings denoting the key phrases in the document.
* See <a href="https://aka.ms/talangs">this</a> for the list of enabled languages.
*
* <p><strong>Code Sample</strong></p>
* <p>Extracts key phrases in a document with a provided language representation.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.extractKeyPhrases#String-String-Context}
*
* @param document The document to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language for the document. If not set, uses "en" for
* English as default.
*
* @return A {@link KeyPhrasesCollection} contains a list of extracted key phrases.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public KeyPhrasesCollection extractKeyPhrases(String document, String language) {
Objects.requireNonNull(document, "'document' cannot be null.");
return client.extractKeyPhrases(document, language).block();
}
/**
* Returns a list of strings denoting the key phrases in the documents with provided language code and
* request options.
*
* See <a href="https://aka.ms/talangs">this</a> for the list of enabled languages.
*
* <p><strong>Code Sample</strong></p>
* <p>Extracts key phrases in a list of documents with a provided language code and request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.extractKeyPhrasesBatch#Iterable-String-TextAnalyticsRequestOptions}
*
* @param documents A list of documents to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language for the documents. If not set, uses "en" for
* English as default.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
*
* @return A {@link ExtractKeyPhrasesResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public ExtractKeyPhrasesResultCollection extractKeyPhrasesBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
inputDocumentsValidation(documents);
return client.extractKeyPhrasesBatch(documents, language, options).block();
}
/**
* Returns a list of strings denoting the key phrases in the a batch of {@link TextDocumentInput document} with
* request options.
*
* See <a href="https://aka.ms/talangs">this</a> for the list of enabled languages.
*
* <p><strong>Code Sample</strong></p>
* <p>Extracts key phrases with http response in a list of {@link TextDocumentInput} with request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.extractKeyPhrasesBatch#Iterable-TextAnalyticsRequestOptions-Context}
*
* @param documents A list of {@link TextDocumentInput documents} to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
* @param context Additional context that is passed through the Http pipeline during the service call.
*
* @return A {@link Response} that contains a {@link ExtractKeyPhrasesResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<ExtractKeyPhrasesResultCollection> extractKeyPhrasesBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options, Context context) {
inputDocumentsValidation(documents);
return client.extractKeyPhraseAsyncClient.extractKeyPhrasesBatchWithContext(documents, options, context)
.block();
}
// Sentiment
/**
* Returns a sentiment prediction, as well as confidence scores for each sentiment label
* (Positive, Negative, and Neutral) for the document and each sentence within i
*
* This method will use the default language that sets up in
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiments of documents</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.analyzeSentiment#String}
*
* @param document The document to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
*
* @return A {@link DocumentSentiment analyzed document sentiment} of the document.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public DocumentSentiment analyzeSentiment(String document) {
return analyzeSentiment(document, client.getDefaultLanguage());
}
/**
* Returns a sentiment prediction, as well as confidence scores for each sentiment label
* (Positive, Negative, and Neutral) for the document and each sentence within i
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiments in a document with a provided language representation.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.analyzeSentiment#String-String}
*
* @param document The document to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language for the document. If not set, uses "en" for
* English as default.
*
* @return A {@link DocumentSentiment analyzed document sentiment} of the document.
*
* @throws NullPointerException if {@code document} is {@code null}.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public DocumentSentiment analyzeSentiment(String document, String language) {
Objects.requireNonNull(document, "'document' cannot be null.");
return client.analyzeSentiment(document, language).block();
}
/**
* Returns a sentiment prediction, as well as confidence scores for each sentiment label
* (Positive, Negative, and Neutral) for the document and each sentence within it.
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiments in a list of documents with a provided language representation and request options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.analyzeSentimentBatch#Iterable-String-TextAnalyticsRequestOptions}
*
* @param documents A list of documents to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param language The 2 letter ISO 639-1 representation of language for the documents. If not set, uses "en" for
* English as default.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
*
* @return A {@link AnalyzeSentimentResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public AnalyzeSentimentResultCollection analyzeSentimentBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
inputDocumentsValidation(documents);
return client.analyzeSentimentBatch(documents, language, options).block();
}
/**
* Returns a sentiment prediction, as well as confidence scores for each sentiment label
* (Positive, Negative, and Neutral) for the document and each sentence within it.
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiments with http response in a list of {@link TextDocumentInput documents} with request
* options.</p>
* {@codesnippet com.azure.ai.textanalytics.TextAnalyticsClient.analyzeSentimentBatch#Iterable-TextAnalyticsRequestOptions-Context}
*
* @param documents A list of {@link TextDocumentInput documents} to be analyzed.
* For text length limits, maximum batch size, and supported text encoding, see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview#data-limits">data limits</a>.
* @param options The {@link TextAnalyticsRequestOptions options} to configure the scoring model for documents
* and show statistics.
* @param context Additional context that is passed through the Http pipeline during the service call.
*
* @return A {@link Response} that contains a {@link AnalyzeSentimentResultCollection}.
*
* @throws NullPointerException if {@code documents} is {@code null}.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<AnalyzeSentimentResultCollection> analyzeSentimentBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options, Context context) {
inputDocumentsValidation(documents);
return client.analyzeSentimentAsyncClient.analyzeSentimentBatchWithContext(documents, options, context).block();
}
}