-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_analysis.py
79 lines (64 loc) · 2.27 KB
/
sentiment_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pandas as pd
#from faker import Faker
#from textblob import TextBlob
#from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import spacy
from transformers import pipeline
# Initializing the libraries
nlp = spacy.load('en_core_web_sm')
analyzer = pipeline("sentiment-analysis", model = "nlptown/bert-base-multilingual-uncased-sentiment")
# Preprocessing function
def preprocess_text_spacy(text):
doc = nlp(text.lower())
return [token.text for token in doc if not token.is_stop and not token.is_punct]
# TextBlob sentiment analysis
'''
def get_sentiment_textblob(tokens):
text = " ".join(tokens)
blob = TextBlob(text)
return blob.sentiment.polarity
'''
# VADER sentiment analysis
'''
def get_sentiment_vader(text):
sentiment = analyzer.polarity_scores(text)
return sentiment['compound']
'''
#BERT sentiment analysis
def get_sentiment_bert(tokens):
text = " ".join(tokens)
sentiment = analyzer(text)
#extracting the sentiment labels and score
sentiment_label = sentiment[0]['label']
sentiment_score = sentiment[0]['score']
#mapping the sentiment labels
label_mapping = {
"1 star" : "Very Negative",
"2 stars" : "Negative",
"3 stars" : "Neutral",
"4 stars" : "Positive",
"5 stars" : "Very Positive"
}
sentiment_label_human = label_mapping.get(sentiment_label, "Unknown")
return sentiment_score, sentiment_label_human
# Categorize sentiments
def categorize_sentiment_custom(score):
if score > 0.1:
return 'Satisfactory'
elif score < -0.1:
return 'Unsatisfactory'
else:
return 'Neutral'
# Main function to process the CSV
def analyze_sentiments(file_path):
# Read the CSV file
df = pd.read_csv(file_path)
# Preprocess the feedback text
df['ProcessedFeedback'] = df['FeedbackText'].apply(preprocess_text_spacy)
# Apply BERT sentiment analysis
df['SentimentBertScore'], df['SentimentBertLabel'] = zip(*df['ProcessedFeedback'].apply(get_sentiment_bert))
return df
'''# Categorize sentiments based on the analysis
df['SentimentCategoryTextblob'] = df['SentimentTextblob'].apply(categorize_sentiment_custom)
df['SentimentCategoryVader'] = df['SentimentVader'].apply(categorize_sentiment_custom)
'''