Watson Natural Language Understanding
Text AnalysisUsing IBM Watson's Natural Language Understanding API
IBM Watson's Natural Language Understanding API provides powerful features for analyzing text, including sentiment analysis, entity recognition, and keyword extraction. In this blog, we will explore how to use this API and include some example code in JavaScript.
Getting Started
Before we start writing code, we need to obtain an API key from IBM Watson. You can sign up for a free account on the IBM Cloud website, and then create a Natural Language Understanding instance from the catalog. Once you have the instance created, you can view your API credentials by clicking on the "Service credentials" tab.
API Endpoint
The endpoint for the Natural Language Understanding API is https://api.us-south.natural-language-understanding.watson.cloud.ibm.com
. Note that the URL for your own instance may differ slightly depending on the region you selected when creating the instance.
Example Code
Here are some code snippets that demonstrate how to use the Natural Language Understanding API with JavaScript. Make sure to replace YOUR_API_KEY
with your own API key.
Analyzing Sentiment
To analyze the sentiment of a text, we can use the analyze
endpoint and set the features.sentiment
parameter to true
.
const NLU_URL = 'https://api.us-south.natural-language-understanding.watson.cloud.ibm.com';
const NLU_APIKEY = 'YOUR_API_KEY';
const text = 'I am so excited to be writing this blog!';
const url = `${NLU_URL}/v1/analyze?version=2021-03-25`;
const params = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${btoa(`apikey:${NLU_APIKEY}`)}`
},
body: JSON.stringify({
text: text,
features: {
sentiment: {
'document': true
}
}
})
};
fetch(url, params)
.then(response => response.json())
.then(data => {
console.log(data.sentiment.document);
})
.catch(error => console.error(error));
Extracting Keywords
To extract keywords from a text, we can use the analyze
endpoint and set the features.keywords
parameter to true
.
const NLU_URL = 'https://api.us-south.natural-language-understanding.watson.cloud.ibm.com';
const NLU_APIKEY = 'YOUR_API_KEY';
const text = 'I am planning a trip to the beach this summer and I need to buy some sunscreen and a swimsuit.';
const url = `${NLU_URL}/v1/analyze?version=2021-03-25`;
const params = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${btoa(`apikey:${NLU_APIKEY}`)}`
},
body: JSON.stringify({
text: text,
features: {
keywords: {}
}
})
};
fetch(url, params)
.then(response => response.json())
.then(data => {
data.keywords.forEach(keyword => {
console.log(keyword.text);
});
})
.catch(error => console.error(error));
Recognizing Entities
To recognize entities in a text, we can use the analyze
endpoint and set the features.entities
parameter to true
.
const NLU_URL = 'https://api.us-south.natural-language-understanding.watson.cloud.ibm.com';
const NLU_APIKEY = 'YOUR_API_KEY';
const text = 'Albert Einstein was a German theoretical physicist who developed the theory of relativity.';
const url = `${NLU_URL}/v1/analyze?version=2021-03-25`;
const params = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${btoa(`apikey:${NLU_APIKEY}`)}`
},
body: JSON.stringify({
text: text,
features: {
entities: {}
}
})
};
fetch(url, params)
.then(response => response.json())
.then(data => {
data.entities.forEach(entity => {
console.log(entity.text);
});
})
.catch(error => console.error(error));
Conclusion
In this blog post, we learned how to use IBM Watson's Natural Language Understanding API to analyze text. We included some example code in JavaScript for analyzing sentiment, extracting keywords, and recognizing entities. With the power of this API, we can easily gain insights from text data and use it to drive decision-making in various applications.