<jupyter_start><jupyter_text>Natural Language Patient FinderIdentify patients stored in a FHIR server that match a natural language fragment.<jupyter_code>import requests
import json<jupyter_output><empty_output><jupyter_text>Set target servicesServices usually share a namespace. Specify namespace to be used.<jupyter_code>ingress_subdomain = ""

namespace = "hackathon" # input("Enter a service namespace: ")

#Learned Intent endpoint
intentURL = "https://" + namespace  + "-learned-intent." + ingress_subdomain + "/?text="

#Key Concept Extractor endpoint
extractConceptsURL = "https://" + namespace + "-key-concept-extractor." + ingress_subdomain

#Generate CQL endpoint
generateCQLURL = "https://" + namespace + "-cql-generator." + ingress_subdomain + "/generate"

#libraries
cohortURL = "https://hackathon-ingestion-cohort-service." + ingress_subdomain + "/cohort-service/libraries"
#/libraries/nameoflibrary  (get or delete)
#/libraries (post new library)
#/libraries/nameoflibrary/patients (return list of patients)
#/libraries/nameoflibrary/patientIds (return list of patient ids)<jupyter_output><empty_output><jupyter_text>Provide fragment to be analyzedExamples:- Female patients > 18 years old- Patient has diabetes mellitus- Hypertriglyceridemia- nonproliferative diabetic retinopathy- no gestational diabetes- Patient is able to return to Johns Hopkins for follow-up appointments.<jupyter_code>fragment = input("Enter a fragment: ")<jupyter_output><empty_output><jupyter_text>Identify Intent of fragment<jupyter_code>url = intentURL + "\"" + fragment + "\""

resp = requests.get(url=url)
intent = resp.content.decode("utf-8")
print("Learned intent is: ", intent)<jupyter_output><empty_output><jupyter_text>Extract key entities for given fragment and intent<jupyter_code>params = {'intent': intent, 'text': fragment}
  
# sending get request and saving the response as response object
concept_set = requests.get(url = extractConceptsURL, params = params).content.decode("utf-8")

parsed = json.loads(concept_set)
print(json.dumps(parsed, indent=4, sort_keys=True))<jupyter_output><empty_output><jupyter_text>(Optional) show SnoMed concept expansion<jupyter_code>snomed_code = input("Enter SnoMed code to expand: ")

snomed_expansion_url = "https://snowstorm-fhir.snomedtools.org/fhir/CodeSystem/$lookup?code=" + snomed_code;

expansion_response = requests.get(url=snomed_expansion_url)

parsed = json.loads( expansion_response.content.decode("utf-8"))
print("\nSnoMed expansion:")
print(json.dumps(parsed, indent=4, sort_keys=True))<jupyter_output><empty_output><jupyter_text>Generate CQL from concepts and identified type<jupyter_code>cql_response = requests.post(url=generateCQLURL, data={'conceptSet': concept_set})

cql = cql_response.content.decode("utf-8")
print("\nCQL Generator produced:\n\n", cql)<jupyter_output><empty_output><jupyter_text>Push CQL to Cohort Service and Run<jupyter_code>library_name = cql.split('"')[1]
print("name:", library_name)

library_version = cql.split("'")[1]
print("version:", library_version)

library_endpoint = library_name + "-" + library_version
print("endpoint extension:", library_endpoint)

#verify no existing CQL exists with this name. Delete if it does.
resp = requests.delete(cohortURL + "/" + library_endpoint)
print(resp.content.decode("utf-8"))

#Push CQL to cohort service
headers = {}
headers["Content-Type"] = "text/plain"
headers["Accept"] = "application/json"

resp = requests.post(url = cohortURL, data = cql, headers = headers)

print("Posting CQL to Cohort Service: " + str(resp))

# Run CQL to find matching patient ID's
run_endpoint = cohortURL + "/" + library_endpoint + "/patientIds"
resp = requests.get(url=run_endpoint)

print("Running CQL on Cohort Service: " + str(resp))

result = resp.content.decode("utf-8")

ids = (eval(result))
print(len(ids), "patients returned")
for id in ids:
    print(id)<jupyter_output><empty_output><jupyter_text>Run CQL on cohort service to identify matching patients<jupyter_code>run_endpoint = cohortURL + "/" + library_endpoint + "/patients"
total_resp = requests.get(url=run_endpoint)

print(json.dumps(total_resp.json(), indent=4, sort_keys=True))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1263710061993324, 'fraction_numerical': 0.003099666189794945, 'mean_word_length': 6.930056710775047, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 38, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '201648', 'license': 'Apache-2.0', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1327, 'n_words': 308, 'path': 'notebooks/Natural Language Patient Finder - One Fragment - DREAm Team - Final.ipynb', 'repo_name': 'Alvearie/hackathon-text2cql', 'size': '4194'}