Skip to content

Commit 5900173

Browse files
authored
Merge pull request #104067 from tasharm-0412/tasharm/updateToLatestSDK
Updated to latest java sdk
2 parents c697bae + 3e452f2 commit 5900173

File tree

1 file changed

+79
-100
lines changed
  • articles/cognitive-services/text-analytics/includes/quickstarts

1 file changed

+79
-100
lines changed

articles/cognitive-services/text-analytics/includes/quickstarts/java-sdk.md

Lines changed: 79 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ manager: nitinme
66
ms.service: cognitive-services
77
ms.subservice: text-analytics
88
ms.topic: include
9-
ms.date: 01/15/2020
9+
ms.date: 02/11/2020
1010
ms.author: aahi
1111
ms.reviewer: tasharm, assafi
1212
---
@@ -34,7 +34,7 @@ Add the following text analytics dependency to your project. This version of the
3434
<dependency>
3535
<groupId>com.azure</groupId>
3636
<artifactId>azure-ai-textanalytics</artifactId>
37-
<version>1.0.0-beta.1</version>
37+
<version>1.0.0-beta.2</version>
3838
</dependency>
3939
</dependencies>
4040
```
@@ -44,28 +44,17 @@ Create a new java file in the following directory: `\src\main\java`.
4444
Open the java file and add the following `import` statements:
4545

4646
```java
47-
import com.azure.ai.textanalytics.models.AnalyzeSentimentResult;
48-
import com.azure.ai.textanalytics.models.DetectLanguageResult;
49-
import com.azure.ai.textanalytics.models.DetectedLanguage;
50-
import com.azure.ai.textanalytics.models.ExtractKeyPhraseResult;
51-
import com.azure.ai.textanalytics.models.LinkedEntity;
52-
import com.azure.ai.textanalytics.models.LinkedEntityMatch;
53-
import com.azure.ai.textanalytics.models.NamedEntity;
54-
import com.azure.ai.textanalytics.models.RecognizeEntitiesResult;
55-
import com.azure.ai.textanalytics.models.RecognizeLinkedEntitiesResult;
56-
import com.azure.ai.textanalytics.models.RecognizePiiEntitiesResult;
57-
import com.azure.ai.textanalytics.models.TextSentiment;
47+
import com.azure.ai.textanalytics.models.*;
5848
import com.azure.ai.textanalytics.TextAnalyticsClientBuilder;
5949
import com.azure.ai.textanalytics.TextAnalyticsClient;
60-
import java.util.List;
6150
```
6251

6352
In the java file, add a new class and add your azure resource's key and endpoint as shown below.
6453

6554
[!INCLUDE [text-analytics-find-resource-information](../find-azure-resource-info.md)]
6655

6756
```java
68-
public class TextAnalyticsSample {
57+
public class TextAnalyticsSamples {
6958
private static String KEY = "<replace-with-your-text-analytics-key-here>";
7059
private static String ENDPOINT = "<replace-with-your-text-analytics-endpoint-here>";
7160
}
@@ -75,9 +64,9 @@ Add the following main method to the class. You will define the methods called h
7564

7665
```java
7766
public static void main(String[] args) {
78-
67+
7968
TextAnalyticsClient client = authenticateClient(KEY, ENDPOINT);
80-
69+
8170
sentimentAnalysisExample(client);
8271
detectLanguageExample(client);
8372
recognizeEntitiesExample(client);
@@ -105,11 +94,11 @@ The Text Analytics client is a `TextAnalyticsClient` object that authenticates t
10594
Create a method to instantiate the `TextAnalyticsClient` object with your `KEY` AND `ENDPOINT` created above.
10695

10796
```java
108-
static TextAnalyticsClient authenticateClient(String subscriptionKey, String endpoint) {
97+
static TextAnalyticsClient authenticateClient(String key, String endpoint) {
10998
return new TextAnalyticsClientBuilder()
110-
.subscriptionKey(subscriptionKey)
111-
.endpoint(endpoint)
112-
.buildClient();
99+
.apiKey(new TextAnalyticsApiKeyCredential(key))
100+
.endpoint(endpoint)
101+
.buildClient();
113102
}
114103
```
115104

@@ -125,33 +114,31 @@ static void sentimentAnalysisExample(TextAnalyticsClient client)
125114
// The text that need be analyzed.
126115
String text = "I had the best day of my life. I wish you were there with me.";
127116

128-
AnalyzeSentimentResult sentimentResult = client.analyzeSentiment(text);
129-
TextSentiment documentSentiment = sentimentResult.getDocumentSentiment();
130-
System.out.printf(
131-
"Recognized TextSentiment: %s, Positive Score: %.2f, Neutral Score: %.2f, Negative Score: %.2f.%n",
132-
documentSentiment.getTextSentimentClass(),
133-
documentSentiment.getPositiveScore(),
134-
documentSentiment.getNeutralScore(),
135-
documentSentiment.getNegativeScore());
136-
137-
List<TextSentiment> sentiments = sentimentResult.getSentenceSentiments();
138-
for (TextSentiment textSentiment : sentiments) {
117+
DocumentSentiment documentSentiment = client.analyzeSentiment(text);
139118
System.out.printf(
140-
"Recognized Sentence TextSentiment: %s, Positive Score: %.2f, Neutral Score: %.2f, Negative Score: %.2f.%n",
141-
textSentiment.getTextSentimentClass(),
142-
textSentiment.getPositiveScore(),
143-
textSentiment.getNeutralScore(),
144-
textSentiment.getNegativeScore());
145-
}
119+
"Recognized document sentiment: %s, positive score: %.2f, neutral score: %.2f, negative score: %.2f.%n",
120+
documentSentiment.getSentiment(),
121+
documentSentiment.getSentimentScores().getPositive(),
122+
documentSentiment.getSentimentScores().getNeutral(),
123+
documentSentiment.getSentimentScores().getNegative());
124+
125+
for (SentenceSentiment sentenceSentiment : documentSentiment.getSentences()) {
126+
System.out.printf(
127+
"Recognized sentence sentiment: %s, positive score: %.2f, neutral score: %.2f, negative score: %.2f.%n",
128+
sentenceSentiment.getSentiment(),
129+
sentenceSentiment.getSentimentScores().getPositive(),
130+
sentenceSentiment.getSentimentScores().getNeutral(),
131+
sentenceSentiment.getSentimentScores().getNegative());
132+
}
146133
}
147134
```
148135

149136
### Output
150137

151138
```console
152-
Recognized TextSentiment: positive, Positive Score: 1.00, Neutral Score: 0.00, Negative Score: 0.00.
153-
Recognized Sentence TextSentiment: positive, Positive Score: 1.00, Neutral Score: 0.00, Negative Score: 0.00.
154-
Recognized Sentence TextSentiment: neutral, Positive Score: 0.21, Neutral Score: 0.77, Negative Score: 0.02.
139+
Recognized document sentiment: positive, Positive Score: 1.00, Neutral Score: 0.00, Negative Score: 0.00.
140+
Recognized sentence sentiment: positive, positive score: 1.00, neutral score: 0.00, negative score: 0.00.
141+
Recognized sentence sentiment: neutral, positive score: 0.21, neutral score: 0.77, negative score: 0.02.
155142
```
156143
## Language detection
157144

@@ -166,19 +153,18 @@ static void detectLanguageExample(TextAnalyticsClient client)
166153
// The text that need be analyzed.
167154
String text = "Ce document est rédigé en Français.";
168155

169-
DetectLanguageResult detectLanguageResult = client.detectLanguage(text, "US");
170-
DetectedLanguage detectedDocumentLanguage = detectLanguageResult.getPrimaryLanguage();
171-
System.out.printf("Language: %s, ISO 6391 Name: %s, Score: %s.%n",
172-
detectedDocumentLanguage.getName(),
173-
detectedDocumentLanguage.getIso6391Name(),
174-
detectedDocumentLanguage.getScore());
156+
DetectedLanguage detectedLanguage = client.detectLanguage(text);
157+
System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %.2f.%n",
158+
detectedLanguage.getName(),
159+
detectedLanguage.getIso6391Name(),
160+
detectedLanguage.getScore());
175161
}
176162
```
177163

178164
### Output
179165

180166
```console
181-
Language: French, ISO 6391 Name: fr, Score: 1.0.
167+
Detected primary language: French, ISO 6391 name: fr, score: 1.00.
182168
```
183169
## Named Entity recognition (NER)
184170

@@ -192,17 +178,15 @@ Create a new function called `recognizeEntitiesExample()` that takes the client
192178
```java
193179
static void recognizeEntitiesExample(TextAnalyticsClient client)
194180
{
195-
// The text that need be analysed.
181+
// The text that need be analyzed.
196182
String text = "I had a wonderful trip to Seattle last week.";
197-
198-
RecognizeEntitiesResult recognizeEntitiesResult = client.recognizeEntities(text);
199183

200-
for (NamedEntity entity : recognizeEntitiesResult.getNamedEntities()) {
184+
for (CategorizedEntity entity : client.recognizeEntities(text)) {
201185
System.out.printf(
202-
"Recognized NamedEntity Text: %s, Type: %s, Subtype: %s, Offset: %s, Length: %s, Score: %.3f.%n",
186+
"Recognized entity: %s, entity category: %s, entity sub-category: %s, offset: %s, length: %s, score: %.2f.%n",
203187
entity.getText(),
204-
entity.getType(),
205-
entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(),
188+
entity.getCategory(),
189+
entity.getSubCategory() == null || entity.getSubCategory().isEmpty() ? "N/A" : entity.getSubCategory(),
206190
entity.getOffset(),
207191
entity.getLength(),
208192
entity.getScore());
@@ -213,28 +197,26 @@ static void recognizeEntitiesExample(TextAnalyticsClient client)
213197
### Output
214198

215199
```console
216-
Recognized NamedEntity Text: Seattle, Type: Location, Subtype: N/A, Offset: 26, Length: 7, Score: 0.806.
217-
Recognized NamedEntity Text: last week, Type: DateTime, Subtype: DateRange, Offset: 34, Length: 9, Score: 0.800.
200+
Recognized entity: Seattle, entity category: Location, entity sub-category: GPE, offset: 26, length: 7, score: 0.92.
201+
Recognized entity: last week, entity category: DateTime, entity sub-category: DateRange, offset: 34, length: 9, score: 0.80.
218202
```
219203

220-
## Using NER to detect personal information
204+
## Using NER to recognize personal information
221205

222206
Create a new function called `recognizePIIEntitiesExample()` that takes the client that you created earlier, and call its `recognizePiiEntities()` function. The returned `RecognizePiiEntitiesResult` object will contain a list of `NamedEntity` if successful, or an `errorMessage` if not.
223207

224208
```java
225209
static void recognizePIIEntitiesExample(TextAnalyticsClient client)
226210
{
227-
// The text that need be analysed.
211+
// The text that need be analyzed.
228212
String text = "Insurance policy for SSN on file 123-12-1234 is here by approved.";
229-
230-
RecognizePiiEntitiesResult recognizePIIEntitiesResult = client.recognizePiiEntities(text);
231213

232-
for (NamedEntity entity : recognizePIIEntitiesResult.getNamedEntities()) {
214+
for (PiiEntity entity : client.recognizePiiEntities(text)) {
233215
System.out.printf(
234-
"Personally Identifiable Information Entities Text: %s, Type: %s, Subtype: %s, Offset: %s, Length: %s, Score: %s.%n",
216+
"Recognized personal identifiable information entity: %s, entity category: %s, entity sub-category: %s, offset: %s, length: %s, score: %.2f.%n",
235217
entity.getText(),
236-
entity.getType(),
237-
entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(),
218+
entity.getCategory(),
219+
entity.getSubCategory() == null || entity.getSubCategory().isEmpty() ? "N/A" : entity.getSubCategory(),
238220
entity.getOffset(),
239221
entity.getLength(),
240222
entity.getScore());
@@ -245,8 +227,7 @@ static void recognizePIIEntitiesExample(TextAnalyticsClient client)
245227
### Output
246228

247229
```console
248-
Personally Identifiable Information Entities
249-
Text: 123-12-1234, Type: U.S. Social Security Number (SSN), Subtype: N/A, Offset: 33, Length: 11, Score: 0.85.
230+
Recognized personal identifiable information entity: 123-12-1234, entity category: U.S. Social Security Number (SSN), entity sub-category: N/A, offset: 33, length: 11, score: 0.85.
250231
```
251232

252233
## Entity linking
@@ -256,29 +237,27 @@ Create a new function called `recognizeLinkedEntitiesExample()` that takes the c
256237
```java
257238
static void recognizeLinkedEntitiesExample(TextAnalyticsClient client)
258239
{
259-
// The text that need be analysed.
240+
// The text that need be analyzed.
260241
String text = "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, " +
261242
"to develop and sell BASIC interpreters for the Altair 8800. " +
262243
"During his career at Microsoft, Gates held the positions of chairman, " +
263244
"chief executive officer, president and chief software architect, " +
264245
"while also being the largest individual shareholder until May 2014.";
265-
266-
RecognizeLinkedEntitiesResult recognizeLinkedEntitiesResult = client.recognizeLinkedEntities(text);
267246

268247
System.out.printf("Linked Entities:%n");
269-
for (LinkedEntity linkedEntity : recognizeLinkedEntitiesResult.getLinkedEntities()) {
248+
for (LinkedEntity linkedEntity : client.recognizeLinkedEntities(text)) {
270249
System.out.printf("Name: %s, ID: %s, URL: %s, Data Source: %s.%n",
271-
linkedEntity.getName(),
272-
linkedEntity.getId(),
273-
linkedEntity.getUrl(),
274-
linkedEntity.getDataSource());
275-
System.out.printf("tMatches:%n");
250+
linkedEntity.getName(),
251+
linkedEntity.getId(),
252+
linkedEntity.getUrl(),
253+
linkedEntity.getDataSource());
254+
System.out.printf("Matches:%n");
276255
for (LinkedEntityMatch linkedEntityMatch : linkedEntity.getLinkedEntityMatches()) {
277256
System.out.printf("Text: %s, Offset: %s, Length: %s, Score: %.2f.%n",
278-
linkedEntityMatch.getText(),
279-
linkedEntityMatch.getOffset(),
280-
linkedEntityMatch.getLength(),
281-
linkedEntityMatch.getScore());
257+
linkedEntityMatch.getText(),
258+
linkedEntityMatch.getOffset(),
259+
linkedEntityMatch.getLength(),
260+
linkedEntityMatch.getScore());
282261
}
283262
}
284263
}
@@ -289,25 +268,25 @@ static void recognizeLinkedEntitiesExample(TextAnalyticsClient client)
289268
```console
290269
Linked Entities:
291270
Name: Altair 8800, ID: Altair 8800, URL: https://en.wikipedia.org/wiki/Altair_8800, Data Source: Wikipedia.
292-
tMatches:
293-
Text: Altair 8800, Offset: 11, Length: 116, Score: 0.65.
271+
Matches:
272+
Text: Altair 8800, Offset: 11, Length: 116, Score: 0.78.
294273
Name: Bill Gates, ID: Bill Gates, URL: https://en.wikipedia.org/wiki/Bill_Gates, Data Source: Wikipedia.
295-
tMatches:
296-
Text: Bill Gates, Offset: 10, Length: 25, Score: 0.24.
297-
Text: Gates, Offset: 5, Length: 161, Score: 0.24.
274+
Matches:
275+
Text: Bill Gates, Offset: 10, Length: 25, Score: 0.55.
276+
Text: Gates, Offset: 5, Length: 161, Score: 0.55.
298277
Name: Paul Allen, ID: Paul Allen, URL: https://en.wikipedia.org/wiki/Paul_Allen, Data Source: Wikipedia.
299-
tMatches:
300-
Text: Paul Allen, Offset: 10, Length: 40, Score: 0.17.
278+
Matches:
279+
Text: Paul Allen, Offset: 10, Length: 40, Score: 0.53.
301280
Name: Microsoft, ID: Microsoft, URL: https://en.wikipedia.org/wiki/Microsoft, Data Source: Wikipedia.
302-
tMatches:
303-
Text: Microsoft, Offset: 9, Length: 0, Score: 0.20.
304-
Text: Microsoft, Offset: 9, Length: 150, Score: 0.20.
281+
Matches:
282+
Text: Microsoft, Offset: 9, Length: 0, Score: 0.47.
283+
Text: Microsoft, Offset: 9, Length: 150, Score: 0.47.
305284
Name: April 4, ID: April 4, URL: https://en.wikipedia.org/wiki/April_4, Data Source: Wikipedia.
306-
tMatches:
307-
Text: April 4, Offset: 7, Length: 54, Score: 0.14.
285+
Matches:
286+
Text: April 4, Offset: 7, Length: 54, Score: 0.25.
308287
Name: BASIC, ID: BASIC, URL: https://en.wikipedia.org/wiki/BASIC, Data Source: Wikipedia.
309-
tMatches:
310-
Text: BASIC, Offset: 5, Length: 89, Score: 0.05.
288+
Matches:
289+
Text: BASIC, Offset: 5, Length: 89, Score: 0.28.
311290
```
312291
## Key phrase extraction
313292

@@ -318,18 +297,18 @@ static void extractKeyPhrasesExample(TextAnalyticsClient client)
318297
{
319298
// The text that need be analyzed.
320299
String text = "My cat might need to see a veterinarian.";
321-
322-
ExtractKeyPhraseResult keyPhraseResult = client.extractKeyPhrases(text);
323300

324-
for (String keyPhrase : keyPhraseResult.getKeyPhrases()) {
325-
System.out.printf("Recognized Phrases: %s.%n", keyPhrase);
301+
System.out.printf("Recognized phrases: %n");
302+
for (String keyPhrase : client.extractKeyPhrases(text)) {
303+
System.out.printf("%s%n", keyPhrase);
326304
}
327305
}
328306
```
329307

330308
### Output
331309

332310
```console
333-
Recognized Phrases: cat.
334-
Recognized Phrases: veterinarian.
311+
Recognized phrases:
312+
cat
313+
veterinarian
335314
```

0 commit comments

Comments
 (0)