Perform Sentiment Analysis in JavaScript with A Low-Code API

Perform Sentiment Analysis in JavaScript with A Low-Code API

As your company grows, so does your customer data. And keeping track of it becomes even more complicated, leading you to miss out on valuable insights. Fortunately, with tools like this sentiment analyzer, you can sort data in real-time and understand how customers feel about your brand and products.

Sentiment analysis captures the emotional tone of customer’s opinions – like those in product reviews, social media posts, and online surveys – and assigns them a positive or negative sentiment score. 

To build a sentiment analysis model in JavaScript using an open-source library, such as SpaCy-js or TensorFlow.js, you’ll need to be well-versed in machine learning or have a competent team of data scientists. Designing, training, and running your solution could take up to several months, so you won’t be able to make use of your tools right away either. 

SaaS tools for sentiment analysis, on the other hand, are ready-to-use solutions and more cost-effective. With a low-code cloud platform like MonkeyLearn, you can get started right away with pre-trained models. Or, you can build your own customized models (code free and no machine learning expertise needed). Finally, you can manage your models using an API in Javascript.

Below, we’ll walk you through how to do sentiment analysis in JavaScript with MonkeyLearn.

Tutorial: How to Do Sentiment Analysis in JavaScript

MonkeyLearn’s JavaScript API is a fast, low-code solution, which you can use to call MonkeyLearn’s pre-built sentiment analysis model in just a few simple steps. 

Depending on your needs, you might want to build your own custom sentiment analysis model. For example, if your data contains industry-specific language, you might want to train your own model to recognize specific words and expressions that convey opinion polarity. 

Let’s walk you through the steps of using MonkeyLearn’s API, then we’ll show you how to build your own sentiment analysis model:

  1. Sign up to MonkeyLearn for free to get started.

  2. In the “Explore” tab you’ll see all the available demo models. Click on the Sentiment Analysis model.

  3. Go to the “API” tab to get your API key:

    Details of MonkeyLearn's sentiment analysis API (model ID and API key)
  4. Install the Node.js client for the MonkeyLearn API via npm:

    $ npm install monkeylearn
    

And that’s it! Now you are all set up to use the pre-trained model. 

  1. Use MonkeyLearn’s sentiment analysis model on your own texts by entering this Javascript code:

    const MonkeyLearn = require('monkeylearn')
    
    const ml = new MonkeyLearn('[YOUR_API_KEY HERE]')
    let model_id = 'cl_pi3C7JiL'
    let data = ['This is a great tool!', {text: 'The location is excellent.', external_id: 'ANY_ID'}]
    ml.classifiers.classify(model_id, data).then(res => {
        console.log(res.body)
    })
    
  2. You will get a response that looks like this:

    [
        {
            "text": "This is a great tool!",
            "external_id": null,
            "error": false,
            "classifications": [{
                "tag_name": "Positive",
                "tag_id": 33767179,
                "confidence": 0.998
            }]
        }, {
            "text": "The location is excellent.",
            "external_id": null,
            "error": false,
            "classifications": [{
                "tag_name": "Positive",
                "tag_id": 33767179,
                "confidence": 0.562
            }]
        }
    ]
    

As you can see, above, each piece of text is assigned a classification result which includes:

  • Tag_name: the predicted classification label. “Positive”, “Negative”, or “Neutral”.
  • Tag_ID 
  • Confidence: a numeric value (ranging from 0.0 to 1.0) that indicates the confidence of the prediction, in which 1.0 is the highest.

Check out the API documentation to learn more about how to use MonkeyLearn’s API.

Building a Customized Model for Sentiment Analysis

If you choose to build your own sentiment analysis classifier, you will need some training data. It needs to be relevant to the task, so you’ll need data containing plenty of opinions, such as customer reviews, survey responses, etc. 

You will use this data to train your machine learning model until it starts recognizing patterns and making its own predictions.

Ready to get started? If you haven’t already, sign up to MonkeyLearn and follow these steps:

  • Choose a model type. Go to the dashboard, click on “Create a model” and choose “Classifier”.

    MonkeyLearn's creation wizard, with the option to choose a classifier or extractor.
  • Define a type of classifier. Since you want to classify data by sentiment, choose “Sentiment analysis”.

    MonkeyLearn's creation wizard showing classification options: topic, sentiment, and intent.
  • Upload your data. You can upload a CSV or an Excel file. 

    MonkeyLearn's creation wizard showing available sources to upload your text data.
  • Train your sentiment analysis model. Manually tag your data as positive, negative, or neutral. After tagging a few examples, your model will start making its own predictions. If they are incorrect, you can re-tag them. 

    The training process showing data tagged manually.
  • Test your classifier. Once you’ve trained your model with some examples, it will be ready for you to try out. Just paste some text into the classifier and click on “Classify text” to test your model. If you are not satisfied with the results, you can keep tagging more examples to make your model smarter.

Test with your own text

Results

TagConfidence
Positive99.9%
  • Put your model to work: call your model API with JavaScript. On the “Run” tab, click on “API” and get the API key. From here, you just need to follow the same steps that we explained earlier to connect pre-trained models. 

    Details of MonkeyLearn's sentiment analysis API (model ID and API key)

Closing 

Sentiment analysis gives you a clear idea of how customers perceive your brand, and what they expect from your products. It’s a powerful tool that can help make important business decisions and lead to happier, more loyal customers.

Instead of building a sentiment analysis model from scratch, you can use machine learning tools like MonkeyLearn. They’re just as powerful, more cost-effective, and ready to use straight away. With APIs in Javascript to connect and manage sentiment analysis models, you can use machine learning tools alongside your existing stack.

Sign up to MonkeyLearn for free to start analyzing sentiment in your data!

Tobias Geisler Mesevage

June 29th, 2020

Posts you might like...

MonkeyLearn Logo

Text Analysis with Machine Learning

Turn tweets, emails, documents, webpages and more into actionable data. Automate business processes and save hours of manual data processing.

Try MonkeyLearn
Clearbit LogoSegment LogoPubnub LogoProtagonist Logo