question

KamranAli-0346 avatar image
0 Votes"
KamranAli-0346 asked KamranAli-0346 commented

Converting textanalytics result to JSON Format

Hello,
I am using the example provided in the Machine Learning Studio Docs for extracting Health Entities from a given string.
The code is shown below.

My question is: what is the easiest way to convert the output result into JSON format?

 from azure.core.credentials import AzureKeyCredential
 from azure.ai.textanalytics import TextAnalyticsClient
 import json
    
 credential = AzureKeyCredential("**********************************")
 endpoint="https://eastus.api.cognitive.microsoft.com/"
    
 text_analytics_client = TextAnalyticsClient(endpoint, credential)
    
 documents = ["Subject is taking 100mg of ibuprofen twice daily"]
    
 poller = text_analytics_client.begin_analyze_healthcare_entities(documents)
 result = poller.result()
    
 docs = [doc for doc in result if not doc.is_error]
    
 print("Results of Healthcare Entities Analysis:")
 for idx, doc in enumerate(docs):
     for entity in doc.entities:
         print("Entity: {}".format(entity.text))
         print("...Normalized Text: {}".format(entity.normalized_text))
         print("...Category: {}".format(entity.category))
         print("...Subcategory: {}".format(entity.subcategory))
         print("...Offset: {}".format(entity.offset))
         print("...Confidence score: {}".format(entity.confidence_score))
         if entity.data_sources is not None:
             print("...Data Sources:")
             for data_source in entity.data_sources:
                 print("......Entity ID: {}".format(data_source.entity_id))
                 print("......Name: {}".format(data_source.name))
         if entity.assertion is not None:
             print("...Assertion:")
             print("......Conditionality: {}".format(entity.assertion.conditionality))
             print("......Certainty: {}".format(entity.assertion.certainty))
             print("......Association: {}".format(entity.assertion.association))
         for relation in doc.entity_relations:
             print("Relation of type: {} has the following roles".format(relation.relation_type))
         for role in relation.roles:
             print("...Role '{}' with entity '{}'".format(role.name, role.entity.text))
     print("------------------------------------------")

azure-machine-learning
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KamranAli-0346 Did the below suggestion help to extract the response as JSON?

0 Votes 0 ·

@romungi-MSFT Thanks for the tip. I am away on travel and will be able to check this after May 8th.
Will certainly post the outcome here.

Many Thanks

0 Votes 0 ·

1 Answer

romungi-MSFT avatar image
0 Votes"
romungi-MSFT answered KamranAli-0346 commented

@KamranAli-0346 The result does not seem to be directly serializable to JSON. I found a library JSONS that can do the heavy lifting if you are using python 3.5 or higher.

Install jsons

 pip install jsons

Import JSONS and using jsons.dump() on docs object.

 import jsons #import in the import section
 print(jsons.dump(docs)) #Printing the json after docs is created

This should give a file of this format in this case. Uploaded the file in .txt format since JSON files cannot be uploaded on Q&A, download the file and rename it to .json
I hope this helps!!


If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.


196624-health.txt



health.txt (36.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@romungi-MSFT Your solution worked perfectly!
I have accepted the answer.

Thanks

0 Votes 0 ·