Skip to content

Commit 9d4d7dc

Browse files
author
Chris Tufts
committed
updated python quickstart with examples that work with the new sdk version
1 parent 777591f commit 9d4d7dc

File tree

1 file changed

+26
-56
lines changed
  • articles/cognitive-services/text-analytics/includes/quickstarts

1 file changed

+26
-56
lines changed

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

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ These code snippets show you how to do the following tasks with the Text Analyti
100100
Create a function to instantiate the `TextAnalyticsClient` object with your `key` AND `endpoint` created above. Then create a new client.
101101

102102
```python
103-
from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
103+
from azure.ai.textanalytics import TextAnalyticsClient
104+
from azure.core.credentials import AzureKeyCredential
104105

105106
def authenticate_client():
106-
ta_credential = TextAnalyticsApiKeyCredential(key)
107+
ta_credential = AzureKeyCredential(key)
107108
text_analytics_client = TextAnalyticsClient(
108109
endpoint=endpoint, credential=ta_credential)
109110
return text_analytics_client
@@ -132,7 +133,7 @@ Create a new function called `sentiment_analysis_example()` that takes the clien
132133
def sentiment_analysis_example(client):
133134

134135
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]
136137
print("Document Sentiment: {}".format(response.sentiment))
137138
print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format(
138139
response.confidence_scores.positive,
@@ -147,16 +148,15 @@ def sentiment_analysis_example(client):
147148
sentence.confidence_scores.neutral,
148149
sentence.confidence_scores.negative,
149150
))
150-
151-
151+
152152
sentiment_analysis_example(client)
153153
```
154154

155155
### Output
156156

157157
```console
158158
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
160160

161161
[Length: 30]
162162
Sentence 1 sentiment: positive
@@ -203,7 +203,7 @@ Create a new function called `language_detection_example()` that takes the clien
203203
def language_detection_example(client):
204204
try:
205205
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]
207207
print("Language: ", response.primary_language.name)
208208

209209
except Exception as err:
@@ -251,12 +251,12 @@ def entity_recognition_example(client):
251251

252252
try:
253253
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]
255255

256256
print("Named Entities:\n")
257257
for entity in result.entities:
258258
print("\tText: \t", entity.text, "\tCategory: \t", entity.category, "\tSubCategory: \t", entity.subcategory,
259-
"\n\tLength: \t", entity.grapheme_length, "\tConfidence Score: \t", round(entity.score, 2), "\n")
259+
"\n\tLength: \t", entity.grapheme_length, "\tConfidence Score: \t", round(entity.confidence_score, 2), "\n")
260260

261261
except Exception as err:
262262
print("Encountered exception. {}".format(err))
@@ -275,35 +275,6 @@ Named Entities:
275275
Length: 9 Confidence Score: 0.8
276276
```
277277

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("\tText: ",entity.text,"\tCategory: ", entity.category,"\tSubCategory: ", entity.subcategory)
293-
print("\t\tLength: ", entity.grapheme_length, "\tScore: {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-
307278
## Entity Linking
308279

309280
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):
317288
During his career at Microsoft, Gates held the positions of chairman,
318289
chief executive officer, president and chief software architect,
319290
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]
321292

322293
print("Linked Entities:\n")
323294
for entity in result.entities:
@@ -326,7 +297,7 @@ def entity_linking_example(client):
326297
print("\tMatches:")
327298
for match in entity.matches:
328299
print("\t\tText:", match.text)
329-
print("\t\tScore: {0:.2f}".format(match.score), "\tLength: {}\n".format(match.grapheme_length))
300+
print("\t\tConfidence Score: {0:.2f}".format(match.confidence_score), "\tLength: {}\n".format(match.grapheme_length))
330301

331302
except Exception as err:
332303
print("Encountered exception. {}".format(err))
@@ -338,48 +309,47 @@ entity_linking_example(client)
338309
```console
339310
Linked Entities:
340311

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
342313
Data Source: Wikipedia
343314
Matches:
344315
Text: Altair 8800
345-
Score: 0.78 Length: 11
316+
Confidence Score: 0.00 Length: 11
346317

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
348319
Data Source: Wikipedia
349320
Matches:
350321
Text: Bill Gates
351-
Score: 0.55 Length: 10
322+
Confidence Score: 0.00 Length: 10
352323

353324
Text: Gates
354-
Score: 0.55 Length: 5
325+
Confidence Score: 0.00 Length: 5
355326

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
357328
Data Source: Wikipedia
358329
Matches:
359330
Text: Paul Allen
360-
Score: 0.53 Length: 10
331+
Confidence Score: 0.00 Length: 10
361332

362-
Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
333+
Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
363334
Data Source: Wikipedia
364335
Matches:
365336
Text: Microsoft
366-
Score: 0.47 Length: 9
337+
Confidence Score: 0.00 Length: 9
367338

368339
Text: Microsoft
369-
Score: 0.47 Length: 9
340+
Confidence Score: 0.00 Length: 9
370341

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
372343
Data Source: Wikipedia
373344
Matches:
374345
Text: April 4
375-
Score: 0.25 Length: 7
346+
Confidence Score: 0.00 Length: 7
376347

377-
Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
348+
Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
378349
Data Source: Wikipedia
379350
Matches:
380351
Text: BASIC
381-
Score: 0.28 Length: 5
382-
352+
Confidence Score: 0.00 Length: 5
383353
```
384354

385355
#### [Version 2.1](#tab/version-2)
@@ -445,7 +415,7 @@ def key_phrase_extraction_example(client):
445415
try:
446416
document = ["My cat might need to see a veterinarian."]
447417

448-
response = client.extract_key_phrases(inputs= document)[0]
418+
response = client.extract_key_phrases(documents = document)[0]
449419

450420
if not response.is_error:
451421
print("\tKey Phrases:")

0 commit comments

Comments
 (0)