Issue
I am trying to create a keyword analysis using Watson NLP and JS.
I tried the following code line but the result says ReferrenceError{}
and I have no idea on how to make it work..
var keywords=response.result.keywords;
print(keywords);
createElement("h3", "Main keywords of this synopsis");
nbkeywords = 3;
createP("Keywords in this synopsis are:");
createP(keywords[i].text);
}
Solution
This is an example of JSON response from the keywords feature of the Watson NLU API (reference):
{
"usage": {
"text_units": 1,
"text_characters": 1536,
"features": 1
},
"keywords": [
{
"text": "curated online courses",
"sentiment": {
"score": 0.792454
},
"relevance": 0.864624,
"emotions": {
"sadness": 0.188625,
"joy": 0.522781,
"fear": 0.12012,
"disgust": 0.103212,
"anger": 0.106669
}
},
{
"text": "free virtual server",
"sentiment": {
"score": 0.664726
},
"relevance": 0.864593,
"emotions": {
"sadness": 0.265225,
"joy": 0.532354,
"fear": 0.07773,
"disgust": 0.090112,
"anger": 0.102242
}
}
],
"language": "en",
"retrieved_url": "https://www.ibm.com/us-en/"
}
Meaning that the "keywords" key in the JSON response is an array containing other JSON objects. To print all keywords you need to loop this array, like shown below with the use of a "for" statement:
var keywords = response.result.keywords;
...
createElement("h3", "Main keywords of this synopsis");
createP("Keywords in this synopsis are:");
var numberOfKeywords = keywords.length;
for (var i = 0; i < numberOfKeywords; i++) {
createP(keywords[i].text);
}
In the official Watson NLU documentation there are Javascript examples that could also help you understand the service API. See https://cloud.ibm.com/apidocs/natural-language-understanding?code=node#keywords.
I hope this answer helps you.
Answered By - Vanderlei Munhoz Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.