【问题标题】:Sending an AlchemyData News query using Node.js (watson-developer-cloud module)使用 Node.js(watson-developer-cloud 模块)发送 AlchemyData News 查询
【发布时间】:2016-07-26 04:47:59
【问题描述】:

我目前正在使用 watson-developer-cloud Node.js SDK 处理 Node.js,但在发送包含实体的查询时遇到问题。

这是我的代码:

// require watson's node sdk and fs
var watson = require('watson-developer-cloud');
var fs = require('fs');

// Define output file
var outputJSONFile = '/home/vagrant/Desktop/node/dir/data.json';

// Create alchemy_data_news object using our api_key
var alchemy_data_news = watson.alchemy_data_news({
  api_key: ''
});

// Define params for the query and what values to return
// Accepted returne values:
// docs.alchemyapi.com/v1.0/docs/full-list-of-supported-news-api-fields
var params = {
  start: 'now-1m',
  end: 'now',
  count: 2,
  qs: ['q.enriched.url.enrichedTitle.entities.entity.text=apple'],
  return: ['enriched.url.url,enriched.url.title']
};

// Call getNews method and return json
alchemy_data_news.getNews(params, function (err, news) {
  if (err) {
    console.log('error:', err);
  } else {
    fs.writeFile(outputJSONFile, JSON.stringify(news, null, 2), function(err)   {
      if (err) {
        console.log('WriteFile Error:', err);
      } else {
        console.log("JSON saved to " + outputJSONFile);
      }
    });
  }
});

我仍在尝试弄清楚如何使用 params 对象发送实体参数。

在挖掘一些代码时,我遇到了 qs,所以我一直在使用它进行测试,但我根本没有成功。

非常感谢任何建议。

P.S:我正在尝试通过:
q.enriched.url.enrichedTitle.entities.entity.text=apple q.enriched.url.enrichedTitle.entities.entity.type=company

【问题讨论】:

  • 似乎通过此更改,代码现在可以正常工作: var params = { ... ... 'q.enriched.url.enrichedTitle.entities.entity.text': 'Apple' , 'q.enriched.url.enrichedTitle.entities.entity.type': '公司', ... };如果有更好的方法,请告诉我。谢谢!

标签: javascript node.js ibm-cloud ibm-watson alchemyapi


【解决方案1】:

如果您查看node-sdk source code for AlchemyDataNews,您会看到顶级参数作为查询字符串发送。
那么params的地图应该是:

var params = {
  start: 'now-1m',
  end: 'now',
  count: 2,
  return: ['enriched.url.url,enriched.url.title'],
  // fields here 
  'q.enriched.url.enrichedTitle.entities.entity.text': 'apple',
  'q.enriched.url.enrichedTitle.entities.entity.type': 'company'
};

【讨论】:

  • 非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多