Sentiment Analysis using text blob

Godwin Murithi
2 min readJun 10, 2023

--

Obtaining Sentiment Score for a Sentence Using TextBlob

Sentiment analysis is a natural language processing technique that aims to determine the sentiment or emotional tone expressed in a piece of text, such as a sentence, paragraph, or document. It is commonly used to understand the overall sentiment of a large volume of text data, such as customer reviews, social media posts, or feedback.

TextBlob is a Python library that provides a simple and intuitive API for performing various natural language processing tasks, including sentiment analysis. It is built on top of the NLTK (Natural Language Toolkit) library and provides an easy-to-use interface for text processing and analysis.

To perform sentiment analysis using TextBlob, you typically follow these steps:

  1. TextBlob installation: First, you need to install the TextBlob library in your Python environment. You can do this by running the following command in your terminal or command prompt:
    ```
    pip install textblob
    ```

2. Importing TextBlob: After installing TextBlob, you need to import it in your Python script or notebook:

```python
from textblob import TextBlob
```

3. Creating a TextBlob object: You create a TextBlob object by passing the text you want to analyze as a parameter to the TextBlob constructor:
```python
text = “I love this product! It’s amazing.”
blob = TextBlob(text)
```

4. Sentiment analysis: Once you have the TextBlob object, you can use its `sentiment` property to obtain sentiment-related information. The `sentiment` property returns a named tuple with two values: polarity and subjectivity. Polarity represents the sentiment score ranging from -1.0 (negative) to 1.0 (positive), while subjectivity represents the subjectivity score ranging from 0.0 (objective) to 1.0 (subjective):

```python
sentiment = blob.sentiment
polarity = sentiment.polarity
subjectivity = sentiment.subjectivity
```

The `polarity` value can be used to determine whether the sentiment is positive, negative, or neutral. A positive polarity indicates positive sentiment, a negative polarity indicates negative sentiment and a polarity close to zero indicates neutral sentiment.

5. TextBlob’s sentiment analysis also provides a helper method called `sentiment_assessments()`, which returns a dictionary with more detailed sentiment analysis results. It provides the polarity, subjectivity, and assessments of the text’s sentiment:

```python
assessments = blob.sentiment_assessments
```

The `assessments` dictionary includes the following keys: polarity, subjectivity, assessments, and raw assessments. The `assessments` key holds a list of tuples, where each tuple contains a word and its associated polarity and subjectivity.

TextBlob’s sentiment analysis can be a useful tool for quickly extracting sentiment information from text data. However, it’s worth noting that sentiment analysis is a challenging task, and the accuracy of the results can vary depending on the complexity and context of the text being analyzed.

--

--

Godwin Murithi

GIS Developer. I share knowledge on how to use GIS & Spatial Data to solve real-world problems.