Spaces:
Sleeping
Sleeping
Upload classifier.py
Browse files- classifier.py +24 -0
classifier.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textblob import TextBlob
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def classify_comment(comment):
|
| 5 |
+
text = comment.lower()
|
| 6 |
+
if '?' in text:
|
| 7 |
+
return 'question'
|
| 8 |
+
elif any(word in text for word in ['bad', 'worst', 'hate', 'not good', 'terrible', 'disagree']):
|
| 9 |
+
return 'criticism'
|
| 10 |
+
elif any(word in text for word in ['good', 'love', 'nice', 'amazing', 'agree', 'great', 'awesome']):
|
| 11 |
+
return 'affirmative'
|
| 12 |
+
else:
|
| 13 |
+
polarity = TextBlob(comment).sentiment.polarity
|
| 14 |
+
if polarity > 0.2:
|
| 15 |
+
return 'affirmative'
|
| 16 |
+
elif polarity < -0.2:
|
| 17 |
+
return 'criticism'
|
| 18 |
+
else:
|
| 19 |
+
return 'neutral'
|
| 20 |
+
|
| 21 |
+
def classify_comments(comments):
|
| 22 |
+
df = pd.DataFrame(comments)
|
| 23 |
+
df["category"] = df["text"].astype(str).apply(classify_comment)
|
| 24 |
+
return df
|