情感分析获取文本中特定短语的情绪信息(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