TextAnalyticsAsyncClient.java
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.textanalytics;
import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImpl;
import com.azure.ai.textanalytics.models.AnalyzeActionsOperationDetail;
import com.azure.ai.textanalytics.models.AnalyzeActionsOptions;
import com.azure.ai.textanalytics.models.AnalyzeHealthcareEntitiesOperationDetail;
import com.azure.ai.textanalytics.models.AnalyzeHealthcareEntitiesOptions;
import com.azure.ai.textanalytics.models.AnalyzeSentimentOptions;
import com.azure.ai.textanalytics.models.AnalyzeSentimentResult;
import com.azure.ai.textanalytics.models.CategorizedEntityCollection;
import com.azure.ai.textanalytics.models.DetectLanguageInput;
import com.azure.ai.textanalytics.models.DetectLanguageResult;
import com.azure.ai.textanalytics.models.DetectedLanguage;
import com.azure.ai.textanalytics.models.DocumentSentiment;
import com.azure.ai.textanalytics.models.KeyPhrasesCollection;
import com.azure.ai.textanalytics.models.LinkedEntityCollection;
import com.azure.ai.textanalytics.models.PiiEntityCollection;
import com.azure.ai.textanalytics.models.RecognizePiiEntitiesOptions;
import com.azure.ai.textanalytics.models.TextAnalyticsActions;
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.ai.textanalytics.util.AnalyzeActionsResultPagedFlux;
import com.azure.ai.textanalytics.util.AnalyzeHealthcareEntitiesResultCollection;
import com.azure.ai.textanalytics.util.AnalyzeSentimentResultCollection;
import com.azure.ai.textanalytics.util.DetectLanguageResultCollection;
import com.azure.ai.textanalytics.util.ExtractKeyPhrasesResultCollection;
import com.azure.ai.textanalytics.util.AnalyzeHealthcareEntitiesPagedFlux;
import com.azure.ai.textanalytics.util.RecognizeEntitiesResultCollection;
import com.azure.ai.textanalytics.util.RecognizeLinkedEntitiesResultCollection;
import com.azure.ai.textanalytics.util.RecognizePiiEntitiesResultCollection;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.polling.PollerFlux;
import reactor.core.publisher.Mono;
import java.util.Collections;
import java.util.Objects;
import static com.azure.ai.textanalytics.implementation.Utility.inputDocumentsValidation;
import static com.azure.ai.textanalytics.implementation.Utility.mapByIndex;
import static com.azure.ai.textanalytics.implementation.Utility.toTextAnalyticsException;
import static com.azure.core.util.FluxUtil.monoError;
/**
* This class provides an asynchronous 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 an asynchronous Text Analytics Client</strong></p>
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.instantiation -->
* <pre>
* TextAnalyticsAsyncClient textAnalyticsAsyncClient = new TextAnalyticsClientBuilder()
* .credential(new AzureKeyCredential("{key}"))
* .endpoint("{endpoint}")
* .buildAsyncClient();
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.instantiation -->
*
* <p>View {@link TextAnalyticsClientBuilder} for additional ways to construct the client.</p>
*
* @see TextAnalyticsClientBuilder
*/
@ServiceClient(builder = TextAnalyticsClientBuilder.class, isAsync = true)
public final class TextAnalyticsAsyncClient {
private final ClientLogger logger = new ClientLogger(TextAnalyticsAsyncClient.class);
private final TextAnalyticsClientImpl service;
private final TextAnalyticsServiceVersion serviceVersion;
private final String defaultCountryHint;
private final String defaultLanguage;
// Please see <a href=https://docs.microsoft.com/azure/azure-resource-manager/management/azure-services-resource-providers>here</a>
// for more information on Azure resource provider namespaces.
static final String COGNITIVE_TRACING_NAMESPACE_VALUE = "Microsoft.CognitiveServices";
final DetectLanguageAsyncClient detectLanguageAsyncClient;
final AnalyzeSentimentAsyncClient analyzeSentimentAsyncClient;
final ExtractKeyPhraseAsyncClient extractKeyPhraseAsyncClient;
final RecognizeEntityAsyncClient recognizeEntityAsyncClient;
final RecognizePiiEntityAsyncClient recognizePiiEntityAsyncClient;
final RecognizeLinkedEntityAsyncClient recognizeLinkedEntityAsyncClient;
final AnalyzeHealthcareEntityAsyncClient analyzeHealthcareEntityAsyncClient;
final AnalyzeActionsAsyncClient analyzeActionsAsyncClient;
/**
* Creates a {@link TextAnalyticsAsyncClient} that sends requests to the Text Analytics service's endpoint. Each
* service call goes through the {@link TextAnalyticsClientBuilder#pipeline http pipeline}.
*
* @param service The proxy service used to perform REST calls.
* @param serviceVersion The versions of Azure Text Analytics supported by this client library.
* @param defaultCountryHint The default country hint.
* @param defaultLanguage The default language.
*/
TextAnalyticsAsyncClient(TextAnalyticsClientImpl service, TextAnalyticsServiceVersion serviceVersion,
String defaultCountryHint, String defaultLanguage) {
this.service = service;
this.serviceVersion = serviceVersion;
this.defaultCountryHint = defaultCountryHint;
this.defaultLanguage = defaultLanguage;
this.detectLanguageAsyncClient = new DetectLanguageAsyncClient(service);
this.analyzeSentimentAsyncClient = new AnalyzeSentimentAsyncClient(service);
this.extractKeyPhraseAsyncClient = new ExtractKeyPhraseAsyncClient(service);
this.recognizeEntityAsyncClient = new RecognizeEntityAsyncClient(service);
this.recognizePiiEntityAsyncClient = new RecognizePiiEntityAsyncClient(service);
this.recognizeLinkedEntityAsyncClient = new RecognizeLinkedEntityAsyncClient(service);
this.analyzeHealthcareEntityAsyncClient = new AnalyzeHealthcareEntityAsyncClient(service);
this.analyzeActionsAsyncClient = new AnalyzeActionsAsyncClient(service);
}
/**
* Gets default country hint code.
*
* @return The default country hint code
*/
public String getDefaultCountryHint() {
return defaultCountryHint;
}
/**
* Gets default language when the builder is setup.
*
* @return The default language
*/
public String getDefaultLanguage() {
return defaultLanguage;
}
/**
* 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 language in a document. Subscribes to the call asynchronously and prints out the detected language
* details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.detectLanguage#string -->
* <pre>
* String document = "Bonjour tout le monde";
* textAnalyticsAsyncClient.detectLanguage(document).subscribe(detectedLanguage ->
* System.out.printf("Detected language name: %s, ISO 6391 Name: %s, confidence score: %f.%n",
* detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getConfidenceScore()));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 A {@link Mono} containing the {@link DetectedLanguage detected language} of the document.
*
* @throws NullPointerException if the document is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DetectedLanguage> detectLanguage(String document) {
return detectLanguage(document, defaultCountryHint);
}
/**
* Returns a {@link Response} contains 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 language with http response in a document with a provided country hint. Subscribes to the call
* asynchronously and prints out the detected language details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.detectLanguage#string-string -->
* <pre>
* String document = "This text is in English";
* String countryHint = "US";
* textAnalyticsAsyncClient.detectLanguage(document, countryHint).subscribe(detectedLanguage ->
* System.out.printf("Detected language name: %s, ISO 6391 Name: %s, confidence score: %f.%n",
* detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getConfidenceScore()));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 2-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 A {@link Mono} contains a {@link DetectedLanguage detected language} of the document.
*
* @throws NullPointerException if the document is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DetectedLanguage> detectLanguage(String document, String countryHint) {
try {
Objects.requireNonNull(document, "'document' cannot be null.");
return detectLanguageBatch(Collections.singletonList(document), countryHint, null)
.map(detectLanguageResultCollection -> {
DetectedLanguage detectedLanguage = null;
for (DetectLanguageResult detectLanguageResult : detectLanguageResultCollection) {
if (detectLanguageResult.isError()) {
throw logger.logExceptionAsError(toTextAnalyticsException(detectLanguageResult.getError()));
}
detectedLanguage = detectLanguageResult.getPrimaryLanguage();
}
// When the detected language result collection is empty,
// return empty result for the empty collection returned by the service.
return detectedLanguage;
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Returns the detected language for each of documents with the provided country hint and request option.
*
* <p><strong>Code sample</strong></p>
* <p>Detects language in a list of documents with a provided country hint and request option for the batch.
* Subscribes to the call asynchronously and prints out the detected language details when a response is received.
* </p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.detectLanguageBatch#Iterable-String-TextAnalyticsRequestOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "This is written in English",
* "Este es un documento escrito en Español."
* );
* textAnalyticsAsyncClient.detectLanguageBatch(documents, "US", null).subscribe(
* batchResult -> {
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = batchResult.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
* // Batch result of languages
* for (DetectLanguageResult detectLanguageResult : batchResult) {
* DetectedLanguage detectedLanguage = detectLanguageResult.getPrimaryLanguage();
* System.out.printf("Detected language name: %s, ISO 6391 Name: %s, confidence score: %f.%n",
* detectedLanguage.getName(), detectedLanguage.getIso6391Name(),
* detectedLanguage.getConfidenceScore());
* }
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 Mono} contains a {@link DetectLanguageResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DetectLanguageResultCollection> detectLanguageBatch(
Iterable<String> documents, String countryHint, TextAnalyticsRequestOptions options) {
if (countryHint != null && "none".equalsIgnoreCase(countryHint)) {
countryHint = "";
}
final String finalCountryHint = countryHint;
try {
return detectLanguageBatchWithResponse(
mapByIndex(documents, (index, value) -> new DetectLanguageInput(index, value, finalCountryHint)),
options).flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Returns the detected language for a batch of {@link DetectLanguageInput document} with provided request options.
*
* <p><strong>Code sample</strong></p>
* <p>Detects language in a batch of {@link DetectLanguageInput document} with provided request options. Subscribes
* to the call asynchronously and prints out the detected language details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.detectLanguageBatch#Iterable-TextAnalyticsRequestOptions -->
* <pre>
* List<DetectLanguageInput> detectLanguageInputs1 = Arrays.asList(
* new DetectLanguageInput("1", "This is written in English.", "US"),
* new DetectLanguageInput("2", "Este es un documento escrito en Español.", "ES")
* );
*
* TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setIncludeStatistics(true);
*
* textAnalyticsAsyncClient.detectLanguageBatchWithResponse(detectLanguageInputs1, requestOptions)
* .subscribe(response -> {
* // Response's status code
* System.out.printf("Status code of request response: %d%n", response.getStatusCode());
*
* DetectLanguageResultCollection resultCollection = response.getValue();
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = resultCollection.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
* // Batch result of languages
* for (DetectLanguageResult detectLanguageResult : resultCollection) {
* DetectedLanguage detectedLanguage = detectLanguageResult.getPrimaryLanguage();
* System.out.printf("Detected language name: %s, ISO 6391 Name: %s, confidence score: %f.%n",
* detectedLanguage.getName(), detectedLanguage.getIso6391Name(),
* detectedLanguage.getConfidenceScore());
* }
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.detectLanguageBatch#Iterable-TextAnalyticsRequestOptions -->
*
* @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.
*
* @return A {@link Mono} contains a {@link Response} which contains a {@link DetectLanguageResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<DetectLanguageResultCollection>> detectLanguageBatchWithResponse(
Iterable<DetectLanguageInput> documents, TextAnalyticsRequestOptions options) {
return detectLanguageAsyncClient.detectLanguageBatch(documents, options);
}
// 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>.
* For a list of enabled languages, check: <a href="https://aka.ms/talangs">this</a>.
*
* This method will use the default language that can be set by using method
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize entities in a document. Subscribes to the call asynchronously and prints out the recognized entity
* details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeEntities#string -->
* <pre>
* String document = "Satya Nadella is the CEO of Microsoft";
* textAnalyticsAsyncClient.recognizeEntities(document)
* .subscribe(entityCollection -> entityCollection.forEach(entity ->
* System.out.printf("Recognized categorized entity: %s, category: %s, confidence score: %f.%n",
* entity.getText(),
* entity.getCategory(),
* entity.getConfidenceScore())));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeEntities#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 Mono} contains a {@link CategorizedEntityCollection recognized categorized entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<CategorizedEntityCollection> recognizeEntities(String document) {
return recognizeEntities(document, defaultLanguage);
}
/**
* 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>.
* For a list of enabled languages, check: <a href="https://aka.ms/talangs">this</a>.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize entities in a document with provided language code. Subscribes to the call asynchronously and prints
* out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeEntities#string-string -->
* <pre>
* String document = "Satya Nadella is the CEO of Microsoft";
* textAnalyticsAsyncClient.recognizeEntities(document, "en")
* .subscribe(entityCollection -> entityCollection.forEach(entity ->
* System.out.printf("Recognized categorized entity: %s, category: %s, confidence score: %f.%n",
* entity.getText(),
* entity.getCategory(),
* entity.getConfidenceScore())));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeEntities#string-string -->
*
* @param document the text 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 A {@link Mono} contains a {@link CategorizedEntityCollection recognized categorized entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<CategorizedEntityCollection> recognizeEntities(String document, String language) {
return recognizeEntityAsyncClient.recognizeEntities(document, language);
}
/**
* Returns a list of general categorized entities for the provided list of documents with the provided language code
* and request options.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize entities in a document with the provided language code. Subscribes to the call asynchronously and
* prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeCategorizedEntitiesBatch#Iterable-String-TextAnalyticsRequestOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "I had a wonderful trip to Seattle last week.", "I work at Microsoft.");
*
* textAnalyticsAsyncClient.recognizeEntitiesBatch(documents, "en", null)
* .subscribe(batchResult -> {
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = batchResult.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
* // Batch Result of entities
* batchResult.forEach(recognizeEntitiesResult ->
* recognizeEntitiesResult.getEntities().forEach(entity -> System.out.printf(
* "Recognized categorized entity: %s, category: %s, confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getConfidenceScore())));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 Mono} contains a {@link RecognizeEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<RecognizeEntitiesResultCollection> recognizeEntitiesBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
try {
return recognizeEntitiesBatchWithResponse(
mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), options).flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* 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>Recognize entities in a list of {@link TextDocumentInput document}. Subscribes to the call asynchronously
* and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeCategorizedEntitiesBatch#Iterable-TextAnalyticsRequestOptions -->
* <pre>
* List<TextDocumentInput> textDocumentInputs1 = Arrays.asList(
* new TextDocumentInput("0", "I had a wonderful trip to Seattle last week.").setLanguage("en"),
* new TextDocumentInput("1", "I work at Microsoft.").setLanguage("en"));
*
* TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setIncludeStatistics(true);
*
* textAnalyticsAsyncClient.recognizeEntitiesBatchWithResponse(textDocumentInputs1, requestOptions)
* .subscribe(response -> {
* // Response's status code
* System.out.printf("Status code of request response: %d%n", response.getStatusCode());
* RecognizeEntitiesResultCollection resultCollection = response.getValue();
*
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = resultCollection.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* resultCollection.forEach(recognizeEntitiesResult ->
* recognizeEntitiesResult.getEntities().forEach(entity -> System.out.printf(
* "Recognized categorized entity: %s, category: %s, confidence score: %f.%n",
* entity.getText(),
* entity.getCategory(),
* entity.getConfidenceScore())));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeCategorizedEntitiesBatch#Iterable-TextAnalyticsRequestOptions -->
*
* @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.
*
* @return A {@link Mono} contains a {@link Response} which contains a {@link RecognizeEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<RecognizeEntitiesResultCollection>> recognizeEntitiesBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options) {
return recognizeEntityAsyncClient.recognizeEntitiesBatch(documents, options);
}
// PII Entity
/**
* Returns a list of Personally Identifiable Information(PII) entities in the provided document.
*
* For a list of supported entity types, check: <a href="https://aka.ms/tanerpii">this</a>.
* For a list of enabled languages, check: <a href="https://aka.ms/talangs">this</a>. This method will use the
* default language that is set using {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is
* specified, service will use 'en' as the language.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize the PII entities details in a document.
* Subscribes to the call asynchronously and prints out the recognized entity details when a response is
* received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntities#string -->
* <pre>
* String document = "My SSN is 859-98-0987";
* textAnalyticsAsyncClient.recognizePiiEntities(document).subscribe(piiEntityCollection -> {
* System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText());
* piiEntityCollection.forEach(entity -> System.out.printf(
* "Recognized Personally Identifiable Information entity: %s, entity category: %s,"
* + " entity subcategory: %s, confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getConfidenceScore()));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntities#string -->
*
* @param document The document to recognize PII entities details 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 Mono} contains a {@link PiiEntityCollection recognized PII entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PiiEntityCollection> recognizePiiEntities(String document) {
return recognizePiiEntities(document, defaultLanguage);
}
/**
* Returns a list of Personally Identifiable Information(PII) entities in the provided document
* with provided language code.
*
* For a list of supported entity types, check: <a href="https://aka.ms/tanerpii">this</a>.
* For a list of enabled languages, check: <a href="https://aka.ms/talangs">this</a>.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize the PII entities details in a document with provided language code.
* Subscribes to the call asynchronously and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntities#string-string -->
* <pre>
* String document = "My SSN is 859-98-0987";
* textAnalyticsAsyncClient.recognizePiiEntities(document, "en")
* .subscribe(piiEntityCollection -> {
* System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText());
* piiEntityCollection.forEach(entity -> System.out.printf(
* "Recognized Personally Identifiable Information entity: %s, entity category: %s,"
* + " entity subcategory: %s, confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getConfidenceScore()));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntities#string-string -->
*
* @param document the text to recognize PII entities details 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 A {@link Mono} contains a {@link PiiEntityCollection recognized PII entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PiiEntityCollection> recognizePiiEntities(String document, String language) {
return recognizePiiEntityAsyncClient.recognizePiiEntities(document, language, null);
}
/**
* Returns a list of Personally Identifiable Information(PII) entities in the provided document
* with provided language code.
*
* For a list of supported entity types, check: <a href="https://aka.ms/tanerpii">this</a>.
* For a list of enabled languages, check: <a href="https://aka.ms/talangs">this</a>.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize the PII entities details in a document with provided language code and
* {@link RecognizePiiEntitiesOptions}.
* Subscribes to the call asynchronously and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntities#string-string-RecognizePiiEntitiesOptions -->
* <pre>
* String document = "My SSN is 859-98-0987";
* textAnalyticsAsyncClient.recognizePiiEntities(document, "en",
* new RecognizePiiEntitiesOptions().setDomainFilter(PiiEntityDomain.PROTECTED_HEALTH_INFORMATION))
* .subscribe(piiEntityCollection -> {
* System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText());
* piiEntityCollection.forEach(entity -> System.out.printf(
* "Recognized Personally Identifiable Information entity: %s, entity category: %s,"
* + " entity subcategory: %s, confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getConfidenceScore()));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntities#string-string-RecognizePiiEntitiesOptions -->
*
* @param document the text to recognize PII entities details 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 additional configurable {@link RecognizePiiEntitiesOptions options} that may be passed when
* recognizing PII entities.
*
* @return A {@link Mono} contains a {@link PiiEntityCollection recognized PII entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PiiEntityCollection> recognizePiiEntities(String document, String language,
RecognizePiiEntitiesOptions options) {
return recognizePiiEntityAsyncClient.recognizePiiEntities(document, language, options);
}
/**
* Returns a list of Personally Identifiable Information(PII) entities for the provided list of documents with
* the provided language code and request options.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize Personally Identifiable Information entities in a document with the provided language code.
* Subscribes to the call asynchronously and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntitiesBatch#Iterable-String-RecognizePiiEntitiesOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "My SSN is 859-98-0987.",
* "Visa card 0111 1111 1111 1111."
* );
*
* // Show statistics and model version
* RecognizePiiEntitiesOptions requestOptions = new RecognizePiiEntitiesOptions().setIncludeStatistics(true)
* .setModelVersion("latest");
*
* textAnalyticsAsyncClient.recognizePiiEntitiesBatch(documents, "en", requestOptions)
* .subscribe(piiEntitiesResults -> {
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = piiEntitiesResults.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* piiEntitiesResults.forEach(recognizePiiEntitiesResult -> {
* PiiEntityCollection piiEntityCollection = recognizePiiEntitiesResult.getEntities();
* System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText());
* piiEntityCollection.forEach(entity -> System.out.printf(
* "Recognized Personally Identifiable Information entity: %s, entity category: %s,"
* + " entity subcategory: %s, confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getConfidenceScore()));
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntitiesBatch#Iterable-String-RecognizePiiEntitiesOptions -->
*
* @param documents A list of documents to recognize PII 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 additional configurable {@link RecognizePiiEntitiesOptions options} that may be passed when
* recognizing PII entities.
*
* @return A {@link Mono} contains a {@link RecognizePiiEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<RecognizePiiEntitiesResultCollection> recognizePiiEntitiesBatch(
Iterable<String> documents, String language, RecognizePiiEntitiesOptions options) {
try {
inputDocumentsValidation(documents);
return recognizePiiEntitiesBatchWithResponse(
mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), options).flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Returns a list of Personally Identifiable Information entities for the provided list of
* {@link TextDocumentInput document} with provided request options.
*
* <p><strong>Code sample</strong></p>
* <p>Recognize the PII entities details with http response in a list of {@link TextDocumentInput document}
* with provided request options.
* Subscribes to the call asynchronously and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntitiesBatch#Iterable-RecognizePiiEntitiesOptions -->
* <pre>
* List<TextDocumentInput> textDocumentInputs1 = Arrays.asList(
* new TextDocumentInput("0", "My SSN is 859-98-0987."),
* new TextDocumentInput("1", "Visa card 0111 1111 1111 1111."));
*
* // Show statistics and model version
* RecognizePiiEntitiesOptions requestOptions = new RecognizePiiEntitiesOptions().setIncludeStatistics(true)
* .setModelVersion("latest");
*
* textAnalyticsAsyncClient.recognizePiiEntitiesBatchWithResponse(textDocumentInputs1, requestOptions)
* .subscribe(response -> {
* RecognizePiiEntitiesResultCollection piiEntitiesResults = response.getValue();
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = piiEntitiesResults.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* piiEntitiesResults.forEach(recognizePiiEntitiesResult -> {
* PiiEntityCollection piiEntityCollection = recognizePiiEntitiesResult.getEntities();
* System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText());
* piiEntityCollection.forEach(entity -> System.out.printf(
* "Recognized Personally Identifiable Information entity: %s, entity category: %s,"
* + " entity subcategory: %s, confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getConfidenceScore()));
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizePiiEntitiesBatch#Iterable-RecognizePiiEntitiesOptions -->
*
* @param documents A list of {@link TextDocumentInput documents} to recognize PII 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 additional configurable {@link RecognizePiiEntitiesOptions options} that may be passed when
* recognizing PII entities.
*
* @return A {@link Mono} contains a {@link Response} which contains a {@link RecognizePiiEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<RecognizePiiEntitiesResultCollection>> recognizePiiEntitiesBatchWithResponse(
Iterable<TextDocumentInput> documents, RecognizePiiEntitiesOptions options) {
return recognizePiiEntityAsyncClient.recognizePiiEntitiesBatch(documents, options);
}
// 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 can be set by using method
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p>Recognize linked entities in a document. Subscribes to the call asynchronously and prints out the
* entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeLinkedEntities#string -->
* <pre>
* String document = "Old Faithful is a geyser at Yellowstone Park.";
* textAnalyticsAsyncClient.recognizeLinkedEntities(document).subscribe(
* linkedEntityCollection -> linkedEntityCollection.forEach(linkedEntity -> {
* System.out.println("Linked Entities:");
* System.out.printf("Name: %s, entity ID in data source: %s, URL: %s, data source: %s.%n",
* linkedEntity.getName(), linkedEntity.getDataSourceEntityId(), linkedEntity.getUrl(),
* linkedEntity.getDataSource());
* linkedEntity.getMatches().forEach(entityMatch -> System.out.printf(
* "Matched entity: %s, confidence score: %f.%n",
* entityMatch.getText(), entityMatch.getConfidenceScore()));
* }));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 Mono} contains a {@link LinkedEntityCollection recognized linked entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<LinkedEntityCollection> recognizeLinkedEntities(String document) {
return recognizeLinkedEntities(document, defaultLanguage);
}
/**
* 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.
*
* <p>Recognize linked entities in a text with provided language code. Subscribes to the call asynchronously
* and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeLinkedEntities#string-string -->
* <pre>
* String document = "Old Faithful is a geyser at Yellowstone Park.";
* textAnalyticsAsyncClient.recognizeLinkedEntities(document, "en").subscribe(
* linkedEntityCollection -> linkedEntityCollection.forEach(linkedEntity -> {
* System.out.println("Linked Entities:");
* System.out.printf("Name: %s, entity ID in data source: %s, URL: %s, data source: %s.%n",
* linkedEntity.getName(), linkedEntity.getDataSourceEntityId(), linkedEntity.getUrl(),
* linkedEntity.getDataSource());
* linkedEntity.getMatches().forEach(entityMatch -> System.out.printf(
* "Matched entity: %s, confidence score: %f.%n",
* entityMatch.getText(), entityMatch.getConfidenceScore()));
* }));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 Mono} contains a {@link LinkedEntityCollection recognized linked entities collection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<LinkedEntityCollection> recognizeLinkedEntities(String document, String language) {
return recognizeLinkedEntityAsyncClient.recognizeLinkedEntities(document, language);
}
/**
* 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>Recognize linked entities in a list of documents with provided language code. Subscribes to the call
* asynchronously and prints out the entity details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeLinkedEntitiesBatch#Iterable-String-TextAnalyticsRequestOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "Old Faithful is a geyser at Yellowstone Park.",
* "Mount Shasta has lenticular clouds."
* );
*
* textAnalyticsAsyncClient.recognizeLinkedEntitiesBatch(documents, "en", null)
* .subscribe(batchResult -> {
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = batchResult.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* batchResult.forEach(recognizeLinkedEntitiesResult ->
* recognizeLinkedEntitiesResult.getEntities().forEach(linkedEntity -> {
* System.out.println("Linked Entities:");
* System.out.printf("Name: %s, entity ID in data source: %s, URL: %s, data source: %s.%n",
* linkedEntity.getName(), linkedEntity.getDataSourceEntityId(), linkedEntity.getUrl(),
* linkedEntity.getDataSource());
* linkedEntity.getMatches().forEach(entityMatch -> System.out.printf(
* "Matched entity: %s, confidence score: %f.%n",
* entityMatch.getText(), entityMatch.getConfidenceScore()));
* }));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 text. 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 Mono} contains a {@link RecognizeLinkedEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<RecognizeLinkedEntitiesResultCollection> recognizeLinkedEntitiesBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
try {
return recognizeLinkedEntitiesBatchWithResponse(mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), options).flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Returns a list of recognized entities with links to a well-known knowledge base for the list of
* {@link TextDocumentInput document} with provided request options.
*
* See <a href="https://aka.ms/talangs">this</a> supported languages in Text Analytics API.
*
* <p>Recognize linked entities in a list of {@link TextDocumentInput document} and provided request options to
* show statistics. Subscribes to the call asynchronously and prints out the entity details when a response is
* received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeLinkedEntitiesBatch#Iterable-TextAnalyticsRequestOptions -->
* <pre>
* List<TextDocumentInput> textDocumentInputs1 = Arrays.asList(
* new TextDocumentInput("0", "Old Faithful is a geyser at Yellowstone Park.").setLanguage("en"),
* new TextDocumentInput("1", "Mount Shasta has lenticular clouds.").setLanguage("en"));
*
* TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setIncludeStatistics(true);
*
* textAnalyticsAsyncClient.recognizeLinkedEntitiesBatchWithResponse(textDocumentInputs1, requestOptions)
* .subscribe(response -> {
* // Response's status code
* System.out.printf("Status code of request response: %d%n", response.getStatusCode());
* RecognizeLinkedEntitiesResultCollection resultCollection = response.getValue();
*
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = resultCollection.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* resultCollection.forEach(recognizeLinkedEntitiesResult ->
* recognizeLinkedEntitiesResult.getEntities().forEach(linkedEntity -> {
* System.out.println("Linked Entities:");
* System.out.printf("Name: %s, entity ID in data source: %s, URL: %s, data source: %s.%n",
* linkedEntity.getName(), linkedEntity.getDataSourceEntityId(), linkedEntity.getUrl(),
* linkedEntity.getDataSource());
* linkedEntity.getMatches().forEach(entityMatch -> System.out.printf(
* "Matched entity: %s, confidence score: %.2f.%n",
* entityMatch.getText(), entityMatch.getConfidenceScore()));
* }));
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.recognizeLinkedEntitiesBatch#Iterable-TextAnalyticsRequestOptions -->
*
* @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.
*
* @return A {@link Mono} contains a {@link Response} which contains a
* {@link RecognizeLinkedEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<RecognizeLinkedEntitiesResultCollection>> recognizeLinkedEntitiesBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options) {
return recognizeLinkedEntityAsyncClient.recognizeLinkedEntitiesBatch(documents, options);
}
// Key Phrases
/**
* Returns a list of strings denoting the key phrases in the document.
*
* This method will use the default language that can be set by using method
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p>Extract key phrases in a document. Subscribes to the call asynchronously and prints out the
* key phrases when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.extractKeyPhrases#string -->
* <pre>
* System.out.println("Extracted phrases:");
* textAnalyticsAsyncClient.extractKeyPhrases("Bonjour tout le monde").subscribe(keyPhrase ->
* System.out.printf("%s.%n", keyPhrase));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 Mono} contains a {@link KeyPhrasesCollection}.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<KeyPhrasesCollection> extractKeyPhrases(String document) {
return extractKeyPhrases(document, defaultLanguage);
}
/**
* 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>Extract key phrases in a document with a provided language code. Subscribes to the call asynchronously and
* prints out the key phrases when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.extractKeyPhrases#string-string -->
* <pre>
* System.out.println("Extracted phrases:");
* textAnalyticsAsyncClient.extractKeyPhrases("Bonjour tout le monde", "fr")
* .subscribe(keyPhrase -> System.out.printf("%s.%n", keyPhrase));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.extractKeyPhrases#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 text. If not set, uses "en" for
* English as default.
*
* @return A {@link Mono} contains a {@link KeyPhrasesCollection}
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<KeyPhrasesCollection> extractKeyPhrases(String document, String language) {
return extractKeyPhraseAsyncClient.extractKeyPhrasesSingleText(document, language);
}
/**
* Returns a list of strings denoting the key phrases in the document with provided language code and request
* options.
*
* See <a href="https://aka.ms/talangs">this</a> for the list of enabled languages.
*
* <p>Extract key phrases in a list of documents with a provided language and request options. Subscribes to the
* call asynchronously and prints out the key phrases when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.extractKeyPhrasesBatch#Iterable-String-TextAnalyticsRequestOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "Hello world. This is some input text that I love.",
* "Bonjour tout le monde");
*
* textAnalyticsAsyncClient.extractKeyPhrasesBatch(documents, "en", null).subscribe(
* extractKeyPhraseResults -> {
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = extractKeyPhraseResults.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* extractKeyPhraseResults.forEach(extractKeyPhraseResult -> {
* System.out.println("Extracted phrases:");
* extractKeyPhraseResult.getKeyPhrases().forEach(keyPhrase -> System.out.printf("%s.%n", keyPhrase));
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 text. 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 Mono} contains a {@link ExtractKeyPhrasesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<ExtractKeyPhrasesResultCollection> extractKeyPhrasesBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
try {
return extractKeyPhrasesBatchWithResponse(
mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), options).flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* Returns a list of strings denoting the key phrases in the document with provided request options.
*
* See <a href="https://aka.ms/talangs">this</a> for the list of enabled languages.
*
* <p>Extract key phrases in a list of {@link TextDocumentInput document} with provided request options.
* Subscribes to the call asynchronously and prints out the key phrases when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.extractKeyPhrasesBatch#Iterable-TextAnalyticsRequestOptions -->
* <pre>
* List<TextDocumentInput> textDocumentInputs1 = Arrays.asList(
* new TextDocumentInput("0", "I had a wonderful trip to Seattle last week.").setLanguage("en"),
* new TextDocumentInput("1", "I work at Microsoft.").setLanguage("en"));
*
* TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setIncludeStatistics(true);
*
* textAnalyticsAsyncClient.extractKeyPhrasesBatchWithResponse(textDocumentInputs1, requestOptions)
* .subscribe(response -> {
* // Response's status code
* System.out.printf("Status code of request response: %d%n", response.getStatusCode());
* ExtractKeyPhrasesResultCollection resultCollection = response.getValue();
*
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = resultCollection.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* for (ExtractKeyPhraseResult extractKeyPhraseResult : resultCollection) {
* System.out.println("Extracted phrases:");
* for (String keyPhrase : extractKeyPhraseResult.getKeyPhrases()) {
* System.out.printf("%s.%n", keyPhrase);
* }
* }
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.extractKeyPhrasesBatch#Iterable-TextAnalyticsRequestOptions -->
*
* @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.
*
* @return A {@link Mono} contains a {@link Response} that contains a {@link ExtractKeyPhrasesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<ExtractKeyPhrasesResultCollection>> extractKeyPhrasesBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options) {
return extractKeyPhraseAsyncClient.extractKeyPhrasesWithResponse(documents, options);
}
// 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 it.
*
* This method will use the default language that can be set by using method
* {@link TextAnalyticsClientBuilder#defaultLanguage(String)}. If none is specified, service will use 'en' as
* the language.
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiment in a document. Subscribes to the call asynchronously and prints out the
* sentiment details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentiment#string -->
* <pre>
* String document = "The hotel was dark and unclean.";
* textAnalyticsAsyncClient.analyzeSentiment(document).subscribe(documentSentiment -> {
* System.out.printf("Recognized document sentiment: %s.%n", documentSentiment.getSentiment());
*
* for (SentenceSentiment sentenceSentiment : documentSentiment.getSentences()) {
* System.out.printf(
* "Recognized sentence sentiment: %s, positive score: %.2f, neutral score: %.2f, "
* + "negative score: %.2f.%n",
* sentenceSentiment.getSentiment(),
* sentenceSentiment.getConfidenceScores().getPositive(),
* sentenceSentiment.getConfidenceScores().getNeutral(),
* sentenceSentiment.getConfidenceScores().getNegative());
* }
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 Mono} contains the {@link DocumentSentiment analyzed document sentiment} of the document.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentSentiment> analyzeSentiment(String document) {
return analyzeSentiment(document, defaultLanguage);
}
/**
* 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 document with a provided language representation. Subscribes to the call
* asynchronously and prints out the sentiment details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentiment#String-String -->
* <pre>
* String document = "The hotel was dark and unclean.";
* textAnalyticsAsyncClient.analyzeSentiment(document, "en")
* .subscribe(documentSentiment -> {
* System.out.printf("Recognized sentiment label: %s.%n", documentSentiment.getSentiment());
* for (SentenceSentiment sentenceSentiment : documentSentiment.getSentences()) {
* System.out.printf("Recognized sentence sentiment: %s, positive score: %.2f, neutral score: %.2f, "
* + "negative score: %.2f.%n",
* sentenceSentiment.getSentiment(),
* sentenceSentiment.getConfidenceScores().getPositive(),
* sentenceSentiment.getConfidenceScores().getNeutral(),
* sentenceSentiment.getConfidenceScores().getNegative());
* }
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 text. If not set, uses "en" for
* English as default.
*
* @return A {@link Mono} contains the {@link DocumentSentiment analyzed document sentiment} of the document.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentSentiment> analyzeSentiment(String document, String language) {
return analyzeSentiment(document, language, null);
}
/**
* 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. If the {@code includeOpinionMining} of
* {@link AnalyzeSentimentOptions} set to true, the output will include the opinion mining results. It mines the
* opinions of a sentence and conducts more granular analysis around the aspects in the text
* (also known as aspect-based sentiment analysis).
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiment and mine the opinions for each sentence in a document with a provided language
* representation and {@link AnalyzeSentimentOptions} options. Subscribes to the call asynchronously and prints
* out the sentiment and sentence opinions details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentiment#String-String-AnalyzeSentimentOptions -->
* <pre>
* textAnalyticsAsyncClient.analyzeSentiment("The hotel was dark and unclean.", "en",
* new AnalyzeSentimentOptions().setIncludeOpinionMining(true))
* .subscribe(documentSentiment -> {
* for (SentenceSentiment sentenceSentiment : documentSentiment.getSentences()) {
* System.out.printf("\tSentence sentiment: %s%n", sentenceSentiment.getSentiment());
* sentenceSentiment.getOpinions().forEach(opinion -> {
* TargetSentiment targetSentiment = opinion.getTarget();
* System.out.printf("\tTarget sentiment: %s, target text: %s%n",
* targetSentiment.getSentiment(), targetSentiment.getText());
* for (AssessmentSentiment assessmentSentiment : opinion.getAssessments()) {
* System.out.printf("\t\t'%s' sentiment because of \"%s\". Is the assessment negated: %s.%n",
* assessmentSentiment.getSentiment(), assessmentSentiment.getText(),
* assessmentSentiment.isNegated());
* }
* });
* }
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentiment#String-String-AnalyzeSentimentOptions -->
*
* @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 text. If not set, uses "en" for
* English as default.
* @param options The additional configurable {@link AnalyzeSentimentOptions options} that may be passed when
* analyzing sentiments.
*
* @return A {@link Mono} contains the {@link DocumentSentiment analyzed document sentiment} of the document.
*
* @throws NullPointerException if {@code document} is null.
* @throws TextAnalyticsException if the response returned with an {@link TextAnalyticsError error}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<DocumentSentiment> analyzeSentiment(String document, String language, AnalyzeSentimentOptions options) {
try {
Objects.requireNonNull(document, "'document' cannot be null.");
return analyzeSentimentBatch(Collections.singletonList(document), language, options)
.map(sentimentResultCollection -> {
DocumentSentiment documentSentiment = null;
for (AnalyzeSentimentResult sentimentResult : sentimentResultCollection) {
if (sentimentResult.isError()) {
throw logger.logExceptionAsError(toTextAnalyticsException(sentimentResult.getError()));
}
documentSentiment = sentimentResult.getDocumentSentiment();
}
// When the sentiment result collection is empty,
// return empty result for the empty collection returned by the service.
return documentSentiment;
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* 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>Analyze sentiment in a list of documents with provided language code and request options. Subscribes to the
* call asynchronously and prints out the sentiment details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentimentBatch#Iterable-String-TextAnalyticsRequestOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "The hotel was dark and unclean.",
* "The restaurant had amazing gnocchi."
* );
*
* textAnalyticsAsyncClient.analyzeSentimentBatch(documents, "en",
* new TextAnalyticsRequestOptions().setIncludeStatistics(true)).subscribe(
* response -> {
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = response.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount());
*
* response.forEach(analyzeSentimentResult -> {
* System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId());
* DocumentSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment();
* System.out.printf("Recognized document sentiment: %s.%n", documentSentiment.getSentiment());
* documentSentiment.getSentences().forEach(sentenceSentiment ->
* System.out.printf("Recognized sentence sentiment: %s, positive score: %.2f, "
* + "neutral score: %.2f, negative score: %.2f.%n",
* sentenceSentiment.getSentiment(),
* sentenceSentiment.getConfidenceScores().getPositive(),
* sentenceSentiment.getConfidenceScores().getNeutral(),
* sentenceSentiment.getConfidenceScores().getNegative()));
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.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 document. 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 Mono} contains a {@link AnalyzeSentimentResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
*
* @deprecated Please use the {@link #analyzeSentimentBatch(Iterable, String, AnalyzeSentimentOptions)}.
*/
@Deprecated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<AnalyzeSentimentResultCollection> analyzeSentimentBatch(
Iterable<String> documents, String language, TextAnalyticsRequestOptions options) {
return analyzeSentimentBatch(documents, language, new AnalyzeSentimentOptions()
.setIncludeStatistics(options == null ? false : options.isIncludeStatistics())
.setModelVersion(options == null ? null : options.getModelVersion()));
}
/**
* 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. If the {@code includeOpinionMining} of
* {@link AnalyzeSentimentOptions} set to true, the output will include the opinion mining results. It mines the
* opinions of a sentence and conducts more granular analysis around the aspects in the text
* (also known as aspect-based sentiment analysis).
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze the sentiments and mine the opinions for each sentence in a list of documents with a provided language
* representation and {@link AnalyzeSentimentOptions} options. Subscribes to the call asynchronously and prints out
* the sentiment and sentence opinions details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsClient.beginAnalyzeActions#Iterable-TextAnalyticsActions-AnalyzeActionsOptions-Context -->
* <pre>
* List<TextDocumentInput> documents = Arrays.asList(
* new TextDocumentInput("0", "Elon Musk is the CEO of SpaceX and Tesla.").setLanguage("en"),
* new TextDocumentInput("1", "My SSN is 859-98-0987").setLanguage("en")
* );
*
* SyncPoller<AnalyzeActionsOperationDetail, AnalyzeActionsResultPagedIterable> syncPoller =
* textAnalyticsClient.beginAnalyzeActions(
* documents,
* new TextAnalyticsActions().setDisplayName("{tasks_display_name}")
* .setRecognizeEntitiesActions(new RecognizeEntitiesAction())
* .setExtractKeyPhrasesActions(new ExtractKeyPhrasesAction()),
* new AnalyzeActionsOptions().setIncludeStatistics(false),
* Context.NONE);
* syncPoller.waitForCompletion();
* AnalyzeActionsResultPagedIterable result = syncPoller.getFinalResult();
* result.forEach(analyzeActionsResult -> {
* System.out.println("Entities recognition action results:");
* analyzeActionsResult.getRecognizeEntitiesResults().forEach(
* actionResult -> {
* if (!actionResult.isError()) {
* actionResult.getDocumentsResults().forEach(
* entitiesResult -> entitiesResult.getEntities().forEach(
* entity -> System.out.printf(
* "Recognized entity: %s, entity category: %s, entity subcategory: %s,"
* + " confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(),
* entity.getConfidenceScore())));
* }
* });
* System.out.println("Key phrases extraction action results:");
* analyzeActionsResult.getExtractKeyPhrasesResults().forEach(
* actionResult -> {
* if (!actionResult.isError()) {
* actionResult.getDocumentsResults().forEach(extractKeyPhraseResult -> {
* System.out.println("Extracted phrases:");
* extractKeyPhraseResult.getKeyPhrases()
* .forEach(keyPhrases -> System.out.printf("\t%s.%n", keyPhrases));
* });
* }
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsClient.beginAnalyzeActions#Iterable-TextAnalyticsActions-AnalyzeActionsOptions-Context -->
*
* @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 document. If not set, uses "en" for
* English as default.
* @param options The additional configurable {@link AnalyzeSentimentOptions options} that may be passed when
* analyzing sentiments.
*
* @return A {@link Mono} contains a {@link AnalyzeSentimentResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<AnalyzeSentimentResultCollection> analyzeSentimentBatch(Iterable<String> documents,
String language, AnalyzeSentimentOptions options) {
try {
return analyzeSentimentBatchWithResponse(
mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), options).flatMap(FluxUtil::toMono);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
/**
* 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>Analyze sentiment in a list of {@link TextDocumentInput document} with provided request options. Subscribes
* to the call asynchronously and prints out the sentiment details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentimentBatch#Iterable-TextAnalyticsRequestOptions -->
* <pre>
* List<TextDocumentInput> textDocumentInputs1 = Arrays.asList(
* new TextDocumentInput("0", "The hotel was dark and unclean.").setLanguage("en"),
* new TextDocumentInput("1", "The restaurant had amazing gnocchi.").setLanguage("en"));
*
* TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setIncludeStatistics(true);
*
* textAnalyticsAsyncClient.analyzeSentimentBatchWithResponse(textDocumentInputs1, requestOptions)
* .subscribe(response -> {
* // Response's status code
* System.out.printf("Status code of request response: %d%n", response.getStatusCode());
* AnalyzeSentimentResultCollection resultCollection = response.getValue();
*
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = resultCollection.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(),
* batchStatistics.getValidDocumentCount());
*
* resultCollection.forEach(analyzeSentimentResult -> {
* System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId());
* DocumentSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment();
* System.out.printf("Recognized document sentiment: %s.%n", documentSentiment.getSentiment());
* documentSentiment.getSentences().forEach(sentenceSentiment ->
* System.out.printf("Recognized sentence sentiment: %s, positive score: %.2f, "
* + "neutral score: %.2f, negative score: %.2f.%n",
* sentenceSentiment.getSentiment(),
* sentenceSentiment.getConfidenceScores().getPositive(),
* sentenceSentiment.getConfidenceScores().getNeutral(),
* sentenceSentiment.getConfidenceScores().getNegative()));
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentimentBatch#Iterable-TextAnalyticsRequestOptions -->
*
* @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.
*
* @return A {@link Mono} contains a {@link Response} that contains a {@link AnalyzeSentimentResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
*
* @deprecated Please use the {@link #analyzeSentimentBatchWithResponse(Iterable, AnalyzeSentimentOptions)}.
*/
@Deprecated
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<AnalyzeSentimentResultCollection>> analyzeSentimentBatchWithResponse(
Iterable<TextDocumentInput> documents, TextAnalyticsRequestOptions options) {
return analyzeSentimentAsyncClient.analyzeSentimentBatch(documents, new AnalyzeSentimentOptions()
.setIncludeStatistics(options == null ? false : options.isIncludeStatistics())
.setModelVersion(options == null ? null : options.getModelVersion()));
}
/**
* 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. If the {@code includeOpinionMining} of
* {@link AnalyzeSentimentOptions} set to true, the output will include the opinion mining results. It mines the
* opinions of a sentence and conducts more granular analysis around the aspects in the text
* (also known as aspect-based sentiment analysis).
*
* <p><strong>Code Sample</strong></p>
* <p>Analyze sentiment and mine the opinions for each sentence in a list of
* {@link TextDocumentInput document} with provided {@link AnalyzeSentimentOptions} options. Subscribes to the call
* asynchronously and prints out the sentiment and sentence opinions details when a response is received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentimentBatch#Iterable-AnalyzeSentimentOptions -->
* <pre>
* List<TextDocumentInput> textDocumentInputs1 = Arrays.asList(
* new TextDocumentInput("0", "The hotel was dark and unclean.").setLanguage("en"),
* new TextDocumentInput("1", "The restaurant had amazing gnocchi.").setLanguage("en"));
*
* AnalyzeSentimentOptions options = new AnalyzeSentimentOptions()
* .setIncludeOpinionMining(true).setIncludeStatistics(true);
* textAnalyticsAsyncClient.analyzeSentimentBatchWithResponse(textDocumentInputs1, options)
* .subscribe(response -> {
* // Response's status code
* System.out.printf("Status code of request response: %d%n", response.getStatusCode());
* AnalyzeSentimentResultCollection resultCollection = response.getValue();
*
* // Batch statistics
* TextDocumentBatchStatistics batchStatistics = resultCollection.getStatistics();
* System.out.printf("Batch statistics, transaction count: %s, valid document count: %s.%n",
* batchStatistics.getTransactionCount(),
* batchStatistics.getValidDocumentCount());
*
* resultCollection.forEach(analyzeSentimentResult -> {
* System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId());
* DocumentSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment();
* documentSentiment.getSentences().forEach(sentenceSentiment -> {
* System.out.printf("\tSentence sentiment: %s%n", sentenceSentiment.getSentiment());
* sentenceSentiment.getOpinions().forEach(opinion -> {
* TargetSentiment targetSentiment = opinion.getTarget();
* System.out.printf("\t\tTarget sentiment: %s, target text: %s%n",
* targetSentiment.getSentiment(), targetSentiment.getText());
* for (AssessmentSentiment assessmentSentiment : opinion.getAssessments()) {
* System.out.printf(
* "\t\t\t'%s' assessment sentiment because of \"%s\". Is the assessment negated: %s.%n",
* assessmentSentiment.getSentiment(), assessmentSentiment.getText(),
* assessmentSentiment.isNegated());
* }
* });
* });
* });
* });
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.analyzeSentimentBatch#Iterable-AnalyzeSentimentOptions -->
*
* @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 additional configurable {@link AnalyzeSentimentOptions options} that may be passed when
* analyzing sentiments.
*
* @return A {@link Mono} contains a {@link Response} that contains a {@link AnalyzeSentimentResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<AnalyzeSentimentResultCollection>> analyzeSentimentBatchWithResponse(
Iterable<TextDocumentInput> documents, AnalyzeSentimentOptions options) {
return analyzeSentimentAsyncClient.analyzeSentimentBatch(documents, options);
}
/**
* Analyze healthcare entities, entity data sources, and entity relations in a list of
* {@link String documents} with provided request options.
*
* Note: In order to use this functionality, request to access public preview is required.
* Azure Active Directory (AAD) is not currently supported. For more information see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/how-tos/text-analytics-for-health?tabs=ner#request-access-to-the-public-preview">this</a>.
*
* See <a href="https://aka.ms/talangs">this</a> supported languages in Text Analytics API.
*
* @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 additional configurable {@link AnalyzeHealthcareEntitiesOptions options} that may be passed
* when analyzing healthcare entities.
*
* @return A {@link PollerFlux} that polls the analyze healthcare operation until it has completed, has failed,
* or has been cancelled. The completed operation returns a {@link PagedFlux} of
* {@link AnalyzeHealthcareEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
* @throws TextAnalyticsException If analyze operation fails.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PollerFlux<AnalyzeHealthcareEntitiesOperationDetail, AnalyzeHealthcareEntitiesPagedFlux>
beginAnalyzeHealthcareEntities(Iterable<String> documents, String language,
AnalyzeHealthcareEntitiesOptions options) {
return beginAnalyzeHealthcareEntities(
mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), options);
}
/**
* Analyze healthcare entities, entity data sources, and entity relations in a list of
* {@link TextDocumentInput documents} with provided request options.
*
* Note: In order to use this functionality, request to access public preview is required.
* Azure Active Directory (AAD) is not currently supported. For more information see
* <a href="https://docs.microsoft.com/azure/cognitive-services/text-analytics/how-tos/text-analytics-for-health?tabs=ner#request-access-to-the-public-preview">this</a>.
*
* See <a href="https://aka.ms/talangs">this</a> supported languages in Text Analytics API.
*
* <p>Analyze healthcare entities, entity data sources, and entity relations in a list of
* {@link TextDocumentInput document} and provided request options to
* show statistics. Subscribes to the call asynchronously and prints out the entity details when a response is
* received.</p>
*
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.beginAnalyzeHealthcareEntities#Iterable-AnalyzeHealthcareEntitiesOptions -->
* <pre>
* List<TextDocumentInput> documents = new ArrayList<>();
* for (int i = 0; i < 3; i++) {
* documents.add(new TextDocumentInput(Integer.toString(i),
* "The patient is a 54-year-old gentleman with a history of progressive angina "
* + "over the past several months."));
* }
*
* AnalyzeHealthcareEntitiesOptions options = new AnalyzeHealthcareEntitiesOptions()
* .setIncludeStatistics(true);
*
* textAnalyticsAsyncClient.beginAnalyzeHealthcareEntities(documents, options)
* .flatMap(pollResult -> {
* AnalyzeHealthcareEntitiesOperationDetail operationResult = pollResult.getValue();
* System.out.printf("Operation created time: %s, expiration time: %s.%n",
* operationResult.getCreatedAt(), operationResult.getExpiresAt());
* return pollResult.getFinalResult();
* })
* .flatMap(analyzeActionsResultPagedFlux -> analyzeActionsResultPagedFlux.byPage())
* .subscribe(
* pagedResponse -> pagedResponse.getElements().forEach(
* analyzeHealthcareEntitiesResultCollection -> {
* // Model version
* System.out.printf("Results of Azure Text Analytics \"Analyze Healthcare\" Model, version: %s%n",
* analyzeHealthcareEntitiesResultCollection.getModelVersion());
*
* TextDocumentBatchStatistics healthcareTaskStatistics =
* analyzeHealthcareEntitiesResultCollection.getStatistics();
* // Batch statistics
* System.out.printf("Documents statistics: document count = %s, erroneous document count = %s,"
* + " transaction count = %s, valid document count = %s.%n",
* healthcareTaskStatistics.getDocumentCount(),
* healthcareTaskStatistics.getInvalidDocumentCount(),
* healthcareTaskStatistics.getTransactionCount(),
* healthcareTaskStatistics.getValidDocumentCount());
*
* analyzeHealthcareEntitiesResultCollection.forEach(healthcareEntitiesResult -> {
* System.out.println("document id = " + healthcareEntitiesResult.getId());
* System.out.println("Document entities: ");
* AtomicInteger ct = new AtomicInteger();
* healthcareEntitiesResult.getEntities().forEach(healthcareEntity -> {
* System.out.printf(
* "\ti = %d, Text: %s, category: %s, confidence score: %f.%n",
* ct.getAndIncrement(), healthcareEntity.getText(), healthcareEntity.getCategory(),
* healthcareEntity.getConfidenceScore());
*
* IterableStream<EntityDataSource> healthcareEntityDataSources =
* healthcareEntity.getDataSources();
* if (healthcareEntityDataSources != null) {
* healthcareEntityDataSources.forEach(healthcareEntityLink -> System.out.printf(
* "\t\tEntity ID in data source: %s, data source: %s.%n",
* healthcareEntityLink.getEntityId(), healthcareEntityLink.getName()));
* }
* });
* // Healthcare entity relation groups
* healthcareEntitiesResult.getEntityRelations().forEach(entityRelation -> {
* System.out.printf("\tRelation type: %s.%n", entityRelation.getRelationType());
* entityRelation.getRoles().forEach(role -> {
* final HealthcareEntity entity = role.getEntity();
* System.out.printf("\t\tEntity text: %s, category: %s, role: %s.%n",
* entity.getText(), entity.getCategory(), role.getName());
* });
* });
* });
* }));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.beginAnalyzeHealthcareEntities#Iterable-AnalyzeHealthcareEntitiesOptions -->
*
* @param documents A list of {@link TextDocumentInput documents} to be analyzed.
* @param options The additional configurable {@link AnalyzeHealthcareEntitiesOptions options} that may be passed
* when analyzing healthcare entities.
*
* @return A {@link PollerFlux} that polls the analyze healthcare operation until it has completed, has failed,
* or has been cancelled. The completed operation returns a {@link PagedFlux} of
* {@link AnalyzeHealthcareEntitiesResultCollection}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
* @throws TextAnalyticsException If analyze operation fails.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PollerFlux<AnalyzeHealthcareEntitiesOperationDetail, AnalyzeHealthcareEntitiesPagedFlux>
beginAnalyzeHealthcareEntities(Iterable<TextDocumentInput> documents,
AnalyzeHealthcareEntitiesOptions options) {
return analyzeHealthcareEntityAsyncClient.beginAnalyzeHealthcareEntities(documents, options, Context.NONE);
}
/**
* Execute actions, such as, entities recognition, PII entities recognition and key phrases extraction for a list of
* {@link String documents} with provided request options.
*
* See <a href="https://aka.ms/talangs">this</a> supported languages in Text Analytics API.
*
* <p><strong>Code Sample</strong></p>
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.beginAnalyzeActions#Iterable-TextAnalyticsActions-String-AnalyzeActionsOptions -->
* <pre>
* List<String> documents = Arrays.asList(
* "Elon Musk is the CEO of SpaceX and Tesla.",
* "1", "My SSN is 859-98-0987"
* );
* textAnalyticsAsyncClient.beginAnalyzeActions(documents,
* new TextAnalyticsActions().setDisplayName("{tasks_display_name}")
* .setRecognizeEntitiesActions(new RecognizeEntitiesAction())
* .setExtractKeyPhrasesActions(new ExtractKeyPhrasesAction()),
* "en",
* new AnalyzeActionsOptions().setIncludeStatistics(false))
* .flatMap(AsyncPollResponse::getFinalResult)
* .flatMap(analyzeActionsResultPagedFlux -> analyzeActionsResultPagedFlux.byPage())
* .subscribe(
* pagedResponse -> pagedResponse.getElements().forEach(
* analyzeActionsResult -> {
* analyzeActionsResult.getRecognizeEntitiesResults().forEach(
* actionResult -> {
* if (!actionResult.isError()) {
* actionResult.getDocumentsResults().forEach(
* entitiesResult -> entitiesResult.getEntities().forEach(
* entity -> System.out.printf(
* "Recognized entity: %s, entity category: %s, entity subcategory: %s,"
* + " confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(),
* entity.getConfidenceScore())));
* }
* });
* analyzeActionsResult.getExtractKeyPhrasesResults().forEach(
* actionResult -> {
* if (!actionResult.isError()) {
* actionResult.getDocumentsResults().forEach(extractKeyPhraseResult -> {
* System.out.println("Extracted phrases:");
* extractKeyPhraseResult.getKeyPhrases()
* .forEach(keyPhrases -> System.out.printf("\t%s.%n", keyPhrases));
* });
* }
* });
* }));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.beginAnalyzeActions#Iterable-TextAnalyticsActions-String-AnalyzeActionsOptions -->
*
* @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 actions The {@link TextAnalyticsActions actions} that contains all actions to be executed.
* An action is one task of execution, such as a single task of 'Key Phrases Extraction' on the given document
* inputs.
* @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 additional configurable {@link AnalyzeActionsOptions options} that may be passed when
* analyzing a collection of actions.
*
* @return A {@link PollerFlux} that polls the analyze a collection of actions operation until it has completed,
* has failed, or has been cancelled. The completed operation returns a {@link AnalyzeActionsResultPagedFlux}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
* @throws TextAnalyticsException If analyze operation fails.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PollerFlux<AnalyzeActionsOperationDetail, AnalyzeActionsResultPagedFlux> beginAnalyzeActions(
Iterable<String> documents, TextAnalyticsActions actions, String language, AnalyzeActionsOptions options) {
return beginAnalyzeActions(
mapByIndex(documents, (index, value) -> {
final TextDocumentInput textDocumentInput = new TextDocumentInput(index, value);
textDocumentInput.setLanguage(language);
return textDocumentInput;
}), actions, options);
}
/**
* Execute actions, such as, entities recognition, PII entities recognition and key phrases extraction for a list of
* {@link TextDocumentInput documents} with provided request options.
*
* See <a href="https://aka.ms/talangs">this</a> supported languages in Text Analytics API.
*
* <p><strong>Code Sample</strong></p>
* <!-- src_embed com.azure.ai.textanalytics.TextAnalyticsAsyncClient.beginAnalyzeActions#Iterable-TextAnalyticsActions-AnalyzeActionsOptions -->
* <pre>
* List<TextDocumentInput> documents = Arrays.asList(
* new TextDocumentInput("0", "Elon Musk is the CEO of SpaceX and Tesla.").setLanguage("en"),
* new TextDocumentInput("1", "My SSN is 859-98-0987").setLanguage("en")
* );
* textAnalyticsAsyncClient.beginAnalyzeActions(documents,
* new TextAnalyticsActions().setDisplayName("{tasks_display_name}")
* .setRecognizeEntitiesActions(new RecognizeEntitiesAction())
* .setExtractKeyPhrasesActions(new ExtractKeyPhrasesAction()),
* new AnalyzeActionsOptions().setIncludeStatistics(false))
* .flatMap(AsyncPollResponse::getFinalResult)
* .flatMap(analyzeActionsResultPagedFlux -> analyzeActionsResultPagedFlux.byPage())
* .subscribe(
* pagedResponse -> pagedResponse.getElements().forEach(
* analyzeActionsResult -> {
* System.out.println("Entities recognition action results:");
* analyzeActionsResult.getRecognizeEntitiesResults().forEach(
* actionResult -> {
* if (!actionResult.isError()) {
* actionResult.getDocumentsResults().forEach(
* entitiesResult -> entitiesResult.getEntities().forEach(
* entity -> System.out.printf(
* "Recognized entity: %s, entity category: %s, entity subcategory: %s,"
* + " confidence score: %f.%n",
* entity.getText(), entity.getCategory(), entity.getSubcategory(),
* entity.getConfidenceScore())));
* }
* });
* System.out.println("Key phrases extraction action results:");
* analyzeActionsResult.getExtractKeyPhrasesResults().forEach(
* actionResult -> {
* if (!actionResult.isError()) {
* actionResult.getDocumentsResults().forEach(extractKeyPhraseResult -> {
* System.out.println("Extracted phrases:");
* extractKeyPhraseResult.getKeyPhrases()
* .forEach(keyPhrases -> System.out.printf("\t%s.%n", keyPhrases));
* });
* }
* });
* }));
* </pre>
* <!-- end com.azure.ai.textanalytics.TextAnalyticsAsyncClient.beginAnalyzeActions#Iterable-TextAnalyticsActions-AnalyzeActionsOptions -->
*
* @param documents A list of {@link TextDocumentInput documents} to be analyzed.
* @param actions The {@link TextAnalyticsActions actions} that contains all actions to be executed.
* An action is one task of execution, such as a single task of 'Key Phrases Extraction' on the given document
* inputs.
* @param options The additional configurable {@link AnalyzeActionsOptions options} that may be passed when
* analyzing a collection of tasks.
*
* @return A {@link PollerFlux} that polls the analyze a collection of tasks operation until it has completed,
* has failed, or has been cancelled. The completed operation returns a {@link AnalyzeActionsResultPagedFlux}.
*
* @throws NullPointerException if {@code documents} is null.
* @throws IllegalArgumentException if {@code documents} is empty.
* @throws TextAnalyticsException If analyze operation fails.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PollerFlux<AnalyzeActionsOperationDetail, AnalyzeActionsResultPagedFlux> beginAnalyzeActions(
Iterable<TextDocumentInput> documents, TextAnalyticsActions actions, AnalyzeActionsOptions options) {
return analyzeActionsAsyncClient.beginAnalyzeActions(documents, actions, options, Context.NONE);
}
}