Skip to content

Commit

Permalink
update readme with snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Jan 6, 2020
1 parent 1277f77 commit 5bab849
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 13 deletions.
30 changes: 19 additions & 11 deletions sdk/textanalytics/azure-ai-textanalytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ cognitive services.
```az cognitiveservices account keys list --name "resource-name" --resource-group "resource-group-name"```

Use the key as the credential parameter to authenticate the client:
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L26-L31 -->
```java
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
Expand All @@ -158,6 +159,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#L48-L53 -->
```java
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.endpoint("https://servicename.cognitiveservices.azure.com/")
Expand All @@ -171,6 +173,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#L#L26-L31 -->
```java
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
Expand Down Expand Up @@ -243,14 +246,15 @@ 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#L26-L31 -->
``` java
// An example of creating a synchronous client
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.buildClient();
```
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L37-L42 -->
``` java
// An example of creating an asynchronous client
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
Expand All @@ -260,8 +264,7 @@ TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
```
### Detect language
Detect language in a batch of documents.
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L59-L73 -->
```java
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
Expand All @@ -270,16 +273,16 @@ TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
String inputText = "Bonjour tout le monde";
for(DetectedLanguage detectedLanguage : client.detectLanguage(text, "US").getDetectedLanguages()) {
System.out.printf("Other detected languages: %s, ISO 6391 Name: %s, Score: %s.%n",
for (DetectedLanguage detectedLanguage : textAnalyticsClient.detectLanguage(inputText).getDetectedLanguages()) {
System.out.printf("Detected languages name: %s, ISO 6391 Name: %s, Score: %s.%n",
detectedLanguage.getName(),
detectedLanguage.getIso6391Name(),
detectedLanguage.getScore());
}
```
### Recognize entity
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L79-L95 -->
```java
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
Expand All @@ -301,6 +304,7 @@ for (NamedEntity entity : client.recognizeEntities(text).getNamedEntities()) {
```
### Recognize PII(Personally Identifiable Information) entity
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L101-L118 -->
```java
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
Expand All @@ -310,17 +314,19 @@ TextAnalyticsClient client = new TextAnalyticsClientBuilder()
// The text that need be analysed.
String text = "My SSN is 555-55-5555";
for (NamedEntity entity : client.recognizePiiEntities(text).getNamedEntities()) {
for (NamedEntity entity : textAnalyticsClient.recognizePiiEntities(text).getNamedEntities()) {
System.out.printf(
"Recognized PII Entity: %s, Type: %s, Subtype: %s, Score: %s.%n",
entity.getText(),
entity.getType(),
entity.getSubtype(),
entity.getScore()));
entity.getScore());
}
```
### Recognize linked entity
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L124-L139 -->
```java
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
Expand All @@ -330,15 +336,17 @@ TextAnalyticsClient client = new TextAnalyticsClientBuilder()
// The text that need be analysed.
String text = "Old Faithful is a geyser at Yellowstone Park.";
for (LinkedEntity linkedEntity : client.recognizeLinkedEntities(text).getLinkedEntities()) {
System.out.printf("Recognized Linked NamedEntity: %s, URL: %s, Data Source: %s.%n",
for (LinkedEntity linkedEntity : textAnalyticsClient.recognizeLinkedEntities(text).getLinkedEntities()) {
System.out.printf("Recognized Linked Entity: %s, Url: %s, Data Source: %s.%n",
linkedEntity.getName(),
linkedEntity.getUri(),
linkedEntity.getUrl(),
linkedEntity.getDataSource());
}
```
### Analyze sentiment
<!-- embedme ./src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java#L145-L158 -->
```java
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.azure.ai.textanalytics.models.TextSentiment;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.textanalytics;

import com.azure.ai.textanalytics.models.DetectedLanguage;
import com.azure.ai.textanalytics.models.LinkedEntity;
import com.azure.ai.textanalytics.models.NamedEntity;
import com.azure.ai.textanalytics.models.TextSentiment;

/**
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS ARE USED TO EXTRACT
* APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING LINE NUMBERS OF EXISTING CODE
* SAMPLES.
*
* Class containing code snippets that will be injected to README.md.
*/
public class ReadmeSamples {
private static final String SUBSCRIPTION_KEY = null;
private static final String ENDPOINT = null;

/**
* Code snippet for getting sync client using subscription key authentication.
*
*/
public void useSubscriptionKeySyncClient() {
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(SUBSCRIPTION_KEY)
.endpoint(ENDPOINT)
.buildClient();
}

/**
* Code snippet for getting async client using subscription key authentication.
*
*/
public void useSubscriptionKeyAsyncClient() {
TextAnalyticsAsyncClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(SUBSCRIPTION_KEY)
.endpoint(ENDPOINT)
.buildAsyncClient();
}

/**
* Code snippet for getting async client using AAD authentication.
*
*/
public void useAadAsyncClient() {
TextAnalyticsAsyncClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(SUBSCRIPTION_KEY)
.endpoint(ENDPOINT)
.buildAsyncClient();
}

/**
* Code snippet for detecting language in a text.
*
*/
public void detectLanguages() {
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(SUBSCRIPTION_KEY)
.endpoint(ENDPOINT)
.buildClient();

String inputText = "Bonjour tout le monde";

for (DetectedLanguage detectedLanguage : textAnalyticsClient.detectLanguage(inputText).getDetectedLanguages()) {
System.out.printf("Detected languages name: %s, ISO 6391 Name: %s, Score: %s.%n",
detectedLanguage.getName(),
detectedLanguage.getIso6391Name(),
detectedLanguage.getScore());
}
}

/**
* Code snippet for recognizing named entity in a text.
*
*/
public void recognizeNamedEntity() {
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()) {
System.out.printf(
"Recognized Named Entity: %s, Type: %s, Subtype: %s, Score: %s.%n",
entity.getText(),
entity.getType(),
entity.getSubtype(),
entity.getScore());
}
}

/**
* Code snippet for recognizing pii entity in a text.
*
*/
public void recognizePiiEntity() {
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()) {
System.out.printf(
"Recognized PII Entity: %s, Type: %s, Subtype: %s, Score: %s.%n",
entity.getText(),
entity.getType(),
entity.getSubtype(),
entity.getScore());
}
}

/**
* Code snippet for recognizing linked entity in a text.
*
*/
public void recognizeLinkedEntity() {
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()) {
System.out.printf("Recognized Linked Entity: %s, Url: %s, Data Source: %s.%n",
linkedEntity.getName(),
linkedEntity.getUrl(),
linkedEntity.getDataSource());
}
}

/**
* Code snippet for analyzing sentiment of a text.
*
*/
public void analyzeSentiment() {
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()) {
System.out.printf(
"Analyzed Sentence Sentiment class: %s.%n",
textSentiment.getTextSentimentClass());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void main(String[] args) {
System.out.printf(
"Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n",
entity.getText(),
entity.getType() ,
entity.getType(),
entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(),
entity.getOffset(),
entity.getLength(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public TextAnalyticsClient createTextAnalyticsClient() {
// END: com.azure.ai.textanalytics.TextAnalyticsClient.instantiation
return textAnalyticsClient;
}


}

0 comments on commit 5bab849

Please sign in to comment.