Sentiment Analysis in Golang

In this tutorial,which is part of the series Go4DataScience and Go4NLP in which we experiment with Go for doing Data Science, we will be performing sentiment analysis in Golang.

By the end of this tutorial you will understand and learn about :

  • What sentiment analysis is
  • Why sentiment analysis
  • Its applications.
  • How to perform sentiment analysis using several methods
  • How to interpret a sentiment score of a text
  • And more

So let us begin.

What is Sentiment Analysis?

Sentiment analysis is the process of identifying the emotions or sentiment of a text. It involves the activity of classifying a text as either positive ,negative or neutral. In order words you are trying to know how someone( the speaker or writer of a text) feels via the text provided. It is a type of Text Classification – classifying texts or sentences into categories or special groups.

Why Sentiment Analysis?

So why sentiment analysis? What is the point of performing sentiment analysis on a text or tweet or a sentence?

The main purpose of sentiment analysis is to identify the emotions within a text. This is quite useful especially when you want to know what people think about your product or service. It is a key part of opinion mining : the act of extracting the opinion and judgements of people.

It can be useful in analysing reviews of customers to help marketers and producers restructure their product and know where to fix and the next steps to take for better sales.

By analysing the text given by clients and customers such as product reviews, tweets or comments – we are able to know what people feel about our service or product and make the necessary changes.

Let us see how to perform sentiment analysis using Golang

How to Perform Sentiment Analysis

There are two main ways of identifying the sentiment of a given text. These include

  • Rule Based /Lexicon Based Approach: using a lexicon of positive and negative words and calculating their occurrence and valency in a text.
    • Vader Sentiment (Valence Aware Dictionary and sEntiment Reasoner)
  • Machine Learning(ML) Based Approach: using a human labelled data as a base to train an ML model to predict or classify the text into their respective sentiment.
    • Trained ML or Deep Learning model

Sentiment Analysis Using Vader ( Rule Based – Lexicon Based Approach)

We will be using the vader-go package to perform this type of sentiment. As we stated above it uses a lexicon of positive and negative words and generate a score of the text.

Let us install the vader-go package in our workspace from our terminal/cmd via

go get github.com/grassmudhorses/vader-go

The basic workflow is that we will have to parse the text and compare it with our lexicon of words (lexicon.DefaultLexicon)and then apply our vader rule to our parsed text in order to get the sentiment scores.

package main

import (
    "fmt"
    "github.com/grassmudhorses/vader-go/lexicon"
    "github.com/grassmudhorses/vader-go/sentitext"
)

func main(){
    mytext := "I love apples and coding so much"
    parsedtext := sentitext.Parse(mytext, lexicon.DefaultLexicon)
    sentiment := sentitext.PolarityScore(parsedtext)
    fmt.Println("Pos:",sentiment.Positive)
    fmt.Println("Neg:",sentiment.Negative)
    fmt.Println("Neu:",sentiment.Neutral)
    fmt.Println("Compound/Final Sentiment:",sentiment.Compound)
}
Intepreting Sentiment Score or Polarity

In interpreting the result we can see that we have a score for Positive, Negative ,Neutral and Compound. These indicate the percentage of positive,negative and neutral sentiment in our text. The Compound is the final sentiment or polarity score depicting what our text is. If it is a negative value then that means we have a negative sentiment and positive value means a positive sentiment.

Let us move on to the next method Using an ML Based Approach

Sentiment Analysis Using ML Based Approach (Pretrained Model)

This method involves either using a pretrained model that have been trained on an already labelled dataset. You can either use a Machine Learning Estimator such as Naive Bayes or any classification algorithm or use Deep Learning to build a predictive sentiment model.

In our case we will be using the sentiment package from cdipaolo which has been trained on the IMDB reviews dataset . We can install it via

go get github.com/cdipaolo/sentiment

The basic workflow for this package is to either use either the .Train() or .Restore() and then apply the model to classify the sentiment. You wll have to specify the language to get accurate results.

  • .Train(): used when within the library
  • .Restore(): used anywhere

let us see how to perform a sentiment analysis on our text.

package main

import (
    "fmt"

    "github.com/cdipaolo/sentiment"
)

func main() {
    mytext := "I like eating mangoes and coding"

    // Model : restore or train(project directory)
    sentimentModel, err := sentiment.Restore()
    if err != nil {
        panic(err)
    }
    results := sentimentModel.SentimentAnalysis(mytext, sentiment.English)
    fmt.Println(results)
    // Sentiment For the Entire Sentence
    fmt.Println("Sentiment Score:", results.Score)

}

To conclude we have seen two main ways we can perform sentiment analysis in golang. You can now compile or run your go file via go run main.go and get the results. Cool! Right?

To get more on Go4DataScience and Go4NLP you can check out this resource and other resources for NLP in Python.

You can also check the video tutorial below

Thanks For Your Time
Jesus Saves
By Jesse E.Agbe(JCharis)

2 thoughts on “Sentiment Analysis in Golang”

  1. It is necessary to know about what people think about your product or service and sentiment analysis in golang helps us exactly to know about it in detail. The information that you have shared about sentiment analysis in golang will help us out in knowing about how to perform sentiment analysis in golang.
    I hope that the information that you have shared will help out many people with this analysis and make it easy for them to create great success for their businesses. Keep sharing more such helpful articles in the future also.

Leave a Comment

Your email address will not be published. Required fields are marked *