INDEX
INTRODUCTION
PROGRAM
EXPLANATION
OUTPUT
LIBRARIES USED
ACKNOWLEDGEMENT
iNTRODUCTION
Sentiment Analysis Tool
Sentiment analysis, also known as opinion mining, is a technique in
Natural Language Processing (NLP) that determines whether a given
piece of text expresses a positive, negative, or neutral sentiment.
This tool is built using Python's Tkinter for the graphical user interface
and VADER (Valence Aware Dictionary and sEntiment Reasoner)
from the NLTK library to analyze text sentiment.
Users can enter a sentence or paragraph, and the system will determine
the sentiment score and classify it as Positive , Negative , or Neutral
.
Program
import tkinter as tk
from tkinter import scrolledtext, messagebox
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
nltk.download("vader_lexicon")
sia = SentimentIntensityAnalyzer()
def analyze_sentiment():
text = text_input.get("1.0", tk.END).strip()
if not text:
messagebox.showwarning("Warning", "Please enter some text!")
return
score = sia.polarity_scores(text)["compound"]
if score > 0.05:
sentiment = "😊 Positive"
color = "#28a745"
elif score < -0.05:
sentiment = "😠 Negative"
color = "#dc3545"
else:
sentiment = "😐 Neutral"
color = "#ffc107"
result_label.config(text=f"Sentiment: {sentiment}\nScore: {score:.3f}",
fg=color)
root = tk.Tk()
root.title("Sentiment Analysis Tool")
root.geometry("500x400")
root.configure(bg="#2C2F33")
tk.Label(root, text="Sentiment Analysis Tool", font=("Arial", 18, "bold"),
bg="#7289DA", fg="white", pady=10).pack(fill=tk.X)
tk.Label(root, text="Enter text below:", font=("Arial", 12), bg="#2C2F33",
fg="white").pack(pady=5)
text_input = scrolledtext.ScrolledText(root, height=5, width=50,
font=("Arial", 12), bg="#F0F0F0")
text_input.pack(pady=5, padx=10)
analyze_button = tk.Button(root, text="Analyze Sentiment", font=("Arial",
12, "bold"), bg="#5865F2", fg="white", padx=20, pady=5,
command=analyze_sentiment)
analyze_button.pack(pady=10)
result_label = tk.Label(root, text="", font=("Arial", 14, "bold"),
bg="#2C2F33", fg="white")
result_label.pack(pady=10)
root.mainloop()
Libraries used
📚 Libraries Used
The following Python libraries are used in this project:
1️⃣ Tkinter
Tkinter is Python’s built-in library for creating GUI applications.
It allows easy development of interactive and user-friendly
desktop applications.
2️⃣ ScrolledText (tkinter.scrolledtext)
Provides a scrollable text box, enabling users to input multiple
lines of text.
3️⃣ Messagebox (tkinter.messagebox)
Displays alerts and warnings (e.g., if the user enters no text).
4️⃣ NLTK (Natural Language Toolkit)
A powerful NLP library used for text processing.
In this project, VADER (SentimentIntensityAnalyzer) is used to
classify sentiment.
5️⃣ VADER (Valence Aware Dictionary and Sentiment Reasoner)
VADER is a lexicon-based sentiment analysis model designed
for short texts, like social media posts, reviews, and messages.
It assigns a compound score to text:
o Positive (> 0.05) → 😊
o Negative (< -0.05) → 😠
o Neutral (-0.05 to 0.05) → 😐
🙌 Acknowledgment
This project is inspired by the need to analyze text sentiment in real
time using a simple and user-friendly interface.
NLTK Developers: For creating and maintaining the NLTK library,
which makes NLP tasks easier.
Python & Tkinter Community: For providing extensive
documentation and support for GUI development.
VADER Authors: For developing a powerful sentiment analysis
model, widely used in industry and academia.
This project is a small step towards understanding sentiment
analysis, and future improvements can include multilingual support,
advanced NLP techniques, and real-time analysis of social media
data.
Code Explanation
Code Explanation - Sentiment Analysis Tool (Tkinter & VADER)
This project is a GUI-based sentiment analysis tool built using
Python, Tkinter, and NLTK's VADER. Below is a step-by-step
breakdown of the code.
import tkinter as tk
from tkinter import scrolledtext, messagebox
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
tkinter: Used to create the Graphical User Interface (GUI).
scrolledtext: Provides a scrollable text box for user input.
messagebox: Displays alert dialogs (e.g., if the user doesn’t enter
any text).
nltk: The Natural Language Toolkit, which includes tools for text
processing.
SentimentIntensityAnalyzer: VADER-based sentiment analyzer
for classifying text sentiment.
2️⃣ Downloading the VADER Lexicon
nltk.download("vader_lexicon")
Downloads the VADER lexicon, which is needed to analyze text sentiment.
This step is required only once per system.
3️⃣ Initializing the Sentiment Analyzer
python
CopyEdit
sia = SentimentIntensityAnalyzer()
Creates an instance of SentimentIntensityAnalyzer, allowing us to
analyze sentiment.
4️⃣ Defining the Sentiment Analysis Function
python
CopyEdit
def analyze_sentiment():
text = text_input.get("1.0", tk.END).strip()
if not text:
messagebox.showwarning("Warning", "Please enter some text!")
return
Retrieves the user input from the text box.
If no text is entered, a warning message is displayed.
python
CopyEdit
score = sia.polarity_scores(text)["compound"]
Analyzes sentiment using VADER and retrieves the compound
score (ranging from -1 to +1).
The compound score represents overall sentiment polarity.
python
CopyEdit
if score > 0.05:
sentiment = "😊 Positive"
color = "#28a745"
elif score < -0.05:
sentiment = "😠 Negative"
color = "#dc3545"
else:
sentiment = "😐 Neutral"
color = "#ffc107"
Based on the compound score, the sentiment is classified as:
o Positive (Green) 😊 → score > 0.05
o Negative (Red) 😠 → score < -0.05
o Neutral (Yellow) 😐 → score between -0.05 and 0.05
python
CopyEdit
result_label.config(text=f"Sentiment: {sentiment}\nScore: {score:.3f}",
fg=color)
Updates the result label with the sentiment category and
score, and changes the text color accordingly.
5️⃣ Creating the GUI Window
python
CopyEdit
root = tk.Tk()
root.title("Sentiment Analysis Tool")
root.geometry("500x400")
root.configure(bg="#2C2F33")
Creates the Tkinter root window.
Sets the title, size, and background color.
6️⃣ Adding the Header
python
CopyEdit
tk.Label(root, text="Sentiment Analysis Tool", font=("Arial", 18, "bold"),
bg="#7289DA", fg="white", pady=10).pack(fill=tk.X)
Creates a header label with a blue background and white text.
Uses bold font to make the header more visually appealing.
7️⃣ Adding the Text Input Box
python
CopyEdit
tk.Label(root, text="Enter text below:", font=("Arial", 12), bg="#2C2F33",
fg="white").pack(pady=5)
text_input = scrolledtext.ScrolledText(root, height=5, width=50,
font=("Arial", 12), bg="#F0F0F0")
text_input.pack(pady=5, padx=10)
Adds a label prompting the user to enter text.
Creates a scrollable text box for user input, making it easy to
enter long text.
8️⃣ Adding the "Analyze Sentiment" Button
python
CopyEdit
analyze_button = tk.Button(root, text="Analyze Sentiment", font=("Arial",
12, "bold"), bg="#5865F2", fg="white", padx=20, pady=5,
command=analyze_sentiment)
analyze_button.pack(pady=10)
Creates a button that, when clicked, calls the analyze_sentiment()
function.
Uses aesthetic styling to make the button visually appealing.
9️⃣ Displaying the Sentiment Result
python
CopyEdit
result_label = tk.Label(root, text="", font=("Arial", 14, "bold"),
bg="#2C2F33", fg="white")
result_label.pack(pady=10)
Creates an empty label that will later display the sentiment
result.
🔟 Running the Tkinter Application
python
CopyEdit
root.mainloop()
Starts the Tkinter event loop, keeping the window open and
responsive.
📌 Summary
1. Imports necessary libraries (Tkinter, NLTK, VADER).
2. Initializes the GUI and downloads the VADER lexicon.
3. Retrieves user input, analyzes sentiment, and displays results.
4. Uses colors and emojis to make the interface more engaging.
5. Runs the Tkinter application to display the GUI.