@@ -51,6 +51,16 @@ npm init
51
51
```
52
52
### Install the client library
53
53
54
+ #### [ Version 3.0-preview] ( #tab/version-3 )
55
+
56
+ Install the ` @azure/ai-text-analytics ` NPM packages:
57
+
58
+ ``` console
59
+ npm install --save @azure/ai-text-analytics
60
+ ```
61
+
62
+ #### [ Version 2.1] ( #tab/version-2 )
63
+
54
64
Install the ` @azure/cognitiveservices-textanalytics ` NPM packages:
55
65
56
66
``` console
@@ -61,11 +71,27 @@ Your app's `package.json` file will be updated with the dependencies.
61
71
62
72
Create a file named ` index.js ` and add the following libraries:
63
73
74
+ ---
75
+
76
+ Your app's ` package.json ` file will be updated with the dependencies.
77
+ Create a file named ` index.js ` and add the following libraries:
78
+
79
+ #### [ Version 3.0-preview] ( #tab/version-3 )
80
+
81
+ ``` javascript
82
+ " use strict" ;
83
+
84
+ const { TextAnalyticsClient , TextAnalyticsApiKeyCredential } = require (" @azure/ai-text-analytics" );
85
+ ```
86
+
87
+ #### [ Version 2.1] ( #tab/version-2 )
88
+
64
89
``` javascript
65
90
" use strict" ;
66
91
67
92
const { TextAnalyticsClient , CognitiveServicesCredential } = require (" @azure/cognitiveservices-textanalytics" );
68
93
```
94
+ ---
69
95
70
96
Create variables for your resource's Azure endpoint and key.
71
97
@@ -100,7 +126,7 @@ The response object is a list containing the analysis information for each docum
100
126
Create a new ` TextAnalyticsClient ` object with your key and endpoint as parameters.
101
127
102
128
``` javascript
103
- const client = new TextAnalyticsClient (endpoint, new CognitiveServicesCredential (key));
129
+ const textAnalyticsClient = new TextAnalyticsClient (endpoint, new TextAnalyticsApiKeyCredential (key));
104
130
```
105
131
106
132
#### [ Version 2.1] ( #tab/version-2 )
@@ -122,19 +148,19 @@ async function sentimentAnalysis(client){
122
148
123
149
const sentimentInput = [
124
150
" I had the best day of my life. I wish you were there with me."
125
- ]
126
-
151
+ ];
127
152
const sentimentResult = await client .analyzeSentiment (sentimentInput);
128
- result .forEach (document => {
153
+
154
+ sentimentResult .forEach (document => {
129
155
console .log (` ID: ${ document .id } ` );
130
156
console .log (` \t Document Sentiment: ${ document .sentiment } ` );
131
157
console .log (` \t Document Scores:` );
132
- console .log (` \t\t Positive: ${ document .documentScores .positive .toFixed (2 )} \t Negative: ${ document .documentScores .negative .toFixed (2 )} \t Neutral: ${ document .documentScores .neutral .toFixed (2 )} ` );
158
+ console .log (` \t\t Positive: ${ document .sentimentScores .positive .toFixed (2 )} \t Negative: ${ document .sentimentScores .negative .toFixed (2 )} \t Neutral: ${ document .sentimentScores .neutral .toFixed (2 )} ` );
133
159
console .log (` \t Sentences Sentiment(${ document .sentences .length } ):` );
134
160
document .sentences .forEach (sentence => {
135
161
console .log (` \t\t Sentence sentiment: ${ sentence .sentiment } ` )
136
162
console .log (` \t\t Sentences Scores:` );
137
- console .log (` \t\t Positive: ${ sentence .sentenceScores .positive .toFixed (2 )} \t Negative: ${ sentence .sentenceScores .negative .toFixed (2 )} \t Neutral: ${ sentence .sentenceScores .neutral .toFixed (2 )} ` );
163
+ console .log (` \t\t Positive: ${ sentence .sentimentScores .positive .toFixed (2 )} \t Negative: ${ sentence .sentimentScores .negative .toFixed (2 )} \t Neutral: ${ sentence .sentimentScores .neutral .toFixed (2 )} ` );
138
164
console .log (` \t\t Length: ${ sentence .length } , Offset: ${ sentence .offset } ` );
139
165
})
140
166
});
@@ -150,7 +176,7 @@ Run your code with `node index.js` in your console window.
150
176
ID: 0
151
177
Document Sentiment: positive
152
178
Document Scores:
153
- Positive: 0.61 Negative: 0.01 Neutral: 0.39
179
+ Positive: 1.00 Negative: 0.00 Neutral: 0.00
154
180
Sentences Sentiment(2):
155
181
Sentence sentiment: positive
156
182
Sentences Scores:
@@ -185,22 +211,18 @@ Run your code with `node index.js` in your console window.
185
211
186
212
#### [ Version 3.0-preview] ( #tab/version-3 )
187
213
188
- Create an array of strings containing the document you want to analyze. Call the client's ` detectLanguages ()` method and get the returned ` DetectLanguageResult ` . Then iterate through the results, and print each document's ID, with respective primary and detected language.
214
+ Create an array of strings containing the document you want to analyze. Call the client's ` detectLanguage ()` method and get the returned ` DetectLanguageResultCollection ` . Then iterate through the results, and print each document's ID with respective primary language.
189
215
190
216
``` javascript
191
217
async function languageDetection (client ) {
192
218
193
219
const languageInputArray = [
194
220
" Ce document est rédigé en Français."
195
- ]
196
-
197
- const languageResult = await client .detectLanguages (languageInputArray);
221
+ ];
222
+ const languageResult = await client .detectLanguage (languageInputArray);
198
223
199
- result .forEach (document => {
224
+ languageResult .forEach (document => {
200
225
console .log (` ID: ${ document .id } ` );
201
- document .detectedLanguages .forEach (language =>
202
- console .log (` \t Detected Language ${ language .name } ` )
203
- );
204
226
console .log (` \t Primary Language ${ document .primaryLanguage .name } ` )
205
227
});
206
228
}
@@ -213,7 +235,6 @@ Run your code with `node index.js` in your console window.
213
235
214
236
``` console
215
237
ID: 0
216
- Detected Language French
217
238
Primary Language French
218
239
```
219
240
@@ -253,13 +274,12 @@ async function entityRecognition(client){
253
274
" Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800" ,
254
275
" La sede principal de Microsoft se encuentra en la ciudad de Redmond, a 21 kilómetros de Seattle."
255
276
];
256
-
257
277
const entityResults = await client .recognizeEntities (entityInputs);
258
278
259
279
entityResults .forEach (document => {
260
280
console .log (` Document ID: ${ document .id } ` );
261
281
document .entities .forEach (entity => {
262
- console .log (` \t Name: ${ entity .text } \t Type : ${ entity .type } \t Sub Type : ${ entity .subtype != " " ? entity .subtype : " N/A" } ` );
282
+ console .log (` \t Name: ${ entity .text } \t Category : ${ entity .category } \t Subcategory : ${ entity .subCategory ? entity .subCategory : " N/A" } ` );
263
283
console .log (` \t Offset: ${ entity .offset } , Length: ${ entity .length } \t Score: ${ entity .score } ` );
264
284
});
265
285
});
@@ -273,23 +293,26 @@ Run your code with `node index.js` in your console window.
273
293
274
294
``` console
275
295
Document ID: 0
276
- Name: Microsoft Type : Organization Sub Type : N/A
296
+ Name: Microsoft Category : Organization Subcategory : N/A
277
297
Offset: 0, Length: 9 Score: 1
278
- Name: Bill Gates Type : Person Sub Type : N/A
279
- Offset: 25, Length: 10 Score: 0.999786376953125
280
- Name: Paul Allen Type : Person Sub Type : N/A
281
- Offset: 40, Length: 10 Score: 0.9988105297088623
282
- Name: April 4, 1975 Type : DateTime Sub Type : Date
298
+ Name: Bill Gates Category : Person Subcategory : N/A
299
+ Offset: 25, Length: 10 Score: 0.67
300
+ Name: Paul Allen Category : Person Subcategory : N/A
301
+ Offset: 40, Length: 10 Score: 0.81
302
+ Name: April 4, 1975 Category : DateTime Subcategory : Date
283
303
Offset: 54, Length: 13 Score: 0.8
284
- Name: Altair Type: Organization Sub Type : N/A
285
- Offset: 116 , Length: 6 Score: 0.7996330857276917
286
- Name: 8800 Type : Quantity Sub Type : Number
304
+ Name: interpreters Category: PersonType Subcategory : N/A
305
+ Offset: 95 , Length: 12 Score: 0.6
306
+ Name: 8800 Category : Quantity Subcategory : Number
287
307
Offset: 123, Length: 4 Score: 0.8
288
308
Document ID: 1
289
- Name: Microsoft Type: Organization Sub Type: N/A
290
- Offset: 21, Length: 9 Score: 0.9837456345558167
291
- Name: 21 Type: Quantity Sub Type: Number
309
+ Name: Microsoft Category: Organization Subcategory: N/A
310
+ Offset: 21, Length: 9 Score: 0.96
311
+ Name: Redmond Category: Location Subcategory: GPE
312
+ Offset: 60, Length: 7 Score: 0.09
313
+ Name: 21 Category: Quantity Subcategory: Number
292
314
Offset: 71, Length: 2 Score: 0.8
315
+ Name: Seattle Category: Location Subcategory: GPE
293
316
```
294
317
295
318
## Using NER to detect personal information
@@ -302,13 +325,13 @@ async function entityPiiRecognition(client){
302
325
303
326
const entityPiiInput = [
304
327
" Insurance policy for SSN on file 123-12-1234 is here by approved."
305
- ]
306
- const entityResults = await client .recognizePiiEntities (entityPiiInput);
328
+ ];
329
+ const entityPiiResults = await client .recognizePiiEntities (entityPiiInput);
307
330
308
- result .forEach (document => {
331
+ entityPiiResults .forEach (document => {
309
332
console .log (` Document ID: ${ document .id } ` );
310
333
document .entities .forEach (entity => {
311
- console .log (` \t Name: ${ entity .text } \t Type : ${ entity .type } \t Sub Type : ${ entity .subtype != " " ? entity .subtype : " N/A" } ` );
334
+ console .log (` \t Name: ${ entity .text } \t Category : ${ entity .category } \t Subcategory : ${ entity .subCategory ? entity .subCategory : " N/A" } ` );
312
335
console .log (` \t Offset: ${ entity .offset } , Length: ${ entity .length } \t Score: ${ entity .score } ` );
313
336
});
314
337
});
@@ -322,7 +345,7 @@ Run your code with `node index.js` in your console window.
322
345
323
346
``` console
324
347
Document ID: 0
325
- Name: 123-12-1234 Type : U.S. Social Security Number (SSN) Sub Type : N/A
348
+ Name: 123-12-1234 Category : U.S. Social Security Number (SSN) Subcategory : N/A
326
349
Offset: 33, Length: 11 Score: 0.85
327
350
```
328
351
@@ -335,7 +358,7 @@ async function linkedEntityRecognition(client){
335
358
336
359
const linkedEntityInput = [
337
360
" Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800. During his career at Microsoft, Gates held the positions of chairman, chief executive officer, president and chief software architect, while also being the largest individual shareholder until May 2014."
338
- ]
361
+ ];
339
362
const entityResults = await client .recognizeLinkedEntities (linkedEntityInput);
340
363
341
364
entityResults .forEach (document => {
@@ -346,7 +369,7 @@ async function linkedEntityRecognition(client){
346
369
entity .matches .forEach (match => {
347
370
console .log (` \t\t Text: ${ match .text } ` );
348
371
console .log (` \t\t Offset: ${ match .offset } , Length: ${ match .length } \t Score: ${ match .score .toFixed (3 )} ` );
349
- })
372
+ });
350
373
});
351
374
});
352
375
}
@@ -362,31 +385,31 @@ Document ID: 0
362
385
Name: Altair 8800 ID: Altair 8800 URL: https://en.wikipedia.org/wiki/Altair_8800 Data Source: Wikipedia
363
386
Matches:
364
387
Text: Altair 8800
365
- Offset: 116, Length: 11 Score: 0.650
388
+ Offset: 116, Length: 11 Score: 0.777
366
389
Name: Bill Gates ID: Bill Gates URL: https://en.wikipedia.org/wiki/Bill_Gates Data Source: Wikipedia
367
390
Matches:
368
391
Text: Bill Gates
369
- Offset: 25, Length: 10 Score: 0.243
392
+ Offset: 25, Length: 10 Score: 0.555
370
393
Text: Gates
371
- Offset: 161, Length: 5 Score: 0.243
394
+ Offset: 161, Length: 5 Score: 0.555
372
395
Name: Paul Allen ID: Paul Allen URL: https://en.wikipedia.org/wiki/Paul_Allen Data Source: Wikipedia
373
396
Matches:
374
397
Text: Paul Allen
375
- Offset: 40, Length: 10 Score: 0.174
398
+ Offset: 40, Length: 10 Score: 0.533
376
399
Name: Microsoft ID: Microsoft URL: https://en.wikipedia.org/wiki/Microsoft Data Source: Wikipedia
377
400
Matches:
378
401
Text: Microsoft
379
- Offset: 0, Length: 9 Score: 0.196
402
+ Offset: 0, Length: 9 Score: 0.469
380
403
Text: Microsoft
381
- Offset: 150, Length: 9 Score: 0.196
404
+ Offset: 150, Length: 9 Score: 0.469
382
405
Name: April 4 ID: April 4 URL: https://en.wikipedia.org/wiki/April_4 Data Source: Wikipedia
383
406
Matches:
384
407
Text: April 4
385
- Offset: 54, Length: 7 Score: 0.137
408
+ Offset: 54, Length: 7 Score: 0.248
386
409
Name: BASIC ID: BASIC URL: https://en.wikipedia.org/wiki/BASIC Data Source: Wikipedia
387
410
Matches:
388
411
Text: BASIC
389
- Offset: 89, Length: 5 Score: 0.052
412
+ Offset: 89, Length: 5 Score: 0.281
390
413
```
391
414
392
415
#### [ Version 2.1] ( #tab/version-2 )
@@ -443,12 +466,10 @@ async function keyPhraseExtraction(client){
443
466
444
467
const keyPhrasesInput = [
445
468
" My cat might need to see a veterinarian." ,
446
- ]
447
-
448
- const result = await client .extractKeyPhrases (keyPhrasesInput)
449
-
450
-
451
- result .forEach (document => {
469
+ ];
470
+ const keyPhraseResult = await client .extractKeyPhrases (keyPhrasesInput);
471
+
472
+ keyPhraseResult .forEach (document => {
452
473
console .log (` ID: ${ document .id } ` );
453
474
console .log (` \t Document Key Phrases: ${ document .keyPhrases } ` );
454
475
});
0 commit comments