Skip to content

Commit 55049c8

Browse files
authored
Merge pull request #110681 from ctufts/chtufts/pythonQS1.0.0b4
Chtufts/python qs1.0.0b4
2 parents 2683df8 + 35456c8 commit 55049c8

File tree

1 file changed

+33
-64
lines changed
  • articles/cognitive-services/text-analytics/includes/quickstarts

1 file changed

+33
-64
lines changed

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

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: aahill
33
ms.service: cognitive-services
44
ms.topic: include
5-
ms.date: 03/24/2020
5+
ms.date: 04/8/2020
66
ms.author: aahi
77
---
88

@@ -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
@@ -131,8 +132,8 @@ Create a new function called `sentiment_analysis_example()` that takes the clien
131132
```python
132133
def sentiment_analysis_example(client):
133134

134-
document = ["I had the best day of my life. I wish you were there with me."]
135-
response = client.analyze_sentiment(inputs=document)[0]
135+
documents = ["I had the best day of my life. I wish you were there with me."]
136+
response = client.analyze_sentiment(documents = documents)[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
@@ -202,8 +202,8 @@ Create a new function called `language_detection_example()` that takes the clien
202202
```python
203203
def language_detection_example(client):
204204
try:
205-
document = ["Ce document est rédigé en Français."]
206-
response = client.detect_language(inputs = document, country_hint = 'us')[0]
205+
documents = ["Ce document est rédigé en Français."]
206+
response = client.detect_language(documents = documents, country_hint = 'us')[0]
207207
print("Language: ", response.primary_language.name)
208208

209209
except Exception as err:
@@ -240,8 +240,7 @@ Document ID: 3 , Language: Chinese_Simplified
240240
#### [Version 3.0-preview](#tab/version-3)
241241

242242
> [!NOTE]
243-
> In version `3.0-preview`:
244-
> * NER includes separate methods for detecting personal information.
243+
> In version `3.0-preview`:
245244
> * Entity linking is a separate request than NER.
246245
247246
Create a new function called `entity_recognition_example` that takes the client as an argument, then calls the `recognize_entities()` function and iterates through the results. The returned response object will contain the list of detected entities in `entity` if successful, and an `error` if not. For each detected entity, print its Category and Sub-Category if exists.
@@ -250,13 +249,13 @@ Create a new function called `entity_recognition_example` that takes the client
250249
def entity_recognition_example(client):
251250

252251
try:
253-
document = ["I had a wonderful trip to Seattle last week."]
254-
result = client.recognize_entities(inputs= document)[0]
252+
documents = ["I had a wonderful trip to Seattle last week."]
253+
result = client.recognize_entities(documents = documents)[0]
255254

256255
print("Named Entities:\n")
257256
for entity in result.entities:
258257
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")
258+
"\n\tLength: \t", entity.grapheme_length, "\tConfidence Score: \t", round(entity.confidence_score, 2), "\n")
260259

261260
except Exception as err:
262261
print("Encountered exception. {}".format(err))
@@ -275,35 +274,6 @@ Named Entities:
275274
Length: 9 Confidence Score: 0.8
276275
```
277276

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-
307277
## Entity Linking
308278

309279
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.
@@ -312,12 +282,12 @@ Create a new function called `entity_linking_example()` that takes the client as
312282
def entity_linking_example(client):
313283

314284
try:
315-
document = ["""Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975,
285+
documents = ["""Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975,
316286
to develop and sell BASIC interpreters for the Altair 8800.
317287
During his career at Microsoft, Gates held the positions of chairman,
318288
chief executive officer, president and chief software architect,
319289
while also being the largest individual shareholder until May 2014."""]
320-
result = client.recognize_linked_entities(inputs= document)[0]
290+
result = client.recognize_linked_entities(documents = documents)[0]
321291

322292
print("Linked Entities:\n")
323293
for entity in result.entities:
@@ -326,7 +296,7 @@ def entity_linking_example(client):
326296
print("\tMatches:")
327297
for match in entity.matches:
328298
print("\t\tText:", match.text)
329-
print("\t\tScore: {0:.2f}".format(match.score), "\tLength: {}\n".format(match.grapheme_length))
299+
print("\t\tConfidence Score: {0:.2f}".format(match.confidence_score), "\tLength: {}\n".format(match.grapheme_length))
330300

331301
except Exception as err:
332302
print("Encountered exception. {}".format(err))
@@ -338,48 +308,47 @@ entity_linking_example(client)
338308
```console
339309
Linked Entities:
340310

341-
Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
311+
Name: Altair 8800 Id: Altair 8800 Url: https://en.wikipedia.org/wiki/Altair_8800
342312
Data Source: Wikipedia
343313
Matches:
344314
Text: Altair 8800
345-
Score: 0.78 Length: 11
315+
Confidence Score: 0.00 Length: 11
346316

347-
Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
317+
Name: Bill Gates Id: Bill Gates Url: https://en.wikipedia.org/wiki/Bill_Gates
348318
Data Source: Wikipedia
349319
Matches:
350320
Text: Bill Gates
351-
Score: 0.55 Length: 10
321+
Confidence Score: 0.00 Length: 10
352322

353323
Text: Gates
354-
Score: 0.55 Length: 5
324+
Confidence Score: 0.00 Length: 5
355325

356-
Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
326+
Name: Paul Allen Id: Paul Allen Url: https://en.wikipedia.org/wiki/Paul_Allen
357327
Data Source: Wikipedia
358328
Matches:
359329
Text: Paul Allen
360-
Score: 0.53 Length: 10
330+
Confidence Score: 0.00 Length: 10
361331

362-
Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
332+
Name: Microsoft Id: Microsoft Url: https://en.wikipedia.org/wiki/Microsoft
363333
Data Source: Wikipedia
364334
Matches:
365335
Text: Microsoft
366-
Score: 0.47 Length: 9
336+
Confidence Score: 0.00 Length: 9
367337

368338
Text: Microsoft
369-
Score: 0.47 Length: 9
339+
Confidence Score: 0.00 Length: 9
370340

371-
Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
341+
Name: April 4 Id: April 4 Url: https://en.wikipedia.org/wiki/April_4
372342
Data Source: Wikipedia
373343
Matches:
374344
Text: April 4
375-
Score: 0.25 Length: 7
345+
Confidence Score: 0.00 Length: 7
376346

377-
Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
347+
Name: BASIC Id: BASIC Url: https://en.wikipedia.org/wiki/BASIC
378348
Data Source: Wikipedia
379349
Matches:
380350
Text: BASIC
381-
Score: 0.28 Length: 5
382-
351+
Confidence Score: 0.00 Length: 5
383352
```
384353

385354
#### [Version 2.1](#tab/version-2)
@@ -443,9 +412,9 @@ Create a new function called `key_phrase_extraction_example()` that takes the cl
443412
def key_phrase_extraction_example(client):
444413

445414
try:
446-
document = ["My cat might need to see a veterinarian."]
415+
documents = ["My cat might need to see a veterinarian."]
447416

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

450419
if not response.is_error:
451420
print("\tKey Phrases:")

0 commit comments

Comments
 (0)