olumlu olumsuz notr2

 avatar
unknown
python
5 months ago
2.5 kB
3
Indexable
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imblearn.over_sampling import RandomOverSampler

import tkinter as tk
from tkinter import messagebox


# Categorize stars
def categorize_stars(stars):
    if stars < 3:
        return "Negative"
    elif stars == 3:
        return "Neutral"
    else:
        return "Positive"

df_all['sentiment'] = df_all['stars'].apply(categorize_stars)

# Features and labels
X = df_all["text"]
y = df_all["sentiment"]

# Convert text to numeric format using TF-IDF
vectorizer = TfidfVectorizer(max_features=5000, ngram_range=(1, 2), stop_words="english")
X_transformed = vectorizer.fit_transform(X)

# Over-sampling to balance the dataset
ros = RandomOverSampler(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X_transformed, y)

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2, random_state=43)

# Train Random Forest model
model = RandomForestClassifier(random_state=43, n_estimators=100) #class_weight='balanced'
model.fit(X_train, y_train)

# Evaluate model
y_pred = model.predict(X_test)
print("Classification Report:\n")
print(classification_report(y_test, y_pred))

# Create GUI for prediction
def predict_sentiment():
    user_input = input_box.get("1.0", "end-1c")  # Get user input from the text box
    if not user_input.strip():
        messagebox.showerror("Error", "Please enter a review!")
        return

    # Transform user input to numeric format
    user_input_transformed = vectorizer.transform([user_input])
    prediction = model.predict(user_input_transformed)[0]

    # Show result
    sentiment_mapping = {
        "Negative": "Olumsuz",
        "Neutral": "Nötr",
        "Positive": "Olumlu"
    }
    sentiment = sentiment_mapping.get(prediction, "Unknown")
    messagebox.showinfo("Prediction", f"The review is predicted as: {sentiment}")

# Set up the GUI
root = tk.Tk()
root.title("Yelp Review Sentiment Predictor")

# Input Text Box
tk.Label(root, text="Enter a review:").pack(pady=10)
input_box = tk.Text(root, height=10, width=50)
input_box.pack(pady=10)

# Predict Button
predict_button = tk.Button(root, text="Predict Sentiment", command=predict_sentiment)
predict_button.pack(pady=10)

root.mainloop()
Editor is loading...
Leave a Comment