Python | TextBlob.sentiment() method
Sentiment analysis helps us find the emotional tone of text whether itâs positive, negative or neutral. The TextBlob.sentiment() method simplifies this task by providing two key components:
- polarity: which shows if the text is positive, negative or neutral
- subjectivity: which shows whether the text is more opinion-based or factual
Let's see a basic example:
from textblob import TextBlob
text = "GFG is a good company and always value their employees."
blob = TextBlob(text)
sentiment = blob.sentiment
print(sentiment)
Output:
Sentiment(polarity=0.7, subjectivity=0.6000000000000001)
- Polarity: 0.7 means positive sentiment.
- Subjectivity: 0.6 means itâs more opinion-based.
Syntax:
TextBlob.sentiment
Return:
- Polarity: A score between -1 (negative) and 1 (positive) showing how positive or negative the text is.
- Subjectivity: A score between 0 (factual) and 1 (opinion-based) showing how subjective or objective the text is.
Lets see some more examples:
Example 1: Negative Sentiment
Here, we analyze a sentence that expresses a strong negative sentiment. The polarity score reflects the negative tone while the subjectivity shows opinion-based statement.
text = "I hate bugs in my code."
blob = TextBlob(text)
sentiment = blob.sentiment
print(sentiment)
Output:
Sentiment(polarity=-0.8, subjectivity=0.9)
- Polarity: -0.8 shows a negative sentiment.
- Subjectivity: 0.9 shows the text is highly opinion-based.
Example 2: Neutral Sentiment
In this example, weâll analyze a neutral sentence that conveys factual information without expressing any strong opinion or emotion. It will show how TextBlob classifies a sentence with no sentiment bias.
text = "The sun rises in the east."
blob = TextBlob(text)
sentiment = blob.sentiment
print(sentiment)
Output:
Sentiment(polarity=0.0, subjectivity=0.0)
- Polarity: 0.0 means neutral.
- Subjectivity: 0.0 shows it's factual.
Example 3: Mixed Sentiment
Here the sentence presents a neutral sentiment but is more opinion-based than factual. The polarity score remains neutral while the subjectivity score reflects an opinion or preference.
text = "I enjoy coding, but debugging can be frustrating."
blob = TextBlob(text)
sentiment = blob.sentiment
print(sentiment)
Output:
Sentiment(polarity=0.0, subjectivity=0.7)
- Polarity = 0.0: This shows a neutral sentiment.
- Subjectivity = 0.7: The text is more opinion-based than factual as it's expressing a preference.
Practical Use Cases
This can be useful in various applications including:
- Social Media Monitoring: Analyze sentiment in social media posts to identify public opinion, tracking how positive or negative people feel about topics. This helps in understanding overall sentiment toward trends or events.
- Customer Feedback: Quickly assess customer reviews to identify satisfaction levels, detecting trends in product or service reception. It allows businesses to find customer satisfaction in real-time.
- Content Moderation: Identify and flag negative or inappropriate comments, helping maintain a positive environment in online communities. This improves user experience and promotes healthy discussions.
Limitations
- Context Issues: TextBlob struggles with sarcasm or irony, leading to inaccurate sentiment scores and it may miss the true sentiment of a message. This affects its reliability in certain contexts.
- Simplicity: It relies on a basic lexicon which may miss deeper nuances, making it less accurate for complex or nuanced text. This can limit its application for sophisticated analysis.
- Ambiguity: Mixed sentiments in one sentence can be difficult for it to handle which may result in incorrect classification of sentiment. This reduces its effectiveness in certain cases.