@@ -100,10 +100,11 @@ These code snippets show you how to do the following tasks with the Text Analyti
100
100
Create a function to instantiate the ` TextAnalyticsClient ` object with your ` key ` AND ` endpoint ` created above. Then create a new client.
101
101
102
102
``` python
103
- from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
103
+ from azure.ai.textanalytics import TextAnalyticsClient
104
+ from azure.core.credentials import AzureKeyCredential
104
105
105
106
def authenticate_client ():
106
- ta_credential = TextAnalyticsApiKeyCredential (key)
107
+ ta_credential = AzureKeyCredential (key)
107
108
text_analytics_client = TextAnalyticsClient(
108
109
endpoint = endpoint, credential = ta_credential)
109
110
return text_analytics_client
@@ -132,7 +133,7 @@ Create a new function called `sentiment_analysis_example()` that takes the clien
132
133
def sentiment_analysis_example (client ):
133
134
134
135
document = [" I had the best day of my life. I wish you were there with me." ]
135
- response = client.analyze_sentiment(inputs = document)[0 ]
136
+ response = client.analyze_sentiment(documents = document)[0 ]
136
137
print (" Document Sentiment: {} " .format(response.sentiment))
137
138
print (" Overall scores: positive={0:.2f } ; neutral={1:.2f } ; negative={2:.2f } \n " .format(
138
139
response.confidence_scores.positive,
@@ -147,16 +148,15 @@ def sentiment_analysis_example(client):
147
148
sentence.confidence_scores.neutral,
148
149
sentence.confidence_scores.negative,
149
150
))
150
-
151
-
151
+
152
152
sentiment_analysis_example(client)
153
153
```
154
154
155
155
### Output
156
156
157
157
``` console
158
158
Document Sentiment: positive
159
- Overall scores: positive=1.00; neutral=0.00; negative=0.00
159
+ Overall scores: positive=1.00; neutral=0.00; negative=0.00
160
160
161
161
[Length: 30]
162
162
Sentence 1 sentiment: positive
@@ -203,7 +203,7 @@ Create a new function called `language_detection_example()` that takes the clien
203
203
def language_detection_example (client ):
204
204
try :
205
205
document = [" Ce document est rédigé en Français." ]
206
- response = client.detect_language(inputs = document, country_hint = ' us' )[0 ]
206
+ response = client.detect_language(documents = document, country_hint = ' us' )[0 ]
207
207
print (" Language: " , response.primary_language.name)
208
208
209
209
except Exception as err:
@@ -251,12 +251,12 @@ def entity_recognition_example(client):
251
251
252
252
try :
253
253
document = [" I had a wonderful trip to Seattle last week." ]
254
- result = client.recognize_entities(inputs = document)[0 ]
254
+ result = client.recognize_entities(documents = document)[0 ]
255
255
256
256
print (" Named Entities:\n " )
257
257
for entity in result.entities:
258
258
print (" \t Text: \t " , entity.text, " \t Category: \t " , entity.category, " \t SubCategory: \t " , entity.subcategory,
259
- " \n\t Length: \t " , entity.grapheme_length, " \t Confidence Score: \t " , round (entity.score , 2 ), " \n " )
259
+ " \n\t Length: \t " , entity.grapheme_length, " \t Confidence Score: \t " , round (entity.confidence_score , 2 ), " \n " )
260
260
261
261
except Exception as err:
262
262
print (" Encountered exception. {} " .format(err))
@@ -275,35 +275,6 @@ Named Entities:
275
275
Length: 9 Confidence Score: 0.8
276
276
```
277
277
278
- ## Using NER to detect personal information
279
-
280
- Create a new function called ` entity_pii_example() ` that takes the client as an argument, then calls the ` recognize_pii_entities() ` function and gets the result. Then iterate through the results and print the entities.
281
-
282
- ``` python
283
- def entity_pii_example (client ):
284
-
285
- document = [" Insurance policy for SSN on file 123-12-1234 is here by approved." ]
286
-
287
-
288
- result = client.recognize_pii_entities(inputs = document)[0 ]
289
-
290
- print (" Personally Identifiable Information Entities: " )
291
- for entity in result.entities:
292
- print (" \t Text: " ,entity.text," \t Category: " , entity.category," \t SubCategory: " , entity.subcategory)
293
- print (" \t\t Length: " , entity.grapheme_length, " \t Score: {0:.2f } " .format(entity.score), " \n " )
294
-
295
- entity_pii_example(client)
296
- ```
297
-
298
- ### Output
299
-
300
- ``` console
301
- Personally Identifiable Information Entities:
302
- Text: 123-12-1234 Category: U.S. Social Security Number (SSN) SubCategory: None
303
- Length: 11 Score: 0.85
304
- ```
305
-
306
-
307
278
## Entity Linking
308
279
309
280
Create a new function called ` entity_linking_example() ` that takes the client as an argument, then calls the ` recognize_linked_entities() ` function and iterates through the results. The returned response object will contain the list of detected entities in ` entities ` if successful, and an ` error ` if not. Since linked entities are uniquely identified, occurrences of the same entity are grouped under a ` entity ` object as a list of ` match ` objects.
@@ -317,7 +288,7 @@ def entity_linking_example(client):
317
288
During his career at Microsoft, Gates held the positions of chairman,
318
289
chief executive officer, president and chief software architect,
319
290
while also being the largest individual shareholder until May 2014.""" ]
320
- result = client.recognize_linked_entities(inputs = document)[0 ]
291
+ result = client.recognize_linked_entities(documents = document)[0 ]
321
292
322
293
print (" Linked Entities:\n " )
323
294
for entity in result.entities:
@@ -326,7 +297,7 @@ def entity_linking_example(client):
326
297
print (" \t Matches:" )
327
298
for match in entity.matches:
328
299
print (" \t\t Text:" , match.text)
329
- print (" \t\t Score : {0:.2f } " .format(match.score ), " \t Length: {} \n " .format(match.grapheme_length))
300
+ print (" \t\t Confidence Score : {0:.2f } " .format(match.confidence_score ), " \t Length: {} \n " .format(match.grapheme_length))
330
301
331
302
except Exception as err:
332
303
print (" Encountered exception. {} " .format(err))
@@ -338,48 +309,47 @@ entity_linking_example(client)
338
309
``` console
339
310
Linked Entities:
340
311
341
- Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
312
+ Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
342
313
Data Source: Wikipedia
343
314
Matches:
344
315
Text: Altair 8800
345
- Score: 0.78 Length: 11
316
+ Confidence Score: 0.00 Length: 11
346
317
347
- Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
318
+ Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
348
319
Data Source: Wikipedia
349
320
Matches:
350
321
Text: Bill Gates
351
- Score: 0.55 Length: 10
322
+ Confidence Score: 0.00 Length: 10
352
323
353
324
Text: Gates
354
- Score: 0.55 Length: 5
325
+ Confidence Score: 0.00 Length: 5
355
326
356
- Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
327
+ Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
357
328
Data Source: Wikipedia
358
329
Matches:
359
330
Text: Paul Allen
360
- Score: 0.53 Length: 10
331
+ Confidence Score: 0.00 Length: 10
361
332
362
- Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
333
+ Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
363
334
Data Source: Wikipedia
364
335
Matches:
365
336
Text: Microsoft
366
- Score: 0.47 Length: 9
337
+ Confidence Score: 0.00 Length: 9
367
338
368
339
Text: Microsoft
369
- Score: 0.47 Length: 9
340
+ Confidence Score: 0.00 Length: 9
370
341
371
- Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
342
+ Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
372
343
Data Source: Wikipedia
373
344
Matches:
374
345
Text: April 4
375
- Score: 0.25 Length: 7
346
+ Confidence Score: 0.00 Length: 7
376
347
377
- Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
348
+ Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
378
349
Data Source: Wikipedia
379
350
Matches:
380
351
Text: BASIC
381
- Score: 0.28 Length: 5
382
-
352
+ Confidence Score: 0.00 Length: 5
383
353
```
384
354
385
355
#### [ Version 2.1] ( #tab/version-2 )
@@ -445,7 +415,7 @@ def key_phrase_extraction_example(client):
445
415
try :
446
416
document = [" My cat might need to see a veterinarian." ]
447
417
448
- response = client.extract_key_phrases(inputs = document)[0 ]
418
+ response = client.extract_key_phrases(documents = document)[0 ]
449
419
450
420
if not response.is_error:
451
421
print (" \t Key Phrases:" )
0 commit comments