File size: 819 Bytes
35dd811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from textblob import TextBlob
import pandas as pd

def classify_comment(comment):
    text = comment.lower()
    if '?' in text:
        return 'question'
    elif any(word in text for word in ['bad', 'worst', 'hate', 'not good', 'terrible', 'disagree']):
        return 'criticism'
    elif any(word in text for word in ['good', 'love', 'nice', 'amazing', 'agree', 'great', 'awesome']):
        return 'affirmative'
    else:
        polarity = TextBlob(comment).sentiment.polarity
        if polarity > 0.2:
            return 'affirmative'
        elif polarity < -0.2:
            return 'criticism'
        else:
            return 'neutral'

def classify_comments(comments):
    df = pd.DataFrame(comments)
    df["category"] = df["text"].astype(str).apply(classify_comment)
    return df