Spaces:
Runtime error
Runtime error
Upload sentiment_labeling.py
Browse files- sentiment_labeling.py +20 -0
sentiment_labeling.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
|
3 |
+
def label_sentiment(rating):
|
4 |
+
"""Label sentiment based on the rating."""
|
5 |
+
if rating in [1, 2]:
|
6 |
+
return 'negative'
|
7 |
+
elif rating == 3:
|
8 |
+
return 'neutral'
|
9 |
+
elif rating in [4, 5]:
|
10 |
+
return 'positive'
|
11 |
+
else:
|
12 |
+
return 'unknown'
|
13 |
+
|
14 |
+
def add_sentiment_column(data: pd.DataFrame, rating_col_name='rating'):
|
15 |
+
"""Add a sentiment column to the dataframe based on the ratings."""
|
16 |
+
if rating_col_name not in data.columns:
|
17 |
+
raise ValueError(f"Column '{rating_col_name}' not found in the dataframe.")
|
18 |
+
|
19 |
+
data['sentiment'] = data[rating_col_name].apply(label_sentiment)
|
20 |
+
return data
|