初めに
上司から契約書の条文を確認して問題が無いか判断するシステムを作ってと言われ、Amazon Qに聞くとAmazon Comprehendが使えると言われたので実際に試してみた。
結果、自分の思っていた用途には使えないことが分かった。
でもせっかく試したのでその過程を残しておく。
導入
Microsoft Windows [版本 10.0.19045.5965]
(c) Microsoft Corporation。保留所有权利。
C:\Users\XX>mkdir comprehend-nodejs-example
C:\Users\XX>cd comprehend-nodejs-example
C:\Users\XX\comprehend-nodejs-example>npm init -y
...省略...
C:\Users\XX\comprehend-nodejs-example>npm install @aws-sdk/client-comprehend
...省略...
C:\Users\XX\comprehend-nodejs-example>aws configure
AWS Access Key ID [********************]: ********************
AWS Secret Access Key [********************]: ****************************************
Default region name [us-east-1]: us-east-1
Default output format [json]: json
検証
C:\Users\XX\comprehend-nodejs-example>node test.js
=== 感情分析 ===
結果: POSITIVE
=== 重要な単語 ===
単語: John Smith
種類: PERSON
---
単語: January 15, 2024
種類: DATE
---
test.js
const { ComprehendClient, DetectSentimentCommand, DetectEntitiesCommand } = require("@aws-sdk/client-comprehend");
const client = new ComprehendClient({ region: "us-east-1" });
// 感情分析
async function checkSentiment(text) {
try {
const command = new DetectSentimentCommand({
Text: text,
LanguageCode: "en"
});
const response = await client.send(command);
console.log("=== 感情分析 ===");
console.log("結果:", response.Sentiment); // POSITIVE, NEGATIVE, NEUTRAL
console.log("");
} catch (error) {
console.error("エラー:", error);
}
}
// 重要な単語を見つける
async function findImportantWords(text) {
try {
const command = new DetectEntitiesCommand({
Text: text,
LanguageCode: "en"
});
const response = await client.send(command);
console.log("=== 重要な単語 ===");
response.Entities.forEach(entity => {
console.log(`単語: ${entity.Text}`);
console.log(`種類: ${entity.Type}`); // PERSON, ORGANIZATION, DATE など
console.log("---");
});
} catch (error) {
console.error("エラー:", error);
}
}
// 実行
async function test() {
const text = "John Smith signed the contract on January 15, 2024. This is a great deal!";
await checkSentiment(text);
await findImportantWords(text);
}
test();