watson-developer-cloud / tone-analyzer-nodejs

Sample Node.js Application for the IBM Tone Analyzer Service
https://tone-analyzer-demo.ng.bluemix.net/
Apache License 2.0
453 stars 274 forks source link

Issue with tone-analyzer api endpoint #79

Closed loretoparisi closed 8 years ago

loretoparisi commented 8 years ago

Hi, I have seen this configuration of the tone analyzer demo here:

// Create the service wrapper
      this.analyzer = watson.tone_analyzer({
        url: 'https://gateway.watsonplatform.net/tone-analyzer/api/',
        username: '<username>',
        password: '<password>',
        version_date: '2016-05-19',
        version: 'v3'
      });

But in the credential panel I see

    "credentials": {
        "url": "https://gateway.watsonplatform.net/tone-analyzer-beta/api",
        "password": "<password>",
        "username": "<username>"
    }
}

The demo app returns a whole set of meta data like sentences_tone, tone_categories ,that is the first endpoint, the last endpoint (the beta) has no sentence_tone. Is that correct?

Problem is that with the same developer account, I cannot login on the first endpoint, that is the one coming from the credentials, it seems like the demo app I have deployed is using the other endpoint, while with same credentials I cannot use this one programmatically.

loretoparisi commented 8 years ago

[UPDATE] I have created a brand new tone-analyzer service just to be sure that it was not due to any wrong configuration, now Bluemix gives me back these credentials

{
  "credentials": {
    "url": "https://gateway.watsonplatform.net/tone-analyzer/api",
    "password": "<password>",
    "username": "<username>"
  }
}

So I have the production url, not the beta. By the way, using the same credentials of the demo app I have deployed, for some weird reason gives me back no sentences_tone like before...

The only difference I can see is the plan that are beta and standard. Is there anything else I have to do to get the right api (whole) response?

herchu commented 8 years ago

The credentials panel shows the URL and user/password for the service you have instantiated (and bound to the app). The demo app needs to make sure it requests the same service you have bound the app to.

The first snippet above shows your app requests version: "v3" which searches for the production version. The second snippet suggests your app is still bound to a Beta service. Your update, or third snippet, shows that once you deleted the old service instance (beta) created a new one from production. This is all ok -- does the app work in the current form?

The confusion may come from having started this app earlier than 2016-05-19 when the new production version was published. Since May 19th, you are unable to create more Beta instances. Tone Analyer is now GA (not Beta), and note that you can still do 1000 API calls for free, for evaluation purposes.

Closing the issue now. Please reopen if you are still having trouble.

loretoparisi commented 8 years ago

@herchu it makes sense, that's why I have a created a standard version of the tone-analyzer so paid account. The problem is that I get the same response with this configuration as well:

{
  "credentials": {
    "url": "https://gateway.watsonplatform.net/tone-analyzer/api",
    "password": "<password>",
    "username": "<username>"
  }
}

I'm in the us-south region, just to know.

herchu commented 8 years ago

I am sorry I am confused now: What is "the same response" ? No sentences_tone element?

In that case, can you share the input (text) you are analyzing?

loretoparisi commented 8 years ago

@herchu yes the missing response is the sentences_tone. This is the simple module I did:

/**
* @author Loreto Parisi (loretoparisi at gmail dot com)
*/
(function() {

  var watson  = require('watson-developer-cloud');

  var ToneAnalyzer;
  ToneAnalyzer = (function() {

    function ToneAnalyzer(options) {

      // default options
      this._options = {

      };
      //override
      for (var attrname in options) { this._options[attrname] = options[attrname]; }

      // Create the service wrapper
      this.analyzer = watson.tone_analyzer({
        url : 'https://gateway.watsonplatform.net/tone-analyzer/api',
        username: '#####',
        password: '#######',
        version_date: '2016-05-19',
        version: 'v3'
      });

    }

    /**
     * Get Text tone
     * @param options Object Api parameters: { text := string }
     * @param callback Function Result callback
     */
    ToneAnalyzer.prototype.GetTone = function(options, callback) {
      var self=this;

      var extras={};
      for (var attrname in options) { extras[attrname] = options[attrname]; }

      this.analyzer.tone(extras, function(err, data) {
        if (err)
          return callback(null,err);
        else
          return callback( data );
      });
    }

    return ToneAnalyzer;

  })();

  module.exports = ToneAnalyzer;

}).call(this);

and the example is

toneAnalyzer = new ToneAnalyzer({});

  var lyricsBody = "Baby I like your style\nGrips on your waist\nFront way, back way\nYou know that I don't play\nStreets not safe\nBut I never run away\nEven when I'm away";

  var parameters = { text : lyricsBody };

  console.log( JSON.stringify(parameters,null,2) );

  toneAnalyzer.GetTone( parameters
  , function(results, error) {
      if(results) {
        console.log( JSON.stringify(results,null,2) );
      }
      else {
        console.error( error );
      }
  });

This is the response json, in the comment the input text related to this json.

herchu commented 8 years ago

Ok, the problem is that your text is seen as a single sentence, so the sentences_tone output is just skipped. This is because there are (single) end-of-line chars, with no period, so text is taken as single sentence as a whole.

Lyrics and poetry trick the sentence parser in Tone Analyzer. You will need to add a period to forcefully end each sentence and the end-of-line, or alternatively leave a blank line between sentences. These two versions work when replacing the second line of your example:

  // Use periods
  var parameters = { text : lyricsBody.replace(/\n/g, ".\n") };
  // Or use double EOLs
  var parameters = { text : lyricsBody.replace(/\n/g, "\n\n") };

Looking forward to seeing the outcome of your analysis... or any new feature in musixmatch ;)

loretoparisi commented 8 years ago

@herchu great it makes sense thank you! I have updated resulting json here. Absolutely we will, Watson makes a lot of sense for us ;)