Sentiment refers to a writer's attitude in a text. And sentiment analysis is analyzing or deducing the writer's sentiment based on the text. It is one of the common techniques used in natural language processing tasks.
We can deduce multiple sentiments from a text passage. However, a more straightforward classification would be to separate the text into either positive, negative, or neutral categories.
The following table demonstrates how text can be classified into the categories mentioned above:
Positive | Negative | Neutral |
We like reading books. | We don't like reading books. | We are reading a book. |
We feel great this morning. | We feel bad this morning. | We have to go to work. |
This is our favorite food. | This food is horrible. | Here is our food. |
Sentiment analysis is used for classification because of the following reasons:
It stores data in an efficient, cost-friendly.
It solves real-time issues and helps in solving real-time scenarios.
The following are the types of sentiment analysis:
Fine-grained: It is dependent on the polarity. The text can be classified as very positive, positive, neutral, negative, or negative on a scale from 1 to 5.
Emotion detection: The sentiment happy, sad, angry, upset, jolly, pleasant, and so on comes under emotion detection.
Aspect-based sentiment analysis: It focuses on a particular aspect.
Multilingual sentiment analysis: It consists of different languages where the classification is done as positive, negative, and neutral.
Similar to standard classification, text classification involves input data and label training pairs. In this case, the input data will be tokenized text sequences, and each text sequence will be labeled with a category. For simplicity, the category labels are just integers in the range where
The following example shows three training pairs:
Here, the maximum length of the sequence is four, and it is binary classified.
We can implement sentiment analysis using TensorFlow
. We can follow the following steps to build a classification model:
Import the TensorFlow
library using the following code:
import tensorflow as tf
Make a class that classifies the text passages. After that, the input text is Tokenizer
function from the Keras
library:
def __init__(self, vocab_size, max_length, num_lstm_units):self.vocab_size = vocab_sizeself.max_length = max_lengthself.num_lstm_units = num_lstm_unitsself.tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=self.vocab_size)
Line 5: The num_words
is the number of vocabulary words.
Now, convert the input text into a sequence using the following code:
def tokenize_text_corpus(self, texts):self.tokenizer.fit_on_texts(texts)sequences = self.tokenizer.texts_to_sequences(texts)return sequences
Line 2: The fit_on_text()
function updates the internal vocabulary based on the list of texts
.
Line 3: The input list texts
is converted into an integer sequence using the text_to_sequences()
function.
Now, make the training pairs for the classification model using the following code snippet:
def make_training_pairs(self, texts, labels):sequences = self.tokenize_text_corpus(texts)for i in range(len(sequences)):sequence = sequences[i]if len(sequence) > self.max_length:sequences[i] = sequence[:self.max_length]training_pairs = list(zip(sequences, labels))return training_pairs
There are many pre-trained sentiment analysis models available which can be integrated into the model using a few lines of code. For instance, the following code uses the pipeline class to make predictions:
pip install -q transformersfrom transformers import pipelinesentiment_pipeline = pipeline("sentiment-analysis")data = ["I love you", "I hate you"]sentiment_pipeline(data)
It outputs the following results:
[{'label': 'POSITIVE', 'score': 0.9998},{'label': 'NEGATIVE', 'score': 0.9991}]
We can also specify other models which are better suited to our use case and language.
Sentiment analysis is used in a wide range of applications, such as:
The comments and reviews on social media sites are analyzed and categorized as positive, negative, and neutral.
In the play store, all the reviews on the scale are done with the help of sentiment analysis to enhance customer service.
Sentiment analysis is used in product reviewing in the marketing sector.
Professional reviewers use it to give an overall review of a product based on other people's reviews.
Free Resources