What is Sentiment Analysis?
We will dive deep into the use of sentiment analysis and its code!
Photo by Austin Schmid on Unsplash
Hey Siri, how are my emotions today? How am I doing today?
These seem like ridiculous questions but with the advancements in Sentiment Analysis in Machine Learning, our machines are getting closer to answering these questions.
Let's get straight into examples!
If I would have asked you to rate a sentence out of 10, with 0 being negative and 10 being positive. How would you do that?
For example, have a look at these pictures -
Pretty positive right? Let's give it around 10!
Now have a look at this! Positive, but liking a movie is not as great as loving a movie! We know this right?
Now here's one more. This clearly states a very negative sentence, so that's totally 0.
Sentiment Analysis is simply using Machine Learning to teach Computers to do just this! Extract the sentiments out of our sentences or the emotions behind it!
The most pressing question is what's the use of it?
Well, there are many uses of it but one of the major things Sentiment Analysis will be used in is Controlling the Radicalization of the Internet. This is a very powerful tool that can be used by democracies around the world to make the internet a safer place! But also, at the same time this can be used to press the voices of some people that the Government doesn't like! So that's a Double Edged sword, you can use it the way you like it. Okay now let's get to Coding!
The Code!
We will be using Jupyter Notebooks to build our Machine Learning Model.
Installing and Importing Dependencies
Installing PyTorch- To know more about it refer to my next blog
!pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
Installing some other stuff!
!pip install transformers requests beautifulsoup4 pandas numpy
Transformers helps us to easily import, download and install our NLP model.
Requests are going to help us in making a request to the site we will be scraping for the testing data.
Beautiful Soup as you all know is used for web scraping to extract the data that we actually need - is taught in basics of python programming.
Pandas help us to structure the data in a format that makes it actually easy for us to work with.
NumPy helps us to work with arrays - we will understand its needs when we deep dive into it.
Importing the dependencies.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import requests
from bs4 import BeautifulSoup
import re
Importing the tokenizer from Transformers will help us to pass through a string and convert that into a sequence of numbers that we can pass through our NLP model.
Auto Model for Sequence Classification - This will give us the architecture from transformers to be able to load in our NLP model.
Next, we are importing PyTorch, requests, Beautiful Soup. Re is used for creating a regex function to be able to extract a specific comment we want.
Setting up the Model
tokenizer = AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
model = AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
In the first line we are loading in our tokenizer and then in the next line we are loading in our model.
So, the first line says about creating a tokenizer that is coming from a pre trained model. The words within quotes are the link of the model that is present in the Hugging Face website.
In the next line we are setting up our model = Auto model Sequence Classification which we imported earlier and we are using the from pretrained method to be able to load the pretrained model.
Encode and Calculate Sentiment
tokens = tokenizer. Encode ('I hated this, absolutely the worst', return_tensocould've)
So, the Tokenizer has just converted the tokens into numbers.
I will share more about this in the next blog.