Sentiment Analysis Using Python: A Beginner-Friendly Tutorial!
Tags: python, datascience, dataengineering, tutorial
Published: 2023-09-11 06:09:32

If you've ever wondered how companies understand customer opinions, or how social media platforms gauge public sentiment, you're in the right place. Sentiment Analysis is a fascinating field at the intersection of data science and natural language processing, and Python is one of the most popular languages to perform this analysis. Whether you're completely new to Python or just new to the world of Sentiment Analysis, this tutorial is designed with you in mind. 

## What is Sentiment Analysis?

![Sentiment Analysis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef1rsw5ab2r96bod5qqp.jpg)

Sentiment Analysis, also known as opinion mining, is the process of using natural language processing, text analysis, and computational linguistics to identify and categorize subjective opinions or feelings expressed in a piece of text. The primary objective is to determine the writer's attitude toward a particular topic, product, or service as positive, negative, or neutral. In some advanced forms, sentiment analysis may also involve identifying the intensity of the sentiment or even categorizing it into more specific emotional states like "happy," "angry," or "sad."

Let's understand sentiment analysis with a simple hands-on tutorial. We will use SingleStore's Notebooks feature in this tutorial. So **let's get started!**

## Prerequisites
- [Python installed](https://www.python.org/downloads/)
- [SingleStore Notebook installed](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=sentimentanalysis). Signup and select SingleStore Notebooks feature
- Install required Python package: textblob

You can install TextBlob using pip:
```
pip install textblob
```

## Steps to Create the SingleStore Notebook
We will use SingleStore's Notebooks feature (it is FREE to use) as our development environment for this tutorial.

The SingleStore Notebook extends the capabilities of Jupyter Notebook to enable data professionals to easily work and play around. 

#### What is SingleStore?
[SingleStore](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=sentimentanalysis) is a distributed, in-memory, SQL database management system designed for high-performance, high-velocity applications. It offers real-time analytics and mixes the capabilities of a traditional operational database with that of an analytical database to allow for transactions and analytics to be performed in a single system.

Signup for [SingleStore](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=sentimentanalysis) to use the Notebooks.

![SingleStore Notebooks feature](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ug1scnnwrz9ar1op5yji.png)

Once you sign up to SingleStore, you will also receive $600 worth free computing resources. So why not use this opportunity.

Click on 'Notebooks' and start with a blank Notebook.
![singlestore notebooks usage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99l9xf5hltflrjbl8qlx.png)

Name it something like 'Sentiment-Tutorial' or as per your wish.

![blank notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk3s4wcsgl5tw3alfxa9.png)

Let's start working with our Notebook that we just created.
Follow this step by step guide and keep adding the code shown in each step in your Notebook and execute it. Let's start!

#### Step 1: Import Libraries
```
from textblob import TextBlob
```
#### Step 2: Define Sample Text Data
We'll use a list of sample sentences for this example.

```
sample_texts = [
    "I love programming.",
    "I hate bugs.",
    "I feel indifferent about documentation.",
    "Debugging is fun!",
    "I'm frustrated with errors."
]

```

#### Step 3: Analyze Sentiment
We'll loop through the sample texts and analyze their sentiment using TextBlob.

```
for text in sample_texts:
    analysis = TextBlob(text)
    polarity = analysis.sentiment.polarity
    subjectivity = analysis.sentiment.subjectivity
    
    if polarity > 0:
        sentiment = "Positive"
    elif polarity < 0:
        sentiment = "Negative"
    else:
        sentiment = "Neutral"
    
    print(f"Text: {text}")
    print(f"Sentiment: {sentiment}")
    print(f"Polarity: {polarity}")
    print(f"Subjectivity: {subjectivity}")
    print("------")
```
This will output the sentiment, polarity, and subjectivity for each sample sentence.

#### Step 4: Interpret Results

- **Polarity**: Ranges from -1 to 1. Negative value indicates negative sentiment, and a positive value indicates positive sentiment.

- **Subjectivity**: Ranges from 0 to 1. Higher values indicate that the text contains personal opinion, emotion, or judgment.

You can put all these code snippets together in a SingleStore Notebook to create a complete workflow for basic sentiment analysis.

![ss notebooks usage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz0tbg61gwtdywdefsbt.png)

![sentiment analysis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvbsa50dxyuc3zk5v3z9.png)

The complete Notebook code is available here on my [GitHub repository](https://github.com/pavanbelagatti/sentiment-analysis). 

_Congratulations on completing 'Your First Sentiment Analysis Project in Python!_ By now, you should have some understanding of the basics of Sentiment Analysis and how to implement it using Python and Notebooks. You've not only learned the theory but also applied it in a simple hands-on project. 

As you continue your journey in data science, remember that Sentiment Analysis is just the tip of the iceberg. There are countless other exciting applications and techniques waiting for you to explore. So, what's next? Keep practicing, consider diving into more advanced topics like Large Language Models (LLMs), LangChain, Vector Databases, etc.

Take a look at my other articles that talk about the above important topics/concepts. 

{% embed https://dev.to/pavanbelagatti/python-cheat-sheet-for-data-engineers-and-data-scientists-3emj %}

{% embed https://dev.to/pavanbelagatti/a-beginners-guide-to-building-llm-powered-applications-with-langchain-2d6e %}

{% embed https://dev.to/pavanbelagatti/wtf-is-a-vector-database-a-beginners-guide-16p %}