Skip to content

Commit

Permalink
Added codesnippet for sync client and builder (#7254)
Browse files Browse the repository at this point in the history
* 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
mssfang authored Jan 8, 2020
1 parent 6c9fd21 commit 6da172a
Show file tree
Hide file tree
Showing 11 changed files with 1,302 additions and 117 deletions.
6 changes: 4 additions & 2 deletions sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Release History

## 1.0.0-beta.1 (Unreleased)
## 1.0.0-beta.1 (2020-01-09)

For details on the Azure SDK for Java (January 2020 Preview) release refer to the [release announcement]().
- Initial release of this module.
- New namespace/package name:
- The namespace/package name for Azure Text Analytics client library has changed from
`com.microsoft.azure.cognitiveservices.language.textanalytics` to `com.azure.ai.textanalytics`
- Added support for subscription key and AAD authentication for both synchronous and asynchronous clients.
- Added Detect Language, Recognize Entity, Recognize PII entity, Recognize Linking Entity, Analyze Sentiment APIs.

Expand Down
81 changes: 40 additions & 41 deletions sdk/textanalytics/azure-ai-textanalytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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].
Expand All @@ -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/
Expand All @@ -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
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Ftextanalytics%2Fazure-ai-textanalytics%2FREADME.png)
Loading

0 comments on commit 6da172a

Please sign in to comment.