azure-ai-textanalytics 5.3.0b1__py3-none-any.whl → 6.0.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of azure-ai-textanalytics might be problematic. Click here for more details.

Files changed (128) hide show
  1. azure/ai/textanalytics/__init__.py +26 -197
  2. azure/ai/textanalytics/_client.py +111 -0
  3. azure/ai/textanalytics/_configuration.py +73 -0
  4. azure/ai/textanalytics/{_generated/v2022_05_01/operations → _operations}/__init__.py +13 -8
  5. azure/ai/textanalytics/_operations/_operations.py +716 -0
  6. azure/ai/textanalytics/{_generated/v2022_05_01/models → _operations}/_patch.py +8 -6
  7. azure/ai/textanalytics/_patch.py +350 -0
  8. azure/ai/textanalytics/{_generated/aio → _utils}/__init__.py +1 -5
  9. azure/ai/textanalytics/_utils/model_base.py +1237 -0
  10. azure/ai/textanalytics/{_generated/_serialization.py → _utils/serialization.py} +640 -616
  11. azure/ai/textanalytics/{_generated/v2022_05_01/aio/_vendor.py → _utils/utils.py} +10 -12
  12. azure/ai/textanalytics/_version.py +8 -7
  13. azure/ai/textanalytics/aio/__init__.py +25 -14
  14. azure/ai/textanalytics/aio/_client.py +115 -0
  15. azure/ai/textanalytics/aio/_configuration.py +75 -0
  16. azure/ai/textanalytics/{_generated/v2022_10_01_preview/aio/operations → aio/_operations}/__init__.py +13 -8
  17. azure/ai/textanalytics/aio/_operations/_operations.py +623 -0
  18. azure/ai/textanalytics/{_generated/v2022_05_01 → aio/_operations}/_patch.py +8 -6
  19. azure/ai/textanalytics/aio/_patch.py +344 -0
  20. azure/ai/textanalytics/models/__init__.py +402 -0
  21. azure/ai/textanalytics/models/_enums.py +1979 -0
  22. azure/ai/textanalytics/models/_models.py +6641 -0
  23. azure/ai/textanalytics/{_generated/v2022_05_01/aio → models}/_patch.py +8 -6
  24. azure/ai/textanalytics/py.typed +1 -0
  25. {azure_ai_textanalytics-5.3.0b1.dist-info → azure_ai_textanalytics-6.0.0b1.dist-info}/METADATA +755 -319
  26. azure_ai_textanalytics-6.0.0b1.dist-info/RECORD +29 -0
  27. {azure_ai_textanalytics-5.3.0b1.dist-info → azure_ai_textanalytics-6.0.0b1.dist-info}/WHEEL +1 -1
  28. azure/ai/textanalytics/_base_client.py +0 -111
  29. azure/ai/textanalytics/_check.py +0 -22
  30. azure/ai/textanalytics/_dict_mixin.py +0 -54
  31. azure/ai/textanalytics/_generated/__init__.py +0 -16
  32. azure/ai/textanalytics/_generated/_configuration.py +0 -70
  33. azure/ai/textanalytics/_generated/_operations_mixin.py +0 -795
  34. azure/ai/textanalytics/_generated/_text_analytics_client.py +0 -126
  35. azure/ai/textanalytics/_generated/_version.py +0 -8
  36. azure/ai/textanalytics/_generated/aio/_configuration.py +0 -66
  37. azure/ai/textanalytics/_generated/aio/_operations_mixin.py +0 -776
  38. azure/ai/textanalytics/_generated/aio/_text_analytics_client.py +0 -124
  39. azure/ai/textanalytics/_generated/models.py +0 -8
  40. azure/ai/textanalytics/_generated/v2022_05_01/__init__.py +0 -20
  41. azure/ai/textanalytics/_generated/v2022_05_01/_configuration.py +0 -72
  42. azure/ai/textanalytics/_generated/v2022_05_01/_text_analytics_client.py +0 -100
  43. azure/ai/textanalytics/_generated/v2022_05_01/_vendor.py +0 -45
  44. azure/ai/textanalytics/_generated/v2022_05_01/aio/__init__.py +0 -20
  45. azure/ai/textanalytics/_generated/v2022_05_01/aio/_configuration.py +0 -71
  46. azure/ai/textanalytics/_generated/v2022_05_01/aio/_text_analytics_client.py +0 -97
  47. azure/ai/textanalytics/_generated/v2022_05_01/aio/operations/__init__.py +0 -18
  48. azure/ai/textanalytics/_generated/v2022_05_01/aio/operations/_patch.py +0 -121
  49. azure/ai/textanalytics/_generated/v2022_05_01/aio/operations/_text_analytics_client_operations.py +0 -603
  50. azure/ai/textanalytics/_generated/v2022_05_01/models/__init__.py +0 -281
  51. azure/ai/textanalytics/_generated/v2022_05_01/models/_models_py3.py +0 -5722
  52. azure/ai/textanalytics/_generated/v2022_05_01/models/_text_analytics_client_enums.py +0 -439
  53. azure/ai/textanalytics/_generated/v2022_05_01/operations/_patch.py +0 -120
  54. azure/ai/textanalytics/_generated/v2022_05_01/operations/_text_analytics_client_operations.py +0 -744
  55. azure/ai/textanalytics/_generated/v2022_10_01_preview/__init__.py +0 -20
  56. azure/ai/textanalytics/_generated/v2022_10_01_preview/_configuration.py +0 -72
  57. azure/ai/textanalytics/_generated/v2022_10_01_preview/_patch.py +0 -19
  58. azure/ai/textanalytics/_generated/v2022_10_01_preview/_text_analytics_client.py +0 -100
  59. azure/ai/textanalytics/_generated/v2022_10_01_preview/_vendor.py +0 -45
  60. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/__init__.py +0 -20
  61. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/_configuration.py +0 -71
  62. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/_patch.py +0 -19
  63. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/_text_analytics_client.py +0 -97
  64. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/_vendor.py +0 -27
  65. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/operations/_patch.py +0 -121
  66. azure/ai/textanalytics/_generated/v2022_10_01_preview/aio/operations/_text_analytics_client_operations.py +0 -603
  67. azure/ai/textanalytics/_generated/v2022_10_01_preview/models/__init__.py +0 -407
  68. azure/ai/textanalytics/_generated/v2022_10_01_preview/models/_models_py3.py +0 -8462
  69. azure/ai/textanalytics/_generated/v2022_10_01_preview/models/_patch.py +0 -72
  70. azure/ai/textanalytics/_generated/v2022_10_01_preview/models/_text_analytics_client_enums.py +0 -730
  71. azure/ai/textanalytics/_generated/v2022_10_01_preview/operations/__init__.py +0 -18
  72. azure/ai/textanalytics/_generated/v2022_10_01_preview/operations/_patch.py +0 -120
  73. azure/ai/textanalytics/_generated/v2022_10_01_preview/operations/_text_analytics_client_operations.py +0 -744
  74. azure/ai/textanalytics/_generated/v3_0/__init__.py +0 -20
  75. azure/ai/textanalytics/_generated/v3_0/_configuration.py +0 -66
  76. azure/ai/textanalytics/_generated/v3_0/_patch.py +0 -31
  77. azure/ai/textanalytics/_generated/v3_0/_text_analytics_client.py +0 -96
  78. azure/ai/textanalytics/_generated/v3_0/_vendor.py +0 -33
  79. azure/ai/textanalytics/_generated/v3_0/aio/__init__.py +0 -20
  80. azure/ai/textanalytics/_generated/v3_0/aio/_configuration.py +0 -65
  81. azure/ai/textanalytics/_generated/v3_0/aio/_patch.py +0 -31
  82. azure/ai/textanalytics/_generated/v3_0/aio/_text_analytics_client.py +0 -93
  83. azure/ai/textanalytics/_generated/v3_0/aio/_vendor.py +0 -27
  84. azure/ai/textanalytics/_generated/v3_0/aio/operations/__init__.py +0 -18
  85. azure/ai/textanalytics/_generated/v3_0/aio/operations/_patch.py +0 -19
  86. azure/ai/textanalytics/_generated/v3_0/aio/operations/_text_analytics_client_operations.py +0 -428
  87. azure/ai/textanalytics/_generated/v3_0/models/__init__.py +0 -81
  88. azure/ai/textanalytics/_generated/v3_0/models/_models_py3.py +0 -1467
  89. azure/ai/textanalytics/_generated/v3_0/models/_patch.py +0 -19
  90. azure/ai/textanalytics/_generated/v3_0/models/_text_analytics_client_enums.py +0 -58
  91. azure/ai/textanalytics/_generated/v3_0/operations/__init__.py +0 -18
  92. azure/ai/textanalytics/_generated/v3_0/operations/_patch.py +0 -19
  93. azure/ai/textanalytics/_generated/v3_0/operations/_text_analytics_client_operations.py +0 -604
  94. azure/ai/textanalytics/_generated/v3_1/__init__.py +0 -20
  95. azure/ai/textanalytics/_generated/v3_1/_configuration.py +0 -66
  96. azure/ai/textanalytics/_generated/v3_1/_patch.py +0 -31
  97. azure/ai/textanalytics/_generated/v3_1/_text_analytics_client.py +0 -98
  98. azure/ai/textanalytics/_generated/v3_1/_vendor.py +0 -45
  99. azure/ai/textanalytics/_generated/v3_1/aio/__init__.py +0 -20
  100. azure/ai/textanalytics/_generated/v3_1/aio/_configuration.py +0 -65
  101. azure/ai/textanalytics/_generated/v3_1/aio/_patch.py +0 -31
  102. azure/ai/textanalytics/_generated/v3_1/aio/_text_analytics_client.py +0 -95
  103. azure/ai/textanalytics/_generated/v3_1/aio/_vendor.py +0 -27
  104. azure/ai/textanalytics/_generated/v3_1/aio/operations/__init__.py +0 -18
  105. azure/ai/textanalytics/_generated/v3_1/aio/operations/_patch.py +0 -19
  106. azure/ai/textanalytics/_generated/v3_1/aio/operations/_text_analytics_client_operations.py +0 -1291
  107. azure/ai/textanalytics/_generated/v3_1/models/__init__.py +0 -205
  108. azure/ai/textanalytics/_generated/v3_1/models/_models_py3.py +0 -3976
  109. azure/ai/textanalytics/_generated/v3_1/models/_patch.py +0 -19
  110. azure/ai/textanalytics/_generated/v3_1/models/_text_analytics_client_enums.py +0 -367
  111. azure/ai/textanalytics/_generated/v3_1/operations/__init__.py +0 -18
  112. azure/ai/textanalytics/_generated/v3_1/operations/_patch.py +0 -19
  113. azure/ai/textanalytics/_generated/v3_1/operations/_text_analytics_client_operations.py +0 -1709
  114. azure/ai/textanalytics/_lro.py +0 -552
  115. azure/ai/textanalytics/_models.py +0 -3142
  116. azure/ai/textanalytics/_policies.py +0 -66
  117. azure/ai/textanalytics/_request_handlers.py +0 -104
  118. azure/ai/textanalytics/_response_handlers.py +0 -580
  119. azure/ai/textanalytics/_text_analytics_client.py +0 -1802
  120. azure/ai/textanalytics/_user_agent.py +0 -8
  121. azure/ai/textanalytics/_validate.py +0 -113
  122. azure/ai/textanalytics/aio/_base_client_async.py +0 -95
  123. azure/ai/textanalytics/aio/_lro_async.py +0 -501
  124. azure/ai/textanalytics/aio/_response_handlers_async.py +0 -94
  125. azure/ai/textanalytics/aio/_text_analytics_client_async.py +0 -1800
  126. azure_ai_textanalytics-5.3.0b1.dist-info/RECORD +0 -115
  127. {azure_ai_textanalytics-5.3.0b1.dist-info → azure_ai_textanalytics-6.0.0b1.dist-info/licenses}/LICENSE +0 -0
  128. {azure_ai_textanalytics-5.3.0b1.dist-info → azure_ai_textanalytics-6.0.0b1.dist-info}/top_level.txt +0 -0
@@ -1,580 +0,0 @@
1
- # ------------------------------------
2
- # Copyright (c) Microsoft Corporation.
3
- # Licensed under the MIT License.
4
- # ------------------------------------
5
-
6
- import json
7
- import functools
8
- from collections import defaultdict
9
- from urllib.parse import urlparse, parse_qsl
10
- from azure.core.exceptions import (
11
- HttpResponseError,
12
- ClientAuthenticationError,
13
- ResourceNotFoundError,
14
- ODataV4Format,
15
- )
16
- from azure.core.paging import ItemPaged
17
- from ._models import (
18
- RecognizeEntitiesResult,
19
- CategorizedEntity,
20
- TextDocumentStatistics,
21
- RecognizeLinkedEntitiesResult,
22
- LinkedEntity,
23
- ExtractKeyPhrasesResult,
24
- AnalyzeSentimentResult,
25
- SentenceSentiment,
26
- DetectLanguageResult,
27
- DetectedLanguage,
28
- DocumentError,
29
- SentimentConfidenceScores,
30
- TextAnalyticsError,
31
- TextAnalyticsWarning,
32
- RecognizePiiEntitiesResult,
33
- PiiEntity,
34
- AnalyzeHealthcareEntitiesResult,
35
- _AnalyzeActionsType,
36
- RecognizeCustomEntitiesResult,
37
- ClassifyDocumentResult,
38
- ActionPointerKind,
39
- ExtractSummaryResult,
40
- AbstractSummaryResult,
41
- DynamicClassificationResult,
42
- )
43
-
44
-
45
- class CSODataV4Format(ODataV4Format):
46
- def __init__(self, odata_error):
47
- try:
48
- if odata_error["error"]["innererror"]:
49
- super().__init__(
50
- odata_error["error"]["innererror"]
51
- )
52
- self.details = odata_error["error"].get("details", [])
53
- except KeyError:
54
- super().__init__(odata_error)
55
-
56
-
57
- def process_http_response_error(error):
58
- """Raise detailed error message."""
59
- raise_error = HttpResponseError
60
- if error.status_code == 401:
61
- raise_error = ClientAuthenticationError
62
- if error.status_code == 404:
63
- raise_error = ResourceNotFoundError
64
- raise raise_error(response=error.response, error_format=CSODataV4Format) from error
65
-
66
-
67
- def order_results(response, combined):
68
- """Order results in the order the user passed them in.
69
-
70
- :param response: Used to get the original documents in the request
71
- :param combined: A combined list of the results | errors
72
- :return: In order list of results | errors (if any)
73
- """
74
- try:
75
- request = json.loads(response.http_response.request.body)["documents"]
76
- except KeyError: # language API compat
77
- request = json.loads(response.http_response.request.body)["analysisInput"]["documents"]
78
- mapping = {item.id: item for item in combined}
79
- ordered_response = [mapping[item["id"]] for item in request]
80
- return ordered_response
81
-
82
-
83
- def order_lro_results(doc_id_order, combined):
84
- """Order results in the order the user passed them in.
85
- For long running operations, we need to explicitly pass in the
86
- document ids since the initial request will no longer be available.
87
-
88
- :param doc_id_order: A list of document IDs from the original request.
89
- :param combined: A combined list of the results | errors
90
- :return: In order list of results | errors (if any)
91
- """
92
-
93
- mapping = [(item.id, item) for item in combined]
94
- ordered_response = [
95
- i[1] for i in sorted(mapping, key=lambda m: doc_id_order.index(m[0]))
96
- ]
97
- return ordered_response
98
-
99
-
100
- def prepare_result(func):
101
- def choose_wrapper(*args, **kwargs):
102
- def wrapper(
103
- response, obj, _, ordering_function
104
- ):
105
- if hasattr(obj, "results"):
106
- obj = obj.results # language API compat
107
-
108
- if obj.errors:
109
- combined = obj.documents + obj.errors
110
- results = ordering_function(response, combined)
111
-
112
- else:
113
- results = obj.documents
114
-
115
- for idx, item in enumerate(results):
116
- if hasattr(item, "error"):
117
- results[idx] = DocumentError(
118
- id=item.id,
119
- error=TextAnalyticsError._from_generated( # pylint: disable=protected-access
120
- item.error
121
- ),
122
- )
123
- else:
124
- results[idx] = func(item, results)
125
- return results
126
-
127
- lro = kwargs.get("lro", False)
128
-
129
- if lro:
130
- return wrapper(*args, ordering_function=order_lro_results)
131
- return wrapper(*args, ordering_function=order_results)
132
-
133
- return choose_wrapper
134
-
135
-
136
- @prepare_result
137
- def abstract_summary_result(
138
- summary, results, *args, **kwargs
139
- ): # pylint: disable=unused-argument
140
- return AbstractSummaryResult._from_generated( # pylint: disable=protected-access
141
- summary
142
- )
143
-
144
-
145
- @prepare_result
146
- def language_result(language, results): # pylint: disable=unused-argument
147
- return DetectLanguageResult(
148
- id=language.id,
149
- primary_language=DetectedLanguage._from_generated( # pylint: disable=protected-access
150
- language.detected_language
151
- ),
152
- warnings=[
153
- TextAnalyticsWarning._from_generated(w) # pylint: disable=protected-access
154
- for w in language.warnings
155
- ],
156
- statistics=TextDocumentStatistics._from_generated( # pylint: disable=protected-access
157
- language.statistics
158
- ),
159
- )
160
-
161
-
162
- @prepare_result
163
- def entities_result(
164
- entity, results, *args, **kwargs
165
- ): # pylint: disable=unused-argument
166
- return RecognizeEntitiesResult(
167
- id=entity.id,
168
- entities=[
169
- CategorizedEntity._from_generated(e) # pylint: disable=protected-access
170
- for e in entity.entities
171
- ],
172
- warnings=[
173
- TextAnalyticsWarning._from_generated(w) # pylint: disable=protected-access
174
- for w in entity.warnings
175
- ],
176
- statistics=TextDocumentStatistics._from_generated( # pylint: disable=protected-access
177
- entity.statistics
178
- ),
179
- detected_language=DetectedLanguage._from_generated( # pylint: disable=protected-access
180
- entity.detected_language
181
- ) if hasattr(entity, "detected_language") and entity.detected_language else None
182
- )
183
-
184
-
185
- @prepare_result
186
- def linked_entities_result(
187
- entity, results, *args, **kwargs
188
- ): # pylint: disable=unused-argument
189
- return RecognizeLinkedEntitiesResult(
190
- id=entity.id,
191
- entities=[
192
- LinkedEntity._from_generated(e) # pylint: disable=protected-access
193
- for e in entity.entities
194
- ],
195
- warnings=[
196
- TextAnalyticsWarning._from_generated(w) # pylint: disable=protected-access
197
- for w in entity.warnings
198
- ],
199
- statistics=TextDocumentStatistics._from_generated( # pylint: disable=protected-access
200
- entity.statistics
201
- ),
202
- detected_language=DetectedLanguage._from_generated( # pylint: disable=protected-access
203
- entity.detected_language
204
- ) if hasattr(entity, "detected_language") and entity.detected_language else None
205
- )
206
-
207
-
208
- @prepare_result
209
- def key_phrases_result(
210
- phrases, results, *args, **kwargs
211
- ): # pylint: disable=unused-argument
212
- return ExtractKeyPhrasesResult(
213
- id=phrases.id,
214
- key_phrases=phrases.key_phrases,
215
- warnings=[
216
- TextAnalyticsWarning._from_generated(w) # pylint: disable=protected-access
217
- for w in phrases.warnings
218
- ],
219
- statistics=TextDocumentStatistics._from_generated( # pylint: disable=protected-access
220
- phrases.statistics
221
- ),
222
- detected_language=DetectedLanguage._from_generated( # pylint: disable=protected-access
223
- phrases.detected_language
224
- ) if hasattr(phrases, "detected_language") and phrases.detected_language else None
225
- )
226
-
227
-
228
- @prepare_result
229
- def sentiment_result(
230
- sentiment, results, *args, **kwargs
231
- ): # pylint: disable=unused-argument
232
- return AnalyzeSentimentResult(
233
- id=sentiment.id,
234
- sentiment=sentiment.sentiment,
235
- warnings=[
236
- TextAnalyticsWarning._from_generated(w) # pylint: disable=protected-access
237
- for w in sentiment.warnings
238
- ],
239
- statistics=TextDocumentStatistics._from_generated( # pylint: disable=protected-access
240
- sentiment.statistics
241
- ),
242
- confidence_scores=SentimentConfidenceScores._from_generated( # pylint: disable=protected-access
243
- sentiment.confidence_scores
244
- ),
245
- sentences=[
246
- SentenceSentiment._from_generated( # pylint: disable=protected-access
247
- s, results, sentiment
248
- )
249
- for s in sentiment.sentences
250
- ],
251
- detected_language=DetectedLanguage._from_generated( # pylint: disable=protected-access
252
- sentiment.detected_language
253
- ) if hasattr(sentiment, "detected_language") and sentiment.detected_language else None
254
- )
255
-
256
-
257
- @prepare_result
258
- def pii_entities_result(
259
- entity, results, *args, **kwargs
260
- ): # pylint: disable=unused-argument
261
- return RecognizePiiEntitiesResult(
262
- id=entity.id,
263
- entities=[
264
- PiiEntity._from_generated(e) # pylint: disable=protected-access
265
- for e in entity.entities
266
- ],
267
- redacted_text=entity.redacted_text
268
- if hasattr(entity, "redacted_text")
269
- else None,
270
- warnings=[
271
- TextAnalyticsWarning._from_generated(w) # pylint: disable=protected-access
272
- for w in entity.warnings
273
- ],
274
- statistics=TextDocumentStatistics._from_generated( # pylint: disable=protected-access
275
- entity.statistics
276
- ),
277
- detected_language=DetectedLanguage._from_generated( # pylint: disable=protected-access
278
- entity.detected_language
279
- ) if hasattr(entity, "detected_language") and entity.detected_language else None
280
- )
281
-
282
-
283
- @prepare_result
284
- def healthcare_result(
285
- health_result, results, *args, **kwargs
286
- ): # pylint: disable=unused-argument
287
- return AnalyzeHealthcareEntitiesResult._from_generated( # pylint: disable=protected-access
288
- health_result
289
- )
290
-
291
-
292
- @prepare_result
293
- def summary_result(
294
- summary, results, *args, **kwargs
295
- ): # pylint: disable=unused-argument
296
- return ExtractSummaryResult._from_generated( # pylint: disable=protected-access
297
- summary
298
- )
299
-
300
-
301
- @prepare_result
302
- def custom_entities_result(
303
- custom_entities, results, *args, **kwargs
304
- ): # pylint: disable=unused-argument
305
- return RecognizeCustomEntitiesResult._from_generated( # pylint: disable=protected-access
306
- custom_entities
307
- )
308
-
309
-
310
- @prepare_result
311
- def classify_document_result(
312
- custom_categories, results, *args, **kwargs
313
- ): # pylint: disable=unused-argument
314
- return ClassifyDocumentResult._from_generated( # pylint: disable=protected-access
315
- custom_categories
316
- )
317
-
318
-
319
- @prepare_result
320
- def dynamic_classification_result(
321
- categories, results, *args, **kwargs
322
- ): # pylint: disable=unused-argument
323
- return DynamicClassificationResult._from_generated( # pylint: disable=protected-access
324
- categories
325
- )
326
-
327
-
328
- def healthcare_extract_page_data(
329
- doc_id_order, obj, health_job_state
330
- ): # pylint: disable=unused-argument
331
- return (
332
- health_job_state.next_link,
333
- healthcare_result(
334
- doc_id_order,
335
- health_job_state.results
336
- if hasattr(health_job_state, "results")
337
- else health_job_state.tasks.items[0].results,
338
- {},
339
- lro=True
340
- ),
341
- )
342
-
343
-
344
- def _get_deserialization_callback_from_task_type(task_type): # pylint: disable=too-many-return-statements
345
- if task_type == _AnalyzeActionsType.RECOGNIZE_ENTITIES:
346
- return entities_result
347
- if task_type == _AnalyzeActionsType.RECOGNIZE_PII_ENTITIES:
348
- return pii_entities_result
349
- if task_type == _AnalyzeActionsType.RECOGNIZE_LINKED_ENTITIES:
350
- return linked_entities_result
351
- if task_type == _AnalyzeActionsType.ANALYZE_SENTIMENT:
352
- return sentiment_result
353
- if task_type == _AnalyzeActionsType.RECOGNIZE_CUSTOM_ENTITIES:
354
- return custom_entities_result
355
- if task_type == _AnalyzeActionsType.SINGLE_LABEL_CLASSIFY:
356
- return classify_document_result
357
- if task_type == _AnalyzeActionsType.MULTI_LABEL_CLASSIFY:
358
- return classify_document_result
359
- if task_type == _AnalyzeActionsType.ANALYZE_HEALTHCARE_ENTITIES:
360
- return healthcare_result
361
- if task_type == _AnalyzeActionsType.EXTRACT_SUMMARY:
362
- return summary_result
363
- if task_type == _AnalyzeActionsType.ABSTRACT_SUMMARY:
364
- return abstract_summary_result
365
- return key_phrases_result
366
-
367
-
368
- def _get_property_name_from_task_type(task_type): # pylint: disable=too-many-return-statements
369
- """v3.1 only"""
370
- if task_type == _AnalyzeActionsType.RECOGNIZE_ENTITIES:
371
- return "entity_recognition_tasks"
372
- if task_type == _AnalyzeActionsType.RECOGNIZE_PII_ENTITIES:
373
- return "entity_recognition_pii_tasks"
374
- if task_type == _AnalyzeActionsType.RECOGNIZE_LINKED_ENTITIES:
375
- return "entity_linking_tasks"
376
- if task_type == _AnalyzeActionsType.ANALYZE_SENTIMENT:
377
- return "sentiment_analysis_tasks"
378
- return "key_phrase_extraction_tasks"
379
-
380
-
381
- def get_task_from_pointer(task_type): # pylint: disable=too-many-return-statements
382
- """v3.1 only"""
383
- if task_type == ActionPointerKind.RECOGNIZE_ENTITIES:
384
- return "entity_recognition_tasks"
385
- if task_type == ActionPointerKind.RECOGNIZE_PII_ENTITIES:
386
- return "entity_recognition_pii_tasks"
387
- if task_type == ActionPointerKind.RECOGNIZE_LINKED_ENTITIES:
388
- return "entity_linking_tasks"
389
- if task_type == ActionPointerKind.ANALYZE_SENTIMENT:
390
- return "sentiment_analysis_tasks"
391
- return "key_phrase_extraction_tasks"
392
-
393
-
394
- def resolve_action_pointer(pointer):
395
- import re
396
- pointer_union = "|".join(value for value in ActionPointerKind)
397
- found = re.search(fr"#/tasks/({pointer_union})/\d+", pointer)
398
- if found:
399
- index = int(pointer[-1])
400
- task = pointer.split("#/tasks/")[1].split("/")[0]
401
- property_name = get_task_from_pointer(task)
402
- return property_name, index
403
- raise ValueError(
404
- f"Unexpected response from service - action pointer '{pointer}' is not a valid action pointer."
405
- )
406
-
407
-
408
- def pad_result(tasks_obj, doc_id_order):
409
- return [
410
- DocumentError(
411
- id=doc_id,
412
- error=TextAnalyticsError(message=f"No result for document. Action returned status '{tasks_obj.status}'.")
413
- ) for doc_id in doc_id_order
414
- ]
415
-
416
-
417
- def get_ordered_errors(tasks_obj, task_name, doc_id_order):
418
- # throw exception if error missing a target
419
- missing_target = any([error for error in tasks_obj.errors if error.target is None])
420
- if missing_target:
421
- message = "".join([f"({err.code}) {err.message}" for err in tasks_obj.errors])
422
- raise HttpResponseError(message=message)
423
-
424
- # create a DocumentError per input doc with the action error details
425
- for err in tasks_obj.errors:
426
- # language API compat
427
- if err.target.find("items") != -1:
428
- action = tasks_obj.tasks.items[int(err.target.split("/")[-1])]
429
- else:
430
- property_name, index = resolve_action_pointer(err.target)
431
- actions = getattr(tasks_obj.tasks, property_name)
432
- action = actions[index]
433
- if action.task_name == task_name:
434
- errors = [
435
- DocumentError(
436
- id=doc_id,
437
- error=TextAnalyticsError(code=err.code, message=err.message)
438
- ) for doc_id in doc_id_order
439
- ]
440
- return errors
441
- raise ValueError("Unexpected response from service - no errors for missing action results.")
442
-
443
-
444
- def _get_doc_results(task, doc_id_order, returned_tasks_object):
445
- returned_tasks = returned_tasks_object.tasks
446
- current_task_type, task_name = task
447
- deserialization_callback = _get_deserialization_callback_from_task_type(
448
- current_task_type
449
- )
450
- # language api compat
451
- property_name = \
452
- "items" if hasattr(returned_tasks, "items") else _get_property_name_from_task_type(current_task_type)
453
- try:
454
- response_task_to_deserialize = \
455
- next(task for task in getattr(returned_tasks, property_name) if task.task_name == task_name)
456
- except StopIteration:
457
- raise ValueError("Unexpected response from service - unable to deserialize result.")
458
-
459
- # if no results present, check for action errors
460
- if response_task_to_deserialize.results is None:
461
- return get_ordered_errors(returned_tasks_object, task_name, doc_id_order)
462
- # if results obj present, but no document results or errors (likely a canceled scenario)
463
- if not response_task_to_deserialize.results.documents and not response_task_to_deserialize.results.errors:
464
- return pad_result(returned_tasks_object, doc_id_order)
465
- return deserialization_callback(
466
- doc_id_order, response_task_to_deserialize.results, {}, lro=True
467
- )
468
-
469
-
470
- def get_iter_items(doc_id_order, task_order, bespoke, analyze_job_state):
471
- iter_items = defaultdict(list) # map doc id to action results
472
- returned_tasks_object = analyze_job_state
473
-
474
- if bespoke:
475
- return _get_doc_results(
476
- task_order[0],
477
- doc_id_order,
478
- returned_tasks_object,
479
- )
480
-
481
- for task in task_order:
482
- results = _get_doc_results(
483
- task,
484
- doc_id_order,
485
- returned_tasks_object,
486
- )
487
- for result in results:
488
- iter_items[result.id].append(result)
489
-
490
- return [iter_items[doc_id] for doc_id in doc_id_order if doc_id in iter_items]
491
-
492
-
493
- def analyze_extract_page_data(
494
- doc_id_order, task_order, bespoke, analyze_job_state
495
- ):
496
- # return next link, list of
497
- iter_items = get_iter_items(
498
- doc_id_order, task_order, bespoke, analyze_job_state
499
- )
500
- return analyze_job_state.next_link, iter_items
501
-
502
-
503
- def lro_get_next_page(
504
- lro_status_callback, first_page, continuation_token, show_stats=False
505
- ):
506
- if continuation_token is None:
507
- return first_page
508
-
509
- try:
510
- continuation_token = continuation_token.decode("utf-8")
511
-
512
- except AttributeError:
513
- pass
514
-
515
- parsed_url = urlparse(continuation_token)
516
- job_id = parsed_url.path.split("/")[-1]
517
- query_params = dict(parse_qsl(parsed_url.query.replace("$", "")))
518
- if "showStats" in query_params:
519
- query_params.pop("showStats")
520
- if "api-version" in query_params: # language api compat
521
- query_params.pop("api-version")
522
- query_params["show_stats"] = show_stats
523
-
524
- return lro_status_callback(job_id, **query_params)
525
-
526
-
527
- def healthcare_paged_result(
528
- doc_id_order, health_status_callback, _, obj, show_stats=False
529
- ):
530
- return ItemPaged(
531
- functools.partial(
532
- lro_get_next_page, health_status_callback, obj, show_stats=show_stats
533
- ),
534
- functools.partial(
535
- healthcare_extract_page_data, doc_id_order, obj
536
- ),
537
- )
538
-
539
-
540
- def analyze_paged_result(
541
- doc_id_order,
542
- task_order,
543
- analyze_status_callback,
544
- _,
545
- obj,
546
- show_stats=False,
547
- bespoke=False
548
- ):
549
- return ItemPaged(
550
- functools.partial(
551
- lro_get_next_page, analyze_status_callback, obj, show_stats=show_stats
552
- ),
553
- functools.partial(
554
- analyze_extract_page_data, doc_id_order, task_order, bespoke
555
- ),
556
- )
557
-
558
-
559
- def _get_result_from_continuation_token(
560
- client, continuation_token, poller_type, polling_method, callback, bespoke=False
561
- ):
562
- def result_callback(initial_response, pipeline_response):
563
- doc_id_order = initial_response.context.options["doc_id_order"]
564
- show_stats = initial_response.context.options["show_stats"]
565
- task_id_order = initial_response.context.options.get("task_id_order")
566
- return callback(
567
- pipeline_response,
568
- None,
569
- doc_id_order,
570
- task_id_order=task_id_order,
571
- show_stats=show_stats,
572
- bespoke=bespoke
573
- )
574
-
575
- return poller_type.from_continuation_token(
576
- polling_method=polling_method,
577
- client=client,
578
- deserialization_callback=result_callback,
579
- continuation_token=continuation_token
580
- )