Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion sdk/textanalytics/azure-ai-textanalytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,11 @@ Note: The Healthcare Entities Analysis service is currently available only in th
```python
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import (
TextAnalyticsClient, RecognizeEntitiesAction, RecognizePiiEntitiesAction, ExtractKeyPhrasesAction
TextAnalyticsClient,
RecognizeEntitiesAction,
RecognizePiiEntitiesAction,
ExtractKeyPhrasesAction,
RecognizeLinkedEntitiesAction
)

credential = AzureKeyCredential("<api_key>")
Expand All @@ -520,6 +524,7 @@ poller = text_analytics_client.begin_analyze_batch_actions(
RecognizeEntitiesAction(),
RecognizePiiEntitiesAction(),
ExtractKeyPhrasesAction(),
RecognizeLinkedEntitiesAction()
]
)

Expand Down Expand Up @@ -559,6 +564,26 @@ for idx, doc in enumerate(docs):
print("Document text: {}\n".format(documents[idx]))
print("Key Phrases: {}\n".format(doc.key_phrases))
print("------------------------------------------")

fourth_action_result = next(result)
print("Results of Linked Entities Recognition action:")
docs = [doc for doc in fourth_action_result.document_results if not doc.is_error]

for idx, doc in enumerate(docs):
print("Document text: {}\n".format(documents[idx]))
for linked_entity in doc.entities:
print("Entity name: {}".format(linked_entity.name))
print("...Data source: {}".format(linked_entity.data_source))
print("...Data source language: {}".format(linked_entity.language))
print("...Data source entity ID: {}".format(linked_entity.data_source_entity_id))
print("...Data source URL: {}".format(linked_entity.url))
print("...Document matches:")
for match in linked_entity.matches:
print("......Match text: {}".format(match.text))
print(".........Confidence Score: {}".format(match.confidence_score))
print(".........Offset: {}".format(match.offset))
print(".........Length: {}".format(match.length))
print("------------------------------------------")
```

The returned response is an object encapsulating multiple iterables, each representing results of individual analyses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def analyze_healthcare_entities(self):
for relation in dosage_of_medication_relations:
# The DosageOfMedication relation should only contain the dosage and medication roles

dosage_role = next(filter(lambda x: x.name == HealthcareEntityRelationRoleType.DOSAGE, relation.roles))
medication_role = next(filter(lambda x: x.name == HealthcareEntityRelationRoleType.MEDICATION, relation.roles))
dosage_role = next(iter(filter(lambda x: x.name == HealthcareEntityRelationRoleType.DOSAGE, relation.roles)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next(filter(...)) seems to not work in Python 2. I can revert this if Python 2 support isn't that important, but this works for both Python2/3.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fair, python 2.7 is making me sad again :'(. Thanks @abhahn !

medication_role = next(iter(filter(lambda x: x.name == HealthcareEntityRelationRoleType.MEDICATION, relation.roles)))

try:
dosage_value = int(re.findall(r"\d+", dosage_role.entity.text)[0]) # we find the numbers in the dosage
Expand All @@ -126,12 +126,10 @@ def analyze_healthcare_entities(self):
# Error handling for if there's no dosage in numbers.
pass

[
for medication, dosage in medication_to_dosage.items():
print("We have fulfilled '{}' total mg of '{}'".format(
dosage, medication
))
for medication, dosage in medication_to_dosage.items()
]


if __name__ == "__main__":
Expand Down