[파이썬] textblob 감성 점수 시각화

TextBlob is a popular Python library that provides simple and easy-to-use methods for natural language processing (NLP) tasks, including sentiment analysis. Sentiment analysis involves determining the sentiment or emotion associated with a piece of text. In this blog post, we will explore how to visualize sentiment scores using TextBlob in Python.

Getting Started

Before we begin, make sure you have TextBlob and Matplotlib installed. If you don’t have them installed, you can install them using pip:

pip install textblob matplotlib

Importing Libraries

Let’s start by importing the necessary libraries:

import matplotlib.pyplot as plt
from textblob import TextBlob

Sentiment Analysis with TextBlob

To perform sentiment analysis with TextBlob, we can use the sentiment.polarity attribute of a TextBlob object. The polarity score ranges from -1 to 1, where -1 represents negative sentiment, 0 represents neutral sentiment, and 1 represents positive sentiment.

Let’s see an example:

# Define some sample text
text = "I love using TextBlob for sentiment analysis!"

# Create a TextBlob object
blob = TextBlob(text)

# Get the polarity score
polarity_score = blob.sentiment.polarity

# Print the polarity score
print("Polarity Score:", polarity_score)

The output of the above code should be:

Polarity Score: 0.5

Visualizing Sentiment Scores

Now that we know how to get the sentiment score using TextBlob, let’s explore how to visualize these scores. We can use Matplotlib, a popular plotting library in Python, to create a simple bar chart.

# Define a list of texts
texts = ["I love using TextBlob for sentiment analysis!",
         "This movie is amazing!",
         "I feel so sad today...",
         "The weather is perfect!",
         "I am not feeling well."]

# Create a list to store the sentiment scores
scores = []

# Get the sentiment score for each text
for text in texts:
    blob = TextBlob(text)
    scores.append(blob.sentiment.polarity)

# Create a bar chart
plt.bar(range(len(texts)), scores)
plt.ylim([-1, 1])
plt.xlabel("Texts")
plt.ylabel("Sentiment Score")

# Add text labels for each bar
for i, score in enumerate(scores):
    plt.text(i, score, round(score, 2), ha='center', va='bottom')

# Show the plot
plt.show()

The code above creates a bar chart that visualizes the sentiment scores for the given texts. The x-axis represents the texts, and the y-axis represents the sentiment scores. The height of each bar represents the sentiment score, and the value is displayed on top of each bar.

Conclusion

In this blog post, we learned how to perform sentiment analysis using TextBlob and visualize the sentiment scores using Matplotlib. TextBlob provides a convenient way to analyze the sentiment of textual data, making it useful for various NLP tasks. By visualizing the sentiment scores, we can gain better insights into the emotions conveyed by the text.

Feel free to explore more advanced techniques with TextBlob and Matplotlib to enhance your sentiment analysis workflows!