Search code examples
node.jsibm-cloudibm-watson

Bluemix: Node.js + Watson Concept Expansion API


I am trying to use the Watson Concept Expansion example app that was listed with the CE Documentation page from IBM DevOps Services.

The app generates an error when it tries to retrieve the results and I think I see why but am unsure what to do about it:

Here is the applicable code for retrieving the results:

// Get the job result by calling PUT to '/result' with the jobid
var job_result_async = function(jobid, onSuccess, onError) {
  // create a new map with the defaault https params
  var params = extend({},default_params);
  params.path += '/result';
  params.method = 'PUT';

  var watson_req = https.request(params, function(result) {
    var response_string = '';

    result.on('data', function(chunk) {
      response_string += chunk;
    });

    result.on('end', function() {
      var result_json = JSON.parse(response_string);

      // format results if exists
      if (util.isArray(result_json.return_seeds)) {
        var result_clean = result_json.return_seeds.map(function(e) {
          return { 'result' : clean_string(e.result),
            'prevalence': e.prevalence }
        });
      result_json.return_seeds = result_clean;
              }

      onSuccess(result_json);
    })

  });

  watson_req.on('error', function(e) {
    onError(e)
  });

  // send the data
  watson_req.write(JSON.stringify({'jobid': jobid}));
  watson_req.end();
};

Here is the documentation page for the /result REST API endpoint: Concept Expansion REST API for /result

In the function the jobid is never being passed as far as I can tell. I suspect this is the problem but if anyone can confirm this that would be great, and also I am unclear how I would pass the jobid to the /result API to be able to retrieve the results.

Thank you for any assistance! -Andy


Solution

  • The service was having issues when you tried few days ago. Looking at your code it should work. If you want to try the service you can use the watson-developer-cloud npm module as described below:

    var watson = require('watson-developer-cloud');
    
    var concept_expansion = watson.concept_expansion({
      username: '<username>',
      password: '<password>',
      version: 'v1'
    });
    
    var params = {
      seeds: ['motrin','tylenol','aspirin'],
      dataset: 'mtsamples',
      label: 'medications'
    };
    
    concept_expansion.expand(params, function (err, response) {
      if (err)
        console.log('error:', err);
      else
        console.log(JSON.stringify(response, null, 2));
    });
    

    I run the code above and got:

    {
      "return_seeds": [
        {
          "prevalence": "2.803636",
          "result": "penicillin"
        },
        {
          "prevalence": "2.748252",
          "result": "plan: 1"
        },
        {
          "prevalence": "2.685714",
          "result": "actos"
        },
        {
          "prevalence": "2.670330",
          "result": "lipitor"
        },
        {
          "prevalence": "2.649351",
          "result": "zocor"
        },
        {
          "prevalence": "1.400000",
          "result": "ibuprofen 200 mg three pills"
        },
        ...
      ]
    }
    

    Concept-Expansion demo: http://watson-ce-demo.mybluemix.net/