-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added codesnippet for sync client and builder #7254
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b1cf0f1
add codesnippet for sync client and builder
mssfang 2ce75a2
update api use cases
mssfang 17ae686
fixed feedbacks
mssfang 0c669d5
fixed tests for sentiment
mssfang 3d20d9b
readme
mssfang 6c8c24f
update readme and changelog
mssfang 5c6d3b6
update line number for end
mssfang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
|
||
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Ftextanalytics%2Fazure-ai-textanalytics%2FREADME.png) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: 400 ->
404
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
404 is an interesting HTTP error code for "Bad Request'. Traditionally, 400 is bad request... and 404 is "Not found". (ie. this URL you made does not exist).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://en.wikipedia.org/wiki/HTTP_404
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh mistyped I mean
400
itself just syntax highlighted 🙆