/
entityRecognition.ts
49 lines (39 loc) · 1.53 KB
/
entityRecognition.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Uses the entity recognition endpoint to detect entities in a document using
* Named Entity Recognition (NER) and prints them along with their recognized
* entity type.
*
* @summary detects entities in a piece of text
* @azsdk-weight 100
*/
import { TextAnalysisClient, AzureKeyCredential } 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 = [
"Microsoft was founded by Bill Gates and Paul Allen.",
"I had a wonderful trip to Seattle last week.",
"I visited the Space Needle 2 times.",
];
export async function main() {
console.log("== Recognize Entities Sample ==");
const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
const results = await client.analyze("EntityRecognition", documents);
for (const result of results) {
console.log(`- Document ${result.id}`);
if (!result.error) {
console.log("\tRecognized Entities:");
for (const entity of result.entities) {
console.log(`\t- Entity ${entity.text} of type ${entity.category}`);
}
} else console.error("\tError:", result.error);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});