/
healthcare.ts
95 lines (84 loc) · 3.22 KB
/
healthcare.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample program uses the Text Analytics for Healthcare API to detect
* healthcare-related entities in some documents and prints them to the
* console.
*
* @summary detects healthcare entities in a piece of text
* @azsdk-weight 50
*/
import {
AnalyzeBatchAction,
AzureKeyCredential,
TextAnalysisClient,
} from "@azure/ai-language-text";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
// You will need to set these environment variables or edit the following values
const endpoint = process.env["ENDPOINT"] || "<cognitive language service endpoint>";
const apiKey = process.env["LANGUAGE_API_KEY"] || "<api key>";
const documents = [
"The patient is a 54-year-old gentleman with a history of progressive angina over the past several months.",
"Prescribed 100mg ibuprofen, taken twice daily.",
"Patient does not suffer from high blood pressure.",
];
export async function main() {
console.log("== Healthcare Sample ==");
const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
const actions: AnalyzeBatchAction[] = [
{
kind: "Healthcare",
},
];
const poller = await client.beginAnalyzeBatch(actions, documents, "en");
poller.onProgress(() => {
console.log(
`Last time the operation was updated was on: ${poller.getOperationState().modifiedOn}`
);
});
console.log(`The operation was created on ${poller.getOperationState().createdOn}`);
console.log(`The operation results will expire on ${poller.getOperationState().expiresOn}`);
const results = await poller.pollUntilDone();
for await (const actionResult of results) {
if (actionResult.kind !== "Healthcare") {
throw new Error(`Expected a healthcare results but got: ${actionResult.kind}`);
}
if (actionResult.error) {
const { code, message } = actionResult.error;
throw new Error(`Unexpected error (${code}): ${message}`);
}
for (const result of actionResult.results) {
console.log(`- Document ${result.id}`);
if (result.error) {
const { code, message } = result.error;
throw new Error(`Unexpected error (${code}): ${message}`);
}
console.log("\tRecognized Entities:");
for (const entity of result.entities) {
console.log(`\t- Entity "${entity.text}" of type ${entity.category}`);
if (entity.dataSources.length > 0) {
console.log("\t and it can be referenced in the following data sources:");
for (const ds of entity.dataSources) {
console.log(`\t\t- ${ds.name} with Entity ID: ${ds.entityId}`);
}
}
}
if (result.entityRelations.length > 0) {
console.log(`\tRecognized relations between entities:`);
for (const relation of result.entityRelations) {
console.log(
`\t\t- Relation of type ${relation.relationType} found between the following entities:`
);
for (const role of relation.roles) {
console.log(`\t\t\t- "${role.entity.text}" with the role ${role.name}`);
}
}
}
}
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});