Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created August 27, 2024 14:05
Show Gist options
  • Save thinkphp/28b50847a24f3bd092e0d8a458c91646 to your computer and use it in GitHub Desktop.
Save thinkphp/28b50847a24f3bd092e0d8a458c91646 to your computer and use it in GitHub Desktop.
textblob sentiment
import tkinter as tk
from tkinter import ttk
from textblob import TextBlob
def analyze_sentiment():
"""Analyzes the sentiment of the input text and updates the UI."""
text = text_entry.get("1.0", "end-1c")
if not text.strip():
sentiment_label.config(text="Please enter some text!", foreground="red")
return
blob = TextBlob(text)
sentiment_polarity = blob.sentiment.polarity
if sentiment_polarity > 0:
sentiment = "Positive 😊"
sentiment_color = "green"
elif sentiment_polarity < 0:
sentiment = "Negative 😞"
sentiment_color = "red"
else:
sentiment = "Neutral 😐"
sentiment_color = "blue"
sentiment_label.config(text=sentiment, foreground=sentiment_color)
# Create the main window
window = tk.Tk()
window.title("Sentiment Analysis")
# Create a label for the title
title_label = tk.Label(window, text="Sentiment Analysis Tool", font=("Helvetica", 16))
title_label.pack(pady=10)
# Create text entry box
text_entry = tk.Text(window, height=10, width=50, font=("Helvetica", 12))
text_entry.pack(pady=10)
# Create analyze button
analyze_button = tk.Button(window, text="Analyze Sentiment", command=analyze_sentiment, font=("Helvetica", 12))
analyze_button.pack(pady=10)
# Create a label to display the sentiment result
sentiment_label = tk.Label(window, text="", font=("Helvetica", 14))
sentiment_label.pack(pady=10)
# Run the application
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment