1
function extractKeywords(data, fn){

    var NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');
    var natural_language_understanding = new NaturalLanguageUnderstandingV1({
        'username': 'hidden',
        'password': 'hidden',
        'version_date': '2017-02-27'
    });

    var parameters = {
        'text': data,
        'features': {
            'entities': {
                'emotion': false,
                'sentiment': false,
                'limit': 10
        }
    };

    keys = null;

    natural_language_understanding.analyze(parameters, function(err, response) {
        if (err) {
            console.log('error:', err);
        }
        else {
            keys = getAll(response); // "getAll" is a function that returns the keywords as a string
            fn(keys);
        }
    });

}

Now, the next two following calls output something that confuses me:

extractKeywords("Abaktal Prospectus, tablets \\n What is Abaktal and what it is used for? nIndications \\n  Infection caused ",
    function(keyss){
        return keyss;
    }
);

outputs:

empty console

while

extractKeywords("Abaktal Prospectus, tablets \\n What is Abaktal and what it is used for? nIndications \\n  Infection caused ",
    function(keyss){
        console.log(keyss);
    }
);

outputs:

Abaktal Prospectus,tablets,Infection,nIndications /health and fitness/disease/aids and hiv,/technology and computing/hardware/computer/portable computer/tablet,/technology and computing/consumer electronics/portable entertainment City,,Abaktal,Location

Which is the correct output.

This is my first javascript project, so I understand that

natural_language_understanding.analyze(parameters, function(err, response) {
            if (err) {
                console.log('error:', err);
            }
            else {
                keys = getAll(response); // "getAll" is a function that returns the keywords as a string
                fn(keys);
            }
        });

Involves asynchronous execution, with which I try to deal by using the

fn(keys);

Obviously, there's an issue in my code that provokes this behavior, because my desired output would be to get:

Abaktal Prospectus,tablets,Infection,nIndications /health and fitness/disease/aids and hiv,/technology and computing/hardware/computer/portable computer/tablet,/technology and computing/consumer electronics/portable entertainment City,,Abaktal,Location

by using this:

extractKeywords("Abaktal Prospectus, tablets \\n What is Abaktal and what it is used for? nIndications \\n  Infection caused ",
    function(keyss){
        return keyss;
    }
);

Any solution on what I should change/reformat to achieve this?

George Cernat
  • 1,323
  • 9
  • 27
  • @Li357 "Obviously, there's an issue in my code that provokes this behavior, because my desired output would be to get: ... Any solution on what I should change/reformat to achieve this?" would be what I need – George Cernat Feb 12 '18 at 12:47
  • Use the return value? `var ret = fn(keys); console.log(ret);`? – Andrew Li Feb 12 '18 at 12:50
  • @Li357 But that means that I can use the value only inside that extractKeywords function scope, but I need access to the keywords outside of that, something like: extractKeywords(textHere, function(keyss){ var k = keyss; // I know this doesn't work } ); and then use k as the return value – George Cernat Feb 12 '18 at 12:57
  • What do you mean? If you call the function *with an argument*, it will be in scope... `fn(keys)` passes `keys` to `fn`... – Andrew Li Feb 12 '18 at 13:06
  • @Li357 but how will I be able to use it out of that scope? Say for instance that I want to use that keys in anothet fuction, how do I achieve that? – George Cernat Feb 12 '18 at 13:25
  • Pass it in as an argument? – Andrew Li Feb 12 '18 at 13:27
  • @GeorgeCernat You cannot. You have to put all code that uses the keys in the place where you had the working `console.log` (or call the code from there). – Bergi Feb 12 '18 at 14:16
  • @Bergi Right, I understand now, I was a little bit confused by how it all works, but I saw it through, finally, thanks. – George Cernat Feb 12 '18 at 14:47
  • @Li357 Right, I understand now, I was a little bit confused by how it all works, but I saw it through, finally, thanks. If you want to post an answer with the clarifications you posted in these comments, I would gladly accept it. – George Cernat Feb 12 '18 at 14:47

0 Answers0