File size: 1,969 Bytes
d8d3197
 
 
 
 
 
 
 
 
 
 
 
 
 
b6ed3ee
2ef1a11
 
 
1694d0a
 
 
2ef1a11
 
1694d0a
2ef1a11
d8d3197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import re
import string
import nltk
import gradio as gr

from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression

# Download resources
nltk.download('stopwords')

# Load data
df = pd.read_csv("NFL_reddit_data_2021.csv")

from textblob import TextBlob

def get_sentiment(text): 
    if not isinstance(text, str): 
        text = str(text) 
    return "positive" if TextBlob(text).sentiment.polarity > 0 else "negative"

df["text"] = df["text"].astype("object").astype(str)
df["sentiment"] = df["text"].apply(get_sentiment)
df = df.dropna(subset=["text", "sentiment"])

# Text preprocessing
stop_words = set(stopwords.words("english"))

def clean_text(text):
    text = text.lower()
    text = re.sub(r"http\S+", "", text)
    text = re.sub(r"\d+", "", text)
    text = text.translate(str.maketrans("", "", string.punctuation))
    words = text.split()
    words = [w for w in words if w not in stop_words]
    return " ".join(words)

df["clean_text"] = df["text"].apply(clean_text)

# Feature extraction
vectorizer = TfidfVectorizer(max_features=5000)
X = vectorizer.fit_transform(df["clean_text"])
y = df["sentiment"]

# Train model
model = LogisticRegression(max_iter=1000)
model.fit(X, y)

# Prediction function for Gradio
def predict_sentiment(user_input):
    cleaned = clean_text(user_input)
    vectorized = vectorizer.transform([cleaned])
    prediction = model.predict(vectorized)[0]
    return f"Predicted Sentiment: {prediction}"

# Gradio Interface
interface = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.Textbox(lines=4, placeholder="Enter an NFL Reddit comment..."),
    outputs="text",
    title="NFL Reddit Sentiment Analyzer",
    description=(
        "Analyze sentiment of NFL-related Reddit comments using NLP. "
        "This tool demonstrates how sentiment analysis can support NFL Draft decisions."
    )
)

interface.launch()