-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added codesnippet for sync client and builder (#7254)
* Fixed sync #6874, * removed detectLanguage(string text, string countryHint) method to align with other API. * renamed analyzeBatchSentimentWithResponse(String text, String language, Context context)) to * analyzeSentimentWithResponse * fixed tests, that include added tests for faultyText in sync and async client.
- Loading branch information
Showing
11 changed files
with
1,302 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,7 +81,7 @@ When an HTTP client is included on the classpath, as shown above, it is not nece | |
|
||
For starters, by having the Netty or OkHTTP dependencies on your classpath, as shown above, you can create new instances of these `HttpClient` types using their builder APIs. For example, here is how you would create a Netty HttpClient instance: | ||
|
||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L159-L162 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L35-L38 --> | ||
```java | ||
HttpClient client = new NettyAsyncHttpClientBuilder() | ||
.port(8080) | ||
|
@@ -135,7 +135,7 @@ cognitive services. | |
``` | ||
|
||
Use the key as the credential parameter to authenticate the client: | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L29-L32 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L45-L48 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
|
@@ -162,7 +162,7 @@ cognitive services. | |
AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET | ||
|
||
Use the returned token credential to authenticate the client: | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L49-L52 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L65-L68 --> | ||
```java | ||
TextAnalyticsAsyncClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.endpoint(ENDPOINT) | ||
|
@@ -176,7 +176,7 @@ analyze sentiment, recognize entities, detect language, and extract key phrases | |
To create a client object, you will need the cognitive services or text analytics endpoint to | ||
your resource and a subscription key that allows you access: | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L29-L32 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L45-L48 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
|
@@ -248,14 +248,14 @@ The following sections provide several code snippets covering some of the most c | |
Text analytics support both synchronous and asynchronous client creation by using | ||
`TextAnalyticsClientBuilder`, | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L29-L32 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L45-L48 --> | ||
``` java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
.endpoint(ENDPOINT) | ||
.buildClient(); | ||
``` | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L39-L42 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L55-L58 --> | ||
``` java | ||
TextAnalyticsAsyncClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
|
@@ -264,13 +264,8 @@ TextAnalyticsAsyncClient textAnalyticsClient = new TextAnalyticsClientBuilder() | |
``` | ||
### Detect language | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L59-L71 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L75-L82 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
.endpoint(ENDPOINT) | ||
.buildClient(); | ||
String inputText = "Bonjour tout le monde"; | ||
for (DetectedLanguage detectedLanguage : textAnalyticsClient.detectLanguage(inputText).getDetectedLanguages()) { | ||
|
@@ -282,13 +277,8 @@ for (DetectedLanguage detectedLanguage : textAnalyticsClient.detectLanguage(inpu | |
``` | ||
### Recognize entity | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L78-L92 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L89-L98 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
.endpoint(ENDPOINT) | ||
.buildClient(); | ||
String text = "Satya Nadella is the CEO of Microsoft"; | ||
for (NamedEntity entity : textAnalyticsClient.recognizeEntities(text).getNamedEntities()) { | ||
|
@@ -302,14 +292,8 @@ for (NamedEntity entity : textAnalyticsClient.recognizeEntities(text).getNamedEn | |
``` | ||
### Recognize PII(Personally Identifiable Information) entity | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L99-L114 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L105-L114 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
.endpoint(ENDPOINT) | ||
.buildClient(); | ||
// The text that need be analysed. | ||
String text = "My SSN is 555-55-5555"; | ||
for (NamedEntity entity : textAnalyticsClient.recognizePiiEntities(text).getNamedEntities()) { | ||
|
@@ -323,15 +307,9 @@ for (NamedEntity entity : textAnalyticsClient.recognizePiiEntities(text).getName | |
``` | ||
### Recognize linked entity | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L121-L134 --> | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L121-L128 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
.endpoint(ENDPOINT) | ||
.buildClient(); | ||
// The text that need be analysed. | ||
String text = "Old Faithful is a geyser at Yellowstone Park."; | ||
for (LinkedEntity linkedEntity : textAnalyticsClient.recognizeLinkedEntities(text).getLinkedEntities()) { | ||
|
@@ -341,16 +319,19 @@ for (LinkedEntity linkedEntity : textAnalyticsClient.recognizeLinkedEntities(tex | |
linkedEntity.getDataSource()); | ||
} | ||
``` | ||
### Extract key phrases | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L135-L139 --> | ||
```java | ||
String text = "My cat might need to see a veterinarian."; | ||
### Analyze sentiment | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L141-L152 --> | ||
for (String keyPhrase : textAnalyticsClient.extractKeyPhrases(text).getKeyPhrases()) { | ||
System.out.printf("Recognized phrases: %s.%n", keyPhrase); | ||
} | ||
``` | ||
### Analyze sentiment | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L146-L152 --> | ||
```java | ||
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder() | ||
.subscriptionKey(SUBSCRIPTION_KEY) | ||
.endpoint(ENDPOINT) | ||
.buildClient(); | ||
String text = "The hotel was dark and unclean."; | ||
for (TextSentiment textSentiment : textAnalyticsClient.analyzeSentiment(text).getSentenceSentiments()) { | ||
|
@@ -361,7 +342,24 @@ for (TextSentiment textSentiment : textAnalyticsClient.analyzeSentiment(text).ge | |
``` | ||
## Troubleshooting | ||
## General | ||
### General | ||
Text Analytics clients raise exceptions. For example, if you try to detect the languages of a batch of text with same | ||
document IDs, 400 error is return that indicating bad request. In the following code snippet, the error is handled | ||
gracefully by catching the exception and display the additional information about the error. | ||
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L164-L168 --> | ||
```java | ||
try { | ||
textAnalyticsClient.detectBatchLanguages(inputs); | ||
} catch (HttpResponseException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
``` | ||
### Enable client logging | ||
You can set the `AZURE_LOG_LEVEL` environment variable to view logging statements made in the client library. For | ||
example, setting `AZURE_LOG_LEVEL=2` would show all informational, warning, and error log messages. The log levels can | ||
be found here: [log levels][LogLevels]. | ||
## Next steps | ||
- Samples are explained in detail [here][samples_readme]. | ||
|
@@ -375,8 +373,8 @@ When you submit a pull request, a CLA-bot will automatically determine whether y | |
This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [[email protected]][coc_contact] with any additional questions or comments. | ||
<!-- LINKS --> | ||
[azure_subscription]: https://azure.microsoft.com/free | ||
[api_reference_doc]: https://azure.github.io/azure-sdk-for-java/textanalytics.html | ||
[azure_subscription]: https://azure.microsoft.com/free | ||
[cla]: https://cla.microsoft.com | ||
[coc]: https://opensource.microsoft.com/codeofconduct/ | ||
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
|
@@ -385,5 +383,6 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m | |
[product_documentation]: https://docs.microsoft.com/azure/cognitive-services/text-analytics/overview | ||
[samples_readme]: src/samples/README.md | ||
[source_code]: src | ||
[LogLevels]: ../../core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java | ||
 |
Oops, something went wrong.