Skip to content

Commit 407d0b7

Browse files
[text analytics] change return type of analyze actions to list of list (#18994)
* change return type of analyze actions to list of list * address johan's and krista's comments * limit # of action calls in readme example Co-authored-by: Krista Pratico <[email protected]>
1 parent bc2aeac commit 407d0b7

File tree

14 files changed

+609
-768
lines changed

14 files changed

+609
-768
lines changed

sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## 5.1.0b8 (Unreleased)
44

5+
**Breaking Changes**
6+
7+
- Changed the response structure of `being_analyze_actions`. Now, we return a list of results, where each result is a list of the action results for the document, in the order the documents and actions were passed
8+
- Removed `AnalyzeActionsType`
9+
- Removed `AnalyzeActionsResult`
10+
- Removed `AnalyzeActionsError`
511

612
## 5.1.0b7 (2021-05-18)
713

sdk/textanalytics/azure-ai-textanalytics/README.md

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,7 @@ from azure.core.credentials import AzureKeyCredential
506506
from azure.ai.textanalytics import (
507507
TextAnalyticsClient,
508508
RecognizeEntitiesAction,
509-
RecognizePiiEntitiesAction,
510-
ExtractKeyPhrasesAction,
511-
RecognizeLinkedEntitiesAction,
512-
AnalyzeSentimentAction
509+
AnalyzeSentimentAction,
513510
)
514511

515512
credential = AzureKeyCredential("<api_key>")
@@ -524,81 +521,39 @@ poller = text_analytics_client.begin_analyze_actions(
524521
display_name="Sample Text Analysis",
525522
actions=[
526523
RecognizeEntitiesAction(),
527-
RecognizePiiEntitiesAction(),
528-
ExtractKeyPhrasesAction(),
529-
RecognizeLinkedEntitiesAction(),
530524
AnalyzeSentimentAction()
531525
]
532526
)
533527

534528
# returns multiple actions results in the same order as the inputted actions
535-
result = poller.result()
536-
537-
first_action_result = next(result)
538-
print("Results of Entities Recognition action:")
539-
docs = [doc for doc in first_action_result.document_results if not doc.is_error]
540-
541-
for idx, doc in enumerate(docs):
542-
print("\nDocument text: {}".format(documents[idx]))
543-
for entity in doc.entities:
544-
print("Entity: {}".format(entity.text))
545-
print("...Category: {}".format(entity.category))
546-
print("...Confidence Score: {}".format(entity.confidence_score))
547-
print("...Offset: {}".format(entity.offset))
548-
print("------------------------------------------")
549-
550-
second_action_result = next(result)
551-
print("Results of PII Entities Recognition action:")
552-
docs = [doc for doc in second_action_result.document_results if not doc.is_error]
553-
554-
for idx, doc in enumerate(docs):
555-
print("Document text: {}".format(documents[idx]))
556-
for entity in doc.entities:
557-
print("Entity: {}".format(entity.text))
558-
print("Category: {}".format(entity.category))
559-
print("Confidence Score: {}\n".format(entity.confidence_score))
560-
print("------------------------------------------")
561-
562-
third_action_result = next(result)
563-
print("Results of Key Phrase Extraction action:")
564-
docs = [doc for doc in third_action_result.document_results if not doc.is_error]
565-
566-
for idx, doc in enumerate(docs):
567-
print("Document text: {}\n".format(documents[idx]))
568-
print("Key Phrases: {}\n".format(doc.key_phrases))
569-
print("------------------------------------------")
570-
571-
fourth_action_result = next(result)
572-
print("Results of Linked Entities Recognition action:")
573-
docs = [doc for doc in fourth_action_result.document_results if not doc.is_error]
574-
575-
for idx, doc in enumerate(docs):
576-
print("Document text: {}\n".format(documents[idx]))
577-
for linked_entity in doc.entities:
578-
print("Entity name: {}".format(linked_entity.name))
579-
print("...Data source: {}".format(linked_entity.data_source))
580-
print("...Data source language: {}".format(linked_entity.language))
581-
print("...Data source entity ID: {}".format(linked_entity.data_source_entity_id))
582-
print("...Data source URL: {}".format(linked_entity.url))
583-
print("...Document matches:")
584-
for match in linked_entity.matches:
585-
print("......Match text: {}".format(match.text))
586-
print(".........Confidence Score: {}".format(match.confidence_score))
587-
print(".........Offset: {}".format(match.offset))
588-
print(".........Length: {}".format(match.length))
589-
print("------------------------------------------")
590-
591-
fifth_action_result = next(result)
592-
print("Results of Sentiment Analysis action:")
593-
docs = [doc for doc in fifth_action_result.document_results if not doc.is_error]
594-
595-
for doc in docs:
596-
print("Overall sentiment: {}".format(doc.sentiment))
597-
print("Scores: positive={}; neutral={}; negative={} \n".format(
598-
doc.confidence_scores.positive,
599-
doc.confidence_scores.neutral,
600-
doc.confidence_scores.negative,
601-
))
529+
document_results = poller.result()
530+
for doc, action_results in zip(documents, document_results):
531+
recognize_entities_result, analyze_sentiment_result = action_results
532+
print("\nDocument text: {}".format(doc))
533+
print("...Results of Recognize Entities Action:")
534+
if recognize_entities_result.is_error:
535+
print("......Is an error with code '{}' and message '{}'".format(
536+
recognize_entities_result.code, recognize_entities_result.message
537+
))
538+
else:
539+
for entity in recognize_entities_result.entities:
540+
print("......Entity: {}".format(entity.text))
541+
print(".........Category: {}".format(entity.category))
542+
print(".........Confidence Score: {}".format(entity.confidence_score))
543+
print(".........Offset: {}".format(entity.offset))
544+
545+
print("...Results of Analyze Sentiment action:")
546+
if analyze_sentiment_result.is_error:
547+
print("......Is an error with code '{}' and message '{}'".format(
548+
analyze_sentiment_result.code, analyze_sentiment_result.message
549+
))
550+
else:
551+
print("......Overall sentiment: {}".format(analyze_sentiment_result.sentiment))
552+
print("......Scores: positive={}; neutral={}; negative={} \n".format(
553+
analyze_sentiment_result.confidence_scores.positive,
554+
analyze_sentiment_result.confidence_scores.neutral,
555+
analyze_sentiment_result.confidence_scores.negative,
556+
))
602557
print("------------------------------------------")
603558
```
604559

sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
RecognizeLinkedEntitiesAction,
4040
RecognizePiiEntitiesAction,
4141
ExtractKeyPhrasesAction,
42-
AnalyzeActionsResult,
43-
AnalyzeActionsType,
44-
AnalyzeActionsError,
42+
_AnalyzeActionsType,
4543
HealthcareEntityRelationRoleType,
4644
HealthcareRelation,
4745
HealthcareRelationRole,
@@ -91,9 +89,7 @@
9189
'RecognizeLinkedEntitiesAction',
9290
'RecognizePiiEntitiesAction',
9391
'ExtractKeyPhrasesAction',
94-
'AnalyzeActionsResult',
95-
'AnalyzeActionsType',
96-
"AnalyzeActionsError",
92+
'_AnalyzeActionsType',
9793
"PiiEntityCategoryType",
9894
"HealthcareEntityRelationType",
9995
"HealthcareEntityRelationRoleType",

sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_models.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ def __repr__(self):
13561356
.format(self.positive, self.neutral, self.negative)[:1024]
13571357

13581358

1359-
class AnalyzeActionsType(str, Enum):
1359+
class _AnalyzeActionsType(str, Enum):
13601360
"""The type of action that was applied to the documents
13611361
"""
13621362
RECOGNIZE_ENTITIES = "recognize_entities" #: Entities Recognition action.
@@ -1365,62 +1365,6 @@ class AnalyzeActionsType(str, Enum):
13651365
RECOGNIZE_LINKED_ENTITIES = "recognize_linked_entities" #: Linked Entities Recognition action.
13661366
ANALYZE_SENTIMENT = "analyze_sentiment" #: Sentiment Analysis action.
13671367

1368-
1369-
class AnalyzeActionsResult(DictMixin):
1370-
"""AnalyzeActionsResult contains the results of a recognize entities action
1371-
on a list of documents. Returned by `begin_analyze_actions`
1372-
1373-
:ivar document_results: A list of objects containing results for all Entity Recognition actions
1374-
included in the analysis.
1375-
:vartype document_results: list[~azure.ai.textanalytics.RecognizeEntitiesResult]
1376-
:ivar bool is_error: Boolean check for error item when iterating over list of
1377-
actions. Always False for an instance of a AnalyzeActionsResult.
1378-
:ivar action_type: The type of action this class is a result of.
1379-
:vartype action_type: str or ~azure.ai.textanalytics.AnalyzeActionsType
1380-
:ivar ~datetime.datetime completed_on: Date and time (UTC) when the result completed
1381-
on the service.
1382-
"""
1383-
def __init__(self, **kwargs):
1384-
self.document_results = kwargs.get("document_results")
1385-
self.is_error = False
1386-
self.action_type = kwargs.get("action_type")
1387-
self.completed_on = kwargs.get("completed_on")
1388-
1389-
def __repr__(self):
1390-
return "AnalyzeActionsResult(document_results={}, is_error={}, action_type={}, completed_on={})".format(
1391-
repr(self.document_results),
1392-
self.is_error,
1393-
self.action_type,
1394-
self.completed_on,
1395-
)[:1024]
1396-
1397-
1398-
class AnalyzeActionsError(DictMixin):
1399-
"""AnalyzeActionsError is an error object which represents an an
1400-
error response for an action.
1401-
1402-
:ivar error: The action result error.
1403-
:vartype error: ~azure.ai.textanalytics.TextAnalyticsError
1404-
:ivar bool is_error: Boolean check for error item when iterating over list of
1405-
results. Always True for an instance of a DocumentError.
1406-
"""
1407-
1408-
def __init__(self, **kwargs):
1409-
self.error = kwargs.get("error")
1410-
self.is_error = True
1411-
1412-
def __repr__(self):
1413-
return "AnalyzeActionsError(error={}, is_error={}".format(
1414-
repr(self.error), self.is_error
1415-
)
1416-
1417-
@classmethod
1418-
def _from_generated(cls, error):
1419-
return cls(
1420-
error=TextAnalyticsError(code=error.code, message=error.message, target=error.target)
1421-
)
1422-
1423-
14241368
class RecognizeEntitiesAction(DictMixin):
14251369
"""RecognizeEntitiesAction encapsulates the parameters for starting a long-running Entities Recognition operation.
14261370

sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_request_handlers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
RecognizePiiEntitiesAction,
1515
RecognizeLinkedEntitiesAction,
1616
AnalyzeSentimentAction,
17-
AnalyzeActionsType,
17+
_AnalyzeActionsType,
1818
)
1919

2020
def _validate_input(documents, hint, whole_input_hint):
@@ -71,14 +71,14 @@ def _validate_input(documents, hint, whole_input_hint):
7171

7272
def _determine_action_type(action):
7373
if isinstance(action, RecognizeEntitiesAction):
74-
return AnalyzeActionsType.RECOGNIZE_ENTITIES
74+
return _AnalyzeActionsType.RECOGNIZE_ENTITIES
7575
if isinstance(action, RecognizePiiEntitiesAction):
76-
return AnalyzeActionsType.RECOGNIZE_PII_ENTITIES
76+
return _AnalyzeActionsType.RECOGNIZE_PII_ENTITIES
7777
if isinstance(action, RecognizeLinkedEntitiesAction):
78-
return AnalyzeActionsType.RECOGNIZE_LINKED_ENTITIES
78+
return _AnalyzeActionsType.RECOGNIZE_LINKED_ENTITIES
7979
if isinstance(action, AnalyzeSentimentAction):
80-
return AnalyzeActionsType.ANALYZE_SENTIMENT
81-
return AnalyzeActionsType.EXTRACT_KEY_PHRASES
80+
return _AnalyzeActionsType.ANALYZE_SENTIMENT
81+
return _AnalyzeActionsType.EXTRACT_KEY_PHRASES
8282

8383
def _check_string_index_type_arg(string_index_type_arg, api_version, string_index_type_default="UnicodeCodePoint"):
8484
string_index_type = None

0 commit comments

Comments
 (0)