This tutorial aims to guide you on how to integrate Natural Language Processing (NLP) capabilities into your HTML application using APIs and JavaScript libraries.
By the end of this tutorial, you will be able to:
Before you start, you should have a basic understanding of:
In this section, we will use the "compromise" JavaScript library and the "sentim-API" to integrate NLP into our application. We will also display the sentiment of a given text in our HTML.
NLP, or Natural Language Processing, is a branch of AI that gives the machines the ability to read, understand and derive meaning from human languages. It involves several tasks such as sentiment analysis, part-of-speech tagging, named entity recognition, etc.
Compromise is a modest-sized NLP library, for the browser, that has a lot of features. You can include it in your project by adding <script src="https://unpkg.com/compromise"></script>
in your HTML.
Sentim-API is a free API that can be used for text sentiment analysis. You can use the Fetch API to make a POST request to https://sentim-api.herokuapp.com/api/v1/
.
Here is an example of how you can use the compromise library:
<!DOCTYPE html>
<html>
<body>
<h2 id="demo">JavaScript in Body</h2>
<script src="https://unpkg.com/compromise"></script>
<script>
let doc = nlp('London is a big city in the United Kingdom.');
console.log(doc.topics().out('array')); // ['London', 'United Kingdom']
</script>
</body>
</html>
In this example, we use the nlp
function from the compromise library to perform NLP on the provided text. The topics
method extracts the main topics from the text.
Here is an example of how you can use Sentim-API:
fetch('https://sentim-api.herokuapp.com/api/v1/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({text: 'I love programming!'})
})
.then(response => response.json())
.then(data => console.log(data));
This example sends a POST request to Sentim-API, and logs the response. The response includes the sentiment of the provided text.
In this tutorial, we discussed how to integrate NLP into your HTML application using the compromise library and Sentim-API. We also learned how to display the results of these processes in HTML.
Display the topics of a user-input text using the compromise library.
Display the sentiment of a user-input text using Sentim-API.
Combine both the above: ask the user to input a text, display the main topics of the text and its sentiment.
Keep practicing and exploring more on the NLP libraries and APIs. You can also try to integrate other NLP tasks into your application, such as named entity recognition, language translation, etc.