Stormsson
Stormsson

Reputation: 1551

How to get the full answers when using the Watson Q&A service with the watson-developer-cloud package?

I am calling the IBM Watson Question and Answer API using the watson-developer-cloud npm module. My code that invokes the service is as follows:

question_and_answer_healthcare.ask({
  text: 'What are the benefits of taking aspirin daily?'}, function (err, response) {
  if (err)
    console.log('error:', err);
  else {
    console.log(JSON.stringify(response, null, 2));
  }
});

In the printed response, I get an array of answers that only list the evidence used, but not the actual answer. Here is an example answer element:

{
    "id": 0,
    "text": "373C41A4A1E374B1BE189D07EF707062 - Taking Aspirin Every Day : Taking Aspirin Every Day : The Basics : What are the benefits of taking aspirin daily?",
    "pipeline": "Descriptive,TAO",
    "confidence": 0.96595,
    "entityTypes": []
}

How do I get the full answer text?

Upvotes: 2

Views: 319

Answers (2)

dalelane
dalelane

Reputation: 2765

TL;DR When you submit your question, you POST a questionText in a question object. In that question object, add formattedAnswer and set it to true.

e.g.

{
    "question" : {
        "questionText" : "This is your question",
        "formattedAnswer" : true,
        ...

When using this option, you can retrieve the full, formatted HTML of the returned answers as follow:

response[0].question.answers.forEach(function(answer, index) {
  console.log(answer.formattedText);
})

You will need to parse out the necessary information from the HTML that you would like to display to the user.

(API doc ref : https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/question-and-answer/question )

Background

The Watson QA API is returning the ID and title of the section of a document that it thinks matches your question. That's the text you're seeing come back. It's in the format <ID> - <TITLE>.

This format is fine for machine-to-machine use, such as if you're planning to look up the answer from somewhere.

But if you want an answer you can display to a user, the formattedAnswer flag gives you that. It tells the API to return the contents of that section of document, not just the ID and title.

The API response payload also includes the supporting evidence that led Watson to have confidence in the answers it returns. You can often find the document section for an answer in there, too - but I'd suggest it's best not to rely on that. If you want the contents of the answer, the formattedAnswer flag is the documented, reliable, will-always-be-there way to get it.

In your specific case, as you're using a wrapper library that doesn't let you set this, I guess that leaves you the workaround of returning supporting-evidence instead of the answer, or building the POST request for the QA API yourself.

Upvotes: 4

Jake Peyser
Jake Peyser

Reputation: 1190

Note: In the case of your question on the healthcare Watson QA beta corpus, the following technique works. However, there are instances where the returned evidence and answers do not align. The following should be considered a workaround to using the formattedText option and should not be relied upon as a foolproof method.

When using version 0.9.20 of the watson-developer-cloud package, you can not set any optional inputs in your HTTPS request body. Therefore, you should use the following method to get your answers:

The full answer text to the question is not located in the response[0].question.answers[] array, but instead located in the response[0].question.evidencelist[X].text attribute of each entry in the evidence list. You will find the associated confidence in the response[0].question.evidencelist[X].value field. In order to retrieve these values in your scenario, you would parse it like the following full sample:

var watson = require('watson-developer-cloud');
var question_and_answer = watson.question_and_answer({
  version: 'v1',
  dataset: 'healthcare',
  username: 'YOUR USERNAME'
  password: 'YOUR PASSWORD'
});

question_and_answer.ask({
  text: 'What are the benefits of taking aspirin daily?'}, function (err, response) {
    if (err)
      console.log('error:', err);
    else {
      // Print the answer with the highest confidence
      console.log(response[0].question.evidencelist[0].text);

      //Print all returned answers
      response[0].question.evidencelist.forEach(function(answer, index) {
        console.log(index, '. ', answer.text);
      })
    }
});

Upvotes: 2

Related Questions