5 Ways to develop a Sentiment Analyser in Machine Learning | by Satyam Kumar | Towards Data Science

Sentiment Analysis is a Natural Language Processing technique to determine the sentiment or opinion of a given text. A sentiment analysis model can predict whether a given text data is positive, negative, or neutral by extracting meaning from the natural language and assigning it to a numerical score.
There are various ways to develop or train a sentiment analysis model, in this article we will discuss 5 different ways:
Custom Trained Supervised Model
TextBlob
Word-Dictionary based model
Bert
Named Entity based Sentiment Analyzer
Sentiment analysis is used by various organizations to understand the sentiment of their customers, using reviews, social media conversations, and to more fast and accurate business decisions accordingly.
You can train a custom machine learning or deep learning sentiment analysis model. A Labeled dataset is the key requirement to train a robust ML model. The ML model will learn various patterns in the dataset and can predict sentiment for given unseen text.
To train a custom sentiment analysis model, one must follow the following steps:
Collect raw labeled dataset for sentiment analysis.
Preprocessing of text
Numerical Encoding of text
Choosing the appropriate ML algorithm
Hypertuning and Training ML model
Prediction
Read the below article, to know how to develop a movie review sentiment analysis model using the Naive Bayes classifier algorithm.
satyam-kumar.medium.com
TextBlob is an open-sourced Python library used to process textual data and allows you to specify which algorithms you want to use under the hood of its simple API. TextBlobs’s API can be used to perform tasks such as part-of-speech tagging, noun phrase extraction, classification, translation, sentiment analysis, etc.
For sentiment analysis, the TextBlob library provides two implementations:
PatternAnalyzer: (Default) Based on pattern library.
NaiveBayesAnalyzer: An NLTK classifier trained on movie review corpus.
pip install -U textblob
It involves creating an n-gram dictionary of positive and negative words from the text corpus. This method requires a labeled text corpus, and use custom python functions to create a dictionary of n-gram words for positive and negative text separately.
Custom words can be also added to the dictionary based on domain knowledge that serves as an added advantage.
In the next step, create a custom function that can use the above-formed dictionary of positive and negative words to analyze the given input text and can be classified as a positive or negative sentiment.
Each positive word present in the input text increments the sentiment score, and the negative word decrements the sentiment score.
Divide the final sentiment score with the number of words in that text to normalize the score.
A positive sentiment score ranging between 0 to 1, depicts a positive sentiment, where 1 being positive sentiment prediction with 100% confidence. Whereas, a negative sentiment score ranges between -1 to 0, where -1 being negative sentiment prediction with 100% confidence.
BERT stands for Bidirectional Encoder Representations from Transformers developed by Google, and it is a state-of-the-art ML model used for NLP tasks. To train a sentiment analysis model using BERT follow the steps:
Install Transformers Library
Load the BERT classifier and Tokenizer
Create a processed dataset
Configure and train the loaded BERT model and fine-tune its hyperparameters
Make sentiment analysis predictions
Follow the below-mentioned article for implementation of sentiment analysis model with BERT.
towardsdatascience.com
Named Entity based Sentiment Analyzer is mainly targetted towards entity or importance words. It can be also referred to as target sentiment analysis, and is more accurate and useful than the above three mentioned methods, as it only focuses on important words or entities.
The first step is to find all the named entities in the text corpus.
Apply name entity recognition on the text to find various entities such as PERSON, ORG, GPE.
Sentiment Analysis based on top named entities.
Targetted by finding sentences containing the named entities and performing sentiment analysis only on those sentences one by one.
In this article, we have discussed 5 different ways to develop a sentiment analysis model. Let us understand that no one method is a hard and fast rule to follow while developing a sentiment analysis model. It needs planning and tweaking of the algorithms according to the problem statement and dataset.
[1] BERT Wiki: https://en.wikipedia.org/wiki/BERT
[2] Sentiment Analysis using BERT by Orhan G. Yalçın: https://towardsdatascience.com/sentiment-analysis-in-10-minutes-with-bert-and-hugging-face-294e8a04b671
Thank You for Reading