@@ -6,7 +6,7 @@ manager: nitinme
6
6
ms.service : cognitive-services
7
7
ms.subservice : text-analytics
8
8
ms.topic : include
9
- ms.date : 01/15 /2020
9
+ ms.date : 02/11 /2020
10
10
ms.author : aahi
11
11
ms.reviewer : tasharm, assafi
12
12
---
@@ -34,7 +34,7 @@ Add the following text analytics dependency to your project. This version of the
34
34
<dependency >
35
35
<groupId >com.azure</groupId >
36
36
<artifactId >azure-ai-textanalytics</artifactId >
37
- <version >1.0.0-beta.1 </version >
37
+ <version >1.0.0-beta.2 </version >
38
38
</dependency >
39
39
</dependencies >
40
40
```
@@ -44,28 +44,17 @@ Create a new java file in the following directory: `\src\main\java`.
44
44
Open the java file and add the following ` import ` statements:
45
45
46
46
``` 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.* ;
58
48
import com.azure.ai.textanalytics.TextAnalyticsClientBuilder ;
59
49
import com.azure.ai.textanalytics.TextAnalyticsClient ;
60
- import java.util.List ;
61
50
```
62
51
63
52
In the java file, add a new class and add your azure resource's key and endpoint as shown below.
64
53
65
54
[ !INCLUDE [ text-analytics-find-resource-information] ( ../find-azure-resource-info.md )]
66
55
67
56
``` java
68
- public class TextAnalyticsSample {
57
+ public class TextAnalyticsSamples {
69
58
private static String KEY = " <replace-with-your-text-analytics-key-here>" ;
70
59
private static String ENDPOINT = " <replace-with-your-text-analytics-endpoint-here>" ;
71
60
}
@@ -75,9 +64,9 @@ Add the following main method to the class. You will define the methods called h
75
64
76
65
``` java
77
66
public static void main(String [] args) {
78
-
67
+
79
68
TextAnalyticsClient client = authenticateClient(KEY , ENDPOINT );
80
-
69
+
81
70
sentimentAnalysisExample(client);
82
71
detectLanguageExample(client);
83
72
recognizeEntitiesExample(client);
@@ -105,11 +94,11 @@ The Text Analytics client is a `TextAnalyticsClient` object that authenticates t
105
94
Create a method to instantiate the ` TextAnalyticsClient ` object with your ` KEY ` AND ` ENDPOINT ` created above.
106
95
107
96
``` java
108
- static TextAnalyticsClient authenticateClient(String subscriptionKey , String endpoint) {
97
+ static TextAnalyticsClient authenticateClient(String key , String endpoint) {
109
98
return new TextAnalyticsClientBuilder ()
110
- .subscriptionKey(subscriptionKey )
111
- .endpoint(endpoint)
112
- .buildClient();
99
+ .apiKey( new TextAnalyticsApiKeyCredential (key) )
100
+ .endpoint(endpoint)
101
+ .buildClient();
113
102
}
114
103
```
115
104
@@ -125,33 +114,31 @@ static void sentimentAnalysisExample(TextAnalyticsClient client)
125
114
// The text that need be analyzed.
126
115
String text = " I had the best day of my life. I wish you were there with me." ;
127
116
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);
139
118
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
+ }
146
133
}
147
134
```
148
135
149
136
### Output
150
137
151
138
``` 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.
155
142
```
156
143
## Language detection
157
144
@@ -166,19 +153,18 @@ static void detectLanguageExample(TextAnalyticsClient client)
166
153
// The text that need be analyzed.
167
154
String text = " Ce document est rédigé en Français." ;
168
155
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());
175
161
}
176
162
```
177
163
178
164
### Output
179
165
180
166
``` console
181
- Language : French, ISO 6391 Name : fr, Score : 1.0 .
167
+ Detected primary language : French, ISO 6391 name : fr, score : 1.00 .
182
168
```
183
169
## Named Entity recognition (NER)
184
170
@@ -192,17 +178,15 @@ Create a new function called `recognizeEntitiesExample()` that takes the client
192
178
``` java
193
179
static void recognizeEntitiesExample(TextAnalyticsClient client)
194
180
{
195
- // The text that need be analysed .
181
+ // The text that need be analyzed .
196
182
String text = " I had a wonderful trip to Seattle last week." ;
197
-
198
- RecognizeEntitiesResult recognizeEntitiesResult = client. recognizeEntities(text);
199
183
200
- for (NamedEntity entity : recognizeEntitiesResult . getNamedEntities( )) {
184
+ for (CategorizedEntity entity : client . recognizeEntities(text )) {
201
185
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" ,
203
187
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 (),
206
190
entity. getOffset(),
207
191
entity. getLength(),
208
192
entity. getScore());
@@ -213,28 +197,26 @@ static void recognizeEntitiesExample(TextAnalyticsClient client)
213
197
### Output
214
198
215
199
``` 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 .
218
202
```
219
203
220
- ## Using NER to detect personal information
204
+ ## Using NER to recognize personal information
221
205
222
206
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.
223
207
224
208
``` java
225
209
static void recognizePIIEntitiesExample(TextAnalyticsClient client)
226
210
{
227
- // The text that need be analysed .
211
+ // The text that need be analyzed .
228
212
String text = " Insurance policy for SSN on file 123-12-1234 is here by approved." ;
229
-
230
- RecognizePiiEntitiesResult recognizePIIEntitiesResult = client. recognizePiiEntities(text);
231
213
232
- for (NamedEntity entity : recognizePIIEntitiesResult . getNamedEntities( )) {
214
+ for (PiiEntity entity : client . recognizePiiEntities(text )) {
233
215
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" ,
235
217
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 (),
238
220
entity. getOffset(),
239
221
entity. getLength(),
240
222
entity. getScore());
@@ -245,8 +227,7 @@ static void recognizePIIEntitiesExample(TextAnalyticsClient client)
245
227
### Output
246
228
247
229
``` 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.
250
231
```
251
232
252
233
## Entity linking
@@ -256,29 +237,27 @@ Create a new function called `recognizeLinkedEntitiesExample()` that takes the c
256
237
``` java
257
238
static void recognizeLinkedEntitiesExample(TextAnalyticsClient client)
258
239
{
259
- // The text that need be analysed .
240
+ // The text that need be analyzed .
260
241
String text = " Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, " +
261
242
" to develop and sell BASIC interpreters for the Altair 8800. " +
262
243
" During his career at Microsoft, Gates held the positions of chairman, " +
263
244
" chief executive officer, president and chief software architect, " +
264
245
" while also being the largest individual shareholder until May 2014." ;
265
-
266
- RecognizeLinkedEntitiesResult recognizeLinkedEntitiesResult = client. recognizeLinkedEntities(text);
267
246
268
247
System . out. printf(" Linked Entities:%n" );
269
- for (LinkedEntity linkedEntity : recognizeLinkedEntitiesResult . getLinkedEntities( )) {
248
+ for (LinkedEntity linkedEntity : client . recognizeLinkedEntities(text )) {
270
249
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" );
276
255
for (LinkedEntityMatch linkedEntityMatch : linkedEntity. getLinkedEntityMatches()) {
277
256
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());
282
261
}
283
262
}
284
263
}
@@ -289,25 +268,25 @@ static void recognizeLinkedEntitiesExample(TextAnalyticsClient client)
289
268
``` console
290
269
Linked Entities:
291
270
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 .
294
273
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 .
298
277
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 .
301
280
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 .
305
284
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 .
308
287
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 .
311
290
```
312
291
## Key phrase extraction
313
292
@@ -318,18 +297,18 @@ static void extractKeyPhrasesExample(TextAnalyticsClient client)
318
297
{
319
298
// The text that need be analyzed.
320
299
String text = " My cat might need to see a veterinarian." ;
321
-
322
- ExtractKeyPhraseResult keyPhraseResult = client. extractKeyPhrases(text);
323
300
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);
326
304
}
327
305
}
328
306
```
329
307
330
308
### Output
331
309
332
310
``` console
333
- Recognized Phrases: cat.
334
- Recognized Phrases: veterinarian.
311
+ Recognized phrases:
312
+ cat
313
+ veterinarian
335
314
```
0 commit comments