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?