public static void Run([BlobTrigger("%StorageAccountBlob%/ingest/documents/{name}", Connection = "StorageAccount")] Stream myBlob, string name, ILogger log) { log.LogInformation("NLP Extract Entities File triggered by ingest/documents/" + name); try { string coid = name; int dirend = coid.LastIndexOf("/"); if (dirend > -1) { coid = coid.Substring(dirend + 1); } int extbegin = coid.LastIndexOf("."); if (extbegin > -1) { coid = coid.Substring(0, extbegin); } string loc = "ingest/documents/" + name; byte[] byteArray = null; using (MemoryStream ms = new MemoryStream()) { myBlob.CopyTo(ms); byteArray = ms.ToArray(); } log.LogInformation("Calling CogServices/TIKA to Extract Text from hl7json/ingest/documents/" + name); string cogurl = Utilities.GetEnvironmentVariable("CogServicesOCRURL"); log.LogInformation("Trying CogServices..."); string responseFromServer = NLPUtilities.ExtractTextUsingCogServices(byteArray, cogurl, Utilities.GetEnvironmentVariable("CogServicesKey")); if (string.IsNullOrEmpty(responseFromServer)) { log.LogInformation("No extract Trying TIKA..."); responseFromServer = NLPUtilities.ExtractTextUsingTIKA(byteArray, Utilities.GetEnvironmentVariable("TIKAServerurl")); } if (responseFromServer.StartsWith("TIMEOUT~")) { log.LogTrace("CogServiceExtract Timeout: {\"id\":\"" + coid + "\",\"status\":\"Timeout\",\"readresulturl\":\"" + responseFromServer.Split("~")[1] + "\"}"); } //string responseFromServer = System.Text.Encoding.UTF8.GetString(byteArray, 0, byteArray.Length); log.LogInformation("Extracting Medical Reports from hl7json/ingest/documents/" + name); //Extract Reports From Content (Auto-Detect Medical Exchange Formats (CDA, HL7, FHIR)) List <string> medreports = NLPUtilities.ExtractMedicalReportData(responseFromServer, log); List <MedicalEntities> retVal = new List <MedicalEntities>(); foreach (string medreport in medreports) { CTakesRequest creq = new CTakesRequest() { Content = medreport, CTAKESUrl = Utilities.GetEnvironmentVariable("CTAKESServerURL"), UMLSUser = Utilities.GetEnvironmentVariable("CTAKESUMLSUser"), UMLSPassword = Utilities.GetEnvironmentVariable("CTAKESUMLSPassword"), Format = Utilities.GetEnvironmentVariable("CTAKESFormat"), }; log.LogInformation("Calling CTAKES to extract medical entities from hl7json/ingest/documents/" + name); var result = NLPUtilities.ExtractMedicalEntities(creq); result.Id = coid; result.Location = loc; result.DocumentType = name; retVal.Add(result); } log.LogInformation("Updateing search index with content and medical entities from hl7json/ingest/documents/" + name); SearchUtilities su = new SearchUtilities(log); su.UploadMedicalEntities(retVal.ToArray()); log.LogInformation("Succesfully Completed processing of hl7json/ingest/documents/" + name); } catch (System.Exception e) { log.LogError(e, e.Message); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("NLP Extract Entities Http called"); try { string doctype = req.Query["doctype"]; if (doctype == null) { doctype = "Unkown Doc Type"; } string handwritten = req.Query["handwritten"]; string coid = req.Query["id"]; if (coid == null) { coid = Guid.NewGuid().ToString(); } string loc = req.Query["location"]; if (loc == null) { loc = "Adhoc Document Addition"; } string updatesearch = req.Query["updatesearch"]; byte[] byteArray = null; // Read the post data into byte array using (var stream = new MemoryStream()) { await req.Body.CopyToAsync(stream); byteArray = stream.ToArray(); } string cogurl = Utilities.GetEnvironmentVariable("CogServicesOCRURL"); if (handwritten != null) { cogurl += "?mode=Handwritten"; } string responseFromServer = NLPUtilities.ExtractTextUsingCogServices(byteArray, cogurl, Utilities.GetEnvironmentVariable("CogServicesKey")); if (string.IsNullOrEmpty(responseFromServer)) { responseFromServer = NLPUtilities.ExtractTextUsingTIKA(byteArray, Utilities.GetEnvironmentVariable("TIKAServerurl")); } if (responseFromServer.StartsWith("TIMEOUT~")) { return(new JsonResult(JObject.Parse("{\"id\":\"" + coid + "\",\"status\":\"Timeout\",\"readresulturl\":\"" + responseFromServer.Split("~")[1] + "\"}"))); } //Extract Reports From Content (Auto-Detect Medical Exchange Formats (CDA, HL7, FHIR)) List <string> medreports = NLPUtilities.ExtractMedicalReportData(responseFromServer, log); List <MedicalEntities> retVal = new List <MedicalEntities>(); foreach (string medreport in medreports) { CTakesRequest creq = new CTakesRequest() { Content = medreport, CTAKESUrl = Utilities.GetEnvironmentVariable("CTAKESServerURL"), UMLSUser = Utilities.GetEnvironmentVariable("CTAKESUMLSUser"), UMLSPassword = Utilities.GetEnvironmentVariable("CTAKESUMLSPassword"), Format = Utilities.GetEnvironmentVariable("CTAKESFormat"), }; var result = NLPUtilities.ExtractMedicalEntities(creq); result.Id = coid; result.Location = loc; result.DocumentType = doctype; retVal.Add(result); } if (updatesearch != null) { SearchUtilities su = new SearchUtilities(log); su.UploadMedicalEntities(retVal.ToArray()); } return(new JsonResult(retVal)); } catch (System.Exception e) { log.LogError(e, e.Message); return(new System.Web.Http.InternalServerErrorResult()); } }