Skip to content

Commit e150397

Browse files
[textanalytics] add single label classify bespoke method (Azure#25005)
* initial work * docs,samples,linting * expose TA poller * add bespoke begin_single_label_classify * oops * missed rename of internal type
1 parent 8b8c9e7 commit e150397

22 files changed

+1232
-139
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features Added
66

77
- Added `begin_recognize_custom_entities` client method to recognize custom named entities in documents.
8+
- Added `begin_single_label_classify` client method to perform custom single label classification on documents.
89

910
### Breaking Changes
1011

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ The following section provides several code snippets covering some of the most c
235235
- [Healthcare Entities Analysis](#healthcare-entities-analysis "Healthcare Entities Analysis")
236236
- [Multiple Analysis](#multiple-analysis "Multiple analysis")
237237
- [Custom Entity Recognition][recognize_custom_entities_sample]
238-
- [Custom Single Category Classification][single_category_classify_sample]
238+
- [Custom Single Label Classification][single_label_classify_sample]
239239
- [Custom Multi Category Classification][multi_category_classify_sample]
240240

241241
### Analyze sentiment
@@ -514,7 +514,7 @@ Note: The Healthcare Entities Analysis service is only available in the Standard
514514
- Key Phrase Extraction
515515
- Sentiment Analysis
516516
- Custom Entity Recognition (see sample [here][recognize_custom_entities_sample])
517-
- Custom Single Category Classification (see sample [here][single_category_classify_sample])
517+
- Custom Single Label Classification (see sample [here][single_label_classify_sample])
518518
- Custom Multi Category Classification (see sample [here][multi_category_classify_sample])
519519
- Healthcare Entities Analysis
520520

@@ -646,7 +646,7 @@ Common scenarios
646646
- Healthcare Entities Analysis: [sample_analyze_healthcare_entities.py][analyze_healthcare_entities_sample] ([async version][analyze_healthcare_entities_sample_async])
647647
- Multiple Analysis: [sample_analyze_actions.py][analyze_sample] ([async version][analyze_sample_async])
648648
- Custom Entity Recognition: [sample_recognize_custom_entities.py][recognize_custom_entities_sample] ([async_version][recognize_custom_entities_sample_async])
649-
- Custom Single Classification: [sample_single_category_classify.py][single_category_classify_sample] ([async_version][single_category_classify_sample_async])
649+
- Custom Single Label Classification: [sample_single_label_classify.py][single_label_classify_sample] ([async_version][single_label_classify_sample_async])
650650
- Custom Multi Classification: [sample_multi_category_classify.py][multi_category_classify_sample] ([async_version][multi_category_classify_sample_async])
651651

652652
Advanced scenarios
@@ -745,8 +745,8 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
745745
[opinion_mining_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_with_opinion_mining_async.py
746746
[recognize_custom_entities_sample]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_custom_entities.py
747747
[recognize_custom_entities_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_custom_entities_async.py
748-
[single_category_classify_sample]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_single_category_classify.py
749-
[single_category_classify_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_single_category_classify_async.py
748+
[single_label_classify_sample]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_single_label_classify.py
749+
[single_label_classify_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_single_label_classify_async.py
750750
[multi_category_classify_sample]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_multi_category_classify.py
751751
[multi_category_classify_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_multi_category_classify_async.py
752752
[cla]: https://cla.microsoft.com

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
HealthcareEntityCategory,
5252
RecognizeCustomEntitiesAction,
5353
RecognizeCustomEntitiesResult,
54-
SingleCategoryClassifyAction,
54+
SingleLabelClassifyAction,
5555
MultiCategoryClassifyAction,
5656
ClassifyDocumentResult,
5757
ClassificationCategory,
@@ -109,7 +109,7 @@
109109
"HealthcareEntityCategory",
110110
"RecognizeCustomEntitiesAction",
111111
"RecognizeCustomEntitiesResult",
112-
"SingleCategoryClassifyAction",
112+
"SingleLabelClassifyAction",
113113
"MultiCategoryClassifyAction",
114114
"ClassifyDocumentResult",
115115
"ClassificationCategory",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ class _AnalyzeActionsType(str, Enum, metaclass=CaseInsensitiveEnumMeta):
17501750
)
17511751
ANALYZE_SENTIMENT = "analyze_sentiment" #: Sentiment Analysis action.
17521752
RECOGNIZE_CUSTOM_ENTITIES = "recognize_custom_entities"
1753-
SINGLE_CATEGORY_CLASSIFY = "single_category_classify"
1753+
SINGLE_LABEL_CLASSIFY = "single_label_classify"
17541754
MULTI_CATEGORY_CLASSIFY = "multi_category_classify"
17551755
ANALYZE_HEALTHCARE_ENTITIES = "analyze_healthcare_entities"
17561756

@@ -2375,8 +2375,8 @@ def _from_generated(cls, result):
23752375
)
23762376

23772377

2378-
class SingleCategoryClassifyAction(DictMixin):
2379-
"""SingleCategoryClassifyAction encapsulates the parameters for starting a long-running custom single category
2378+
class SingleLabelClassifyAction(DictMixin):
2379+
"""SingleLabelClassifyAction encapsulates the parameters for starting a long-running custom single label
23802380
classification operation. For information on regional support of custom features and how to train a model to
23812381
classify your documents, see https://aka.ms/azsdk/textanalytics/customfunctionalities
23822382
@@ -2413,7 +2413,7 @@ def __init__(
24132413
self.disable_service_logs = kwargs.get('disable_service_logs', None)
24142414

24152415
def __repr__(self):
2416-
return "SingleCategoryClassifyAction(project_name={}, deployment_name={}, " \
2416+
return "SingleLabelClassifyAction(project_name={}, deployment_name={}, " \
24172417
"disable_service_logs={})".format(
24182418
self.project_name,
24192419
self.deployment_name,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _determine_action_type(action): # pylint: disable=too-many-return-statement
9292
if action.__class__.__name__ == "CustomEntitiesLROTask":
9393
return _AnalyzeActionsType.RECOGNIZE_CUSTOM_ENTITIES
9494
if action.__class__.__name__ == "CustomSingleLabelClassificationLROTask":
95-
return _AnalyzeActionsType.SINGLE_CATEGORY_CLASSIFY
95+
return _AnalyzeActionsType.SINGLE_LABEL_CLASSIFY
9696
if action.__class__.__name__ == "CustomMultiLabelClassificationLROTask":
9797
return _AnalyzeActionsType.MULTI_CATEGORY_CLASSIFY
9898
if action.__class__.__name__ == "HealthcareLROTask":

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def _get_deserialization_callback_from_task_type(task_type): # pylint: disable=
306306
return sentiment_result
307307
if task_type == _AnalyzeActionsType.RECOGNIZE_CUSTOM_ENTITIES:
308308
return custom_entities_result
309-
if task_type == _AnalyzeActionsType.SINGLE_CATEGORY_CLASSIFY:
309+
if task_type == _AnalyzeActionsType.SINGLE_LABEL_CLASSIFY:
310310
return classify_document_result
311311
if task_type == _AnalyzeActionsType.MULTI_CATEGORY_CLASSIFY:
312312
return classify_document_result

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

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
AnalyzeHealthcareEntitiesResult,
6161
RecognizeCustomEntitiesAction,
6262
RecognizeCustomEntitiesResult,
63-
SingleCategoryClassifyAction,
63+
SingleLabelClassifyAction,
6464
MultiCategoryClassifyAction,
6565
ClassifyDocumentResult,
6666
AnalyzeHealthcareEntitiesAction,
@@ -1057,7 +1057,7 @@ def begin_analyze_actions(
10571057
ExtractKeyPhrasesAction,
10581058
AnalyzeSentimentAction,
10591059
RecognizeCustomEntitiesAction,
1060-
SingleCategoryClassifyAction,
1060+
SingleLabelClassifyAction,
10611061
MultiCategoryClassifyAction,
10621062
AnalyzeHealthcareEntitiesAction,
10631063
]
@@ -1102,7 +1102,7 @@ def begin_analyze_actions(
11021102
:type actions:
11031103
list[RecognizeEntitiesAction or RecognizePiiEntitiesAction or ExtractKeyPhrasesAction or
11041104
RecognizeLinkedEntitiesAction or AnalyzeSentimentAction or
1105-
RecognizeCustomEntitiesAction or SingleCategoryClassifyAction or
1105+
RecognizeCustomEntitiesAction or SingleLabelClassifyAction or
11061106
MultiCategoryClassifyAction or AnalyzeHealthcareEntitiesAction]
11071107
:keyword str display_name: An optional display name to set for the requested analysis.
11081108
:keyword str language: The 2 letter ISO 639-1 representation of language for the
@@ -1138,7 +1138,7 @@ def begin_analyze_actions(
11381138
.. versionadded:: v3.1
11391139
The *begin_analyze_actions* client method.
11401140
.. versionadded:: 2022-04-01-preview
1141-
The *RecognizeCustomEntitiesAction*, *SingleCategoryClassifyAction*,
1141+
The *RecognizeCustomEntitiesAction*, *SingleLabelClassifyAction*,
11421142
*MultiCategoryClassifyAction*, and *AnalyzeHealthcareEntitiesAction* input options and the
11431143
corresponding *RecognizeCustomEntitiesResult*, *ClassifyDocumentResult*,
11441144
and *AnalyzeHealthcareEntitiesResult* result objects
@@ -1404,3 +1404,115 @@ def begin_recognize_custom_entities(
14041404

14051405
except HttpResponseError as error:
14061406
return process_http_response_error(error)
1407+
1408+
@distributed_trace
1409+
@validate_multiapi_args(
1410+
version_method_added="2022-05-01"
1411+
)
1412+
def begin_single_label_classify(
1413+
self,
1414+
documents: Union[List[str], List[TextDocumentInput], List[Dict[str, str]]],
1415+
project_name,
1416+
deployment_name,
1417+
**kwargs: Any,
1418+
) -> TextAnalyticsLROPoller[ItemPaged[Union[ClassifyDocumentResult, DocumentError]]]:
1419+
"""Start a long-running custom single label classification operation.
1420+
1421+
For information on regional support of custom features and how to train a model to
1422+
classify your documents, see https://aka.ms/azsdk/textanalytics/customfunctionalities
1423+
1424+
:param documents: The set of documents to process as part of this batch.
1425+
If you wish to specify the ID and language on a per-item basis you must
1426+
use as input a list[:class:`~azure.ai.textanalytics.TextDocumentInput`] or a list of
1427+
dict representations of :class:`~azure.ai.textanalytics.TextDocumentInput`, like
1428+
`{"id": "1", "language": "en", "text": "hello world"}`.
1429+
:type documents:
1430+
list[str] or list[~azure.ai.textanalytics.TextDocumentInput] or list[dict[str, str]]
1431+
:param str project_name: Required. This field indicates the project name for the model.
1432+
:param str deployment_name: This field indicates the deployment name for the model.
1433+
:keyword str language: The 2 letter ISO 639-1 representation of language for the
1434+
entire batch. For example, use "en" for English; "es" for Spanish etc.
1435+
If not set, uses "en" for English as default. Per-document language will
1436+
take precedence over whole batch language. See https://aka.ms/talangs for
1437+
supported languages in Language API.
1438+
:keyword bool show_stats: If set to true, response will contain document level statistics.
1439+
:keyword bool disable_service_logs: If set to true, you opt-out of having your text input
1440+
logged on the service side for troubleshooting. By default, the Language service logs your
1441+
input text for 48 hours, solely to allow for troubleshooting issues in providing you with
1442+
the service's natural language processing functions. Setting this parameter to true,
1443+
disables input logging and may limit our ability to remediate issues that occur. Please see
1444+
Cognitive Services Compliance and Privacy notes at https://aka.ms/cs-compliance for
1445+
additional details, and Microsoft Responsible AI principles at
1446+
https://www.microsoft.com/ai/responsible-ai.
1447+
:keyword int polling_interval: Waiting time between two polls for LRO operations
1448+
if no Retry-After header is present. Defaults to 5 seconds.
1449+
:keyword str continuation_token:
1450+
Call `continuation_token()` on the poller object to save the long-running operation (LRO)
1451+
state into an opaque token. Pass the value as the `continuation_token` keyword argument
1452+
to restart the LRO from a saved state.
1453+
:keyword str display_name: An optional display name to set for the requested analysis.
1454+
:return: An instance of an TextAnalyticsLROPoller. Call `result()` on the this
1455+
object to return a heterogeneous pageable of
1456+
:class:`~azure.ai.textanalytics.ClassifyDocumentResult` and
1457+
:class:`~azure.ai.textanalytics.DocumentError`.
1458+
:rtype:
1459+
~azure.ai.textanalytics.TextAnalyticsLROPoller[~azure.core.paging.ItemPaged[
1460+
~azure.ai.textanalytics.ClassifyDocumentResult or ~azure.ai.textanalytics.DocumentError]]
1461+
:raises ~azure.core.exceptions.HttpResponseError:
1462+
1463+
.. versionadded:: 2022-05-01
1464+
The *begin_single_label_classify* client method.
1465+
1466+
.. admonition:: Example:
1467+
1468+
.. literalinclude:: ../samples/sample_single_label_classify.py
1469+
:start-after: [START single_label_classify]
1470+
:end-before: [END single_label_classify]
1471+
:language: python
1472+
:dedent: 4
1473+
:caption: Perform single label classification on a batch of documents.
1474+
"""
1475+
1476+
continuation_token = kwargs.pop("continuation_token", None)
1477+
disable_service_logs = kwargs.pop("disable_service_logs", None)
1478+
polling_interval = kwargs.pop("polling_interval", 5)
1479+
1480+
if continuation_token:
1481+
return cast(
1482+
TextAnalyticsLROPoller[ItemPaged[Union[ClassifyDocumentResult, DocumentError]]],
1483+
_get_result_from_continuation_token(
1484+
self._client._client, # pylint: disable=protected-access
1485+
continuation_token,
1486+
TextAnalyticsLROPoller,
1487+
AnalyzeActionsLROPollingMethod(
1488+
timeout=polling_interval,
1489+
**kwargs
1490+
),
1491+
self._analyze_result_callback,
1492+
bespoke=True
1493+
)
1494+
)
1495+
1496+
try:
1497+
return cast(
1498+
TextAnalyticsLROPoller[
1499+
ItemPaged[Union[ClassifyDocumentResult, DocumentError]]
1500+
],
1501+
self.begin_analyze_actions(
1502+
documents,
1503+
actions=[
1504+
SingleLabelClassifyAction(
1505+
project_name=project_name,
1506+
deployment_name=deployment_name,
1507+
disable_service_logs=disable_service_logs
1508+
)
1509+
],
1510+
polling_interval=polling_interval,
1511+
poller_cls=TextAnalyticsLROPoller,
1512+
bespoke=True,
1513+
**kwargs
1514+
)
1515+
)
1516+
1517+
except HttpResponseError as error:
1518+
return process_http_response_error(error)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def check_for_unsupported_actions_types(*args, **kwargs):
3030
"2022-05-01":
3131
[
3232
"RecognizeCustomEntitiesAction",
33-
"SingleCategoryClassifyAction",
33+
"SingleLabelClassifyAction",
3434
"MultiCategoryClassifyAction",
3535
"AnalyzeHealthcareEntitiesAction"
3636
]

0 commit comments

Comments
 (0)