情感分析獲取文字中特定短語的情緒資訊(Node.js)

AlchemyLanguage 的 Targeted Sentiment 功能可以在你的內容中搜尋目標短語並返回每個結果的情緒資訊。

此示例需要 AlchemyLanguage 服務憑據Node.js

  1. 使用命令列介面安裝 Watson Developer Cloud Node.js SDK
$ npm install watson-developer-cloud
  1. 將以下程式碼儲存到同一目錄中的 app.js 檔案中。確保使用 AlchemyAPI 金鑰替換 API_KEY
var AlchemyLanguageV1 = require('watson-developer-cloud/alchemy-language/v1');
var alchemy_language = new AlchemyLanguageV1({
  api_key: 'API_KEY'
})

var parameters = {
  text: 'Grapes are the best! I hate peaches.',
  targets: [
    'grapes',
    'peaches'
  ]
};

alchemy_language.sentiment(parameters, function (err, response) {
  if (err)
    console.log('error:', err);
  else
    console.log(JSON.stringify(response, null, 2));
});
  1. 執行應用程式:
$ node app.js