Create sentiment_analysis.py
Browse files- sentiment_analysis.py +47 -0
sentiment_analysis.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import nltk
|
| 3 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 4 |
+
from textblob import TextBlob
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
class SentimentAnalyzer:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
nltk.download('vader_lexicon')
|
| 10 |
+
self.sia = SentimentIntensityAnalyzer()
|
| 11 |
+
self.transformer_model = pipeline("sentiment-analysis")
|
| 12 |
+
|
| 13 |
+
def analyze(self, data, text_column):
|
| 14 |
+
# VADER Sentiment Analysis
|
| 15 |
+
data['vader_sentiment'] = data[text_column].apply(lambda x: self.sia.polarity_scores(x)['compound'])
|
| 16 |
+
|
| 17 |
+
# TextBlob Sentiment Analysis
|
| 18 |
+
data['textblob_sentiment'] = data[text_column].apply(lambda x: TextBlob(x).sentiment.polarity)
|
| 19 |
+
|
| 20 |
+
# Transformer-based Sentiment Analysis
|
| 21 |
+
transformer_results = self.transformer_model(data[text_column].tolist())
|
| 22 |
+
data['transformer_sentiment'] = [result['score'] if result['label'] == 'POSITIVE' else -result['score'] for result in transformer_results]
|
| 23 |
+
|
| 24 |
+
# Aggregate sentiment
|
| 25 |
+
data['aggregate_sentiment'] = (data['vader_sentiment'] + data['textblob_sentiment'] + data['transformer_sentiment']) / 3
|
| 26 |
+
|
| 27 |
+
return data
|
| 28 |
+
|
| 29 |
+
def get_sentiment_summary(self, data):
|
| 30 |
+
summary = {
|
| 31 |
+
'positive': (data['aggregate_sentiment'] > 0.05).sum(),
|
| 32 |
+
'neutral': ((data['aggregate_sentiment'] >= -0.05) & (data['aggregate_sentiment'] <= 0.05)).sum(),
|
| 33 |
+
'negative': (data['aggregate_sentiment'] < -0.05).sum()
|
| 34 |
+
}
|
| 35 |
+
return summary
|
| 36 |
+
|
| 37 |
+
def plot_sentiment_distribution(self, data):
|
| 38 |
+
import matplotlib.pyplot as plt
|
| 39 |
+
import seaborn as sns
|
| 40 |
+
|
| 41 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 42 |
+
sns.histplot(data['aggregate_sentiment'], kde=True, ax=ax)
|
| 43 |
+
ax.set_title('Distribution of Sentiment Scores')
|
| 44 |
+
ax.set_xlabel('Sentiment Score')
|
| 45 |
+
ax.set_ylabel('Frequency')
|
| 46 |
+
|
| 47 |
+
return fig
|