Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

Preface

Originally this was a task assigned to me for an employment opportunity. I've since decided to revamp this project into a modern approach, and focused primarily on trying to explain what this is and how it works - serving as a more educational resource for anybody interested in such topics (note that I am a beginner).

Hotel Review Sentiment Analysis

A modern, production-ready sentiment analysis system for hotel reviews that automatically classifies customer feedback as positive or negative sentiment. This project demonstrates advanced natural language processing (NLP) and machine learning preprocessing techniques.

🎯 What is Sentiment Analysis?

Sentiment Analysis is a branch of Natural Language Processing (NLP) that determines the emotional tone or attitude expressed in text. In the context of hotel reviews, it helps businesses:

  • Understand Customer Satisfaction: Automatically categorize thousands of reviews as positive or negative
  • Monitor Brand Reputation: Track sentiment trends over time
  • Improve Services: Identify common themes in negative feedback
  • Automate Review Processing: Replace manual review categorization with AI

Traditional Approach vs. This Approach

Manual Processing:

  • ❌ Time-consuming human review of each comment
  • ❌ Inconsistent categorization between reviewers
  • ❌ Cannot scale to thousands of reviews

This AI Approach:

  • ✅ Process 26,000+ reviews in seconds
  • ✅ Consistent, objective classification criteria
  • ✅ Scalable to any dataset size
  • ✅ Configurable sensitivity thresholds

📊 The Dataset: Hotel Booking Reviews

This dataset contains 26,386 real hotel reviews scraped from Booking.com, providing a rich source of authentic customer feedback.

Data Structure

📋 Dataset Overview:
├── 26,386 hotel reviews
├── 15 data columns
├── Reviews from multiple countries
├── Ratings from 1.0 to 10.0 scale
└── Raw text reviews + metadata

Key Data Columns

ColumnDescriptionExample
review_textCustomer's written review"The hotel was clean and staff friendly..."
ratingNumerical rating (1-10)8.5
hotel_nameName of the hotel"Villa Pura Vida"
nationalityReviewer's country"Belgium"
reviewed_atDate of review"11 July 2021"

Sample Review Data

Hotel: Villa Pura Vida
Rating: 8.5/10
Review: "Everything was perfect! Quiet, cozy place to relax. The breakfast was excellent and the staff was very helpful..."
Nationality: Poland
Date: July 2021

Rating Distribution Analysis

📈 Rating Statistics:
├── Range: 1.0 - 10.0
├── Average: 8.45/10
├── Negative (< 5): 462 reviews (1.8%)
├── Neutral (5-7): 6,725 reviews (25.5%)
└── Positive (7+): 19,199 reviews (72.7%)

🔍 What I Expected vs. What I Got

Initial Expectations

I expected to find a balanced distribution of positive, neutral, and negative reviews, similar to typical product review datasets (roughly 60% positive, 25% neutral, 15% negative).

Actual Results

My analysis revealed a highly positive-skewed dataset:

🎯 Expected Distribution:
├── Positive: ~60%
├── Neutral: ~25%
└── Negative: ~15%
📊 Actual Distribution:
├── Positive: 95.6% (25,198 reviews)
└── Negative: 4.4% (1,165 reviews)

Why This Happens

  1. Selection Bias: People are more likely to review when they have extreme experiences
  2. Platform Effect: Booking.com may pre-filter very negative reviews
  3. Hotel Quality: Dataset may focus on higher-rated establishments
  4. Review Incentives: Hotels may encourage satisfied customers to review

Machine Learning Implications

This imbalanced dataset presents classic ML challenges:

  • Class Imbalance: Need techniques like stratified sampling
  • Model Bias: Risk of always predicting "positive"
  • Evaluation Metrics: Accuracy alone is misleading (95.6% by always guessing positive)
  • Real-world Value: Better at detecting rare negative sentiment

🏗️ Project Architecture

SentimentAnalysis/
├── 📄 DataPreprocess.py # Main preprocessing pipeline
├── 🎯 example_usage.py # Usage demonstration
├── 📈 analyze_ratings.py # Data distribution analysis
├── �️ sentiment_gui.py # Interactive GUI application
├── 🚀 launch_gui.py # GUI launcher script
├── �📋 requirements.txt # Python dependencies
├── 📊 booking_reviews copy.csv # Hotel reviews dataset
└── 📖 README.md # This documentation

Core Components

1. DataPreprocess.py - The Heart of the System

Modern object-oriented preprocessing pipeline featuring:

  • Automatic column detection for any CSV structure
  • Advanced text cleaning (HTML, URLs, punctuation)
  • Smart sentiment classification with configurable thresholds
  • Robust error handling and validation
  • Professional logging and type hints

2. sentiment_gui.py - Interactive GUI Application 🆕

User-friendly graphical interface featuring:

  • Real-time sentiment analysis with confidence scores
  • Detailed explanations of why text was classified as positive/negative
  • Interactive model training with progress indicators
  • Example reviews to test the system
  • Model performance metrics and statistics
  • Custom data loading for your own CSV files

3. example_usage.py - Quick Start Demo

Interactive demonstration showing:

  • Complete preprocessing workflow
  • Sample output and statistics
  • Performance metrics
  • Ready-to-use ML data

4. analyze_ratings.py - Data Exploration

Comprehensive analysis tool for:

  • Rating distribution visualization
  • Column structure examination
  • Data quality assessment
  • Statistical summaries

5. demo_gui.py - GUI Features Preview

Demonstration script showing:

  • GUI capabilities overview
  • Feature explanations
  • Example analysis output
  • Perfect for headless environments

🚀 Quick Start

GUI Application (Recommended)

# Install dependencies
pip install -r requirements.txt
# Launch the interactive GUI
python launch_gui.py

The GUI provides:

  • 🎯 Interactive Analysis: Type any hotel review and get instant sentiment analysis
  • 🧠 AI Explanations: Detailed breakdown of why the AI made its decision
  • 📊 Model Training: Train the AI model on your data with progress tracking
  • 📈 Performance Metrics: See how well the model performs
  • 💡 Example Reviews: Try pre-loaded examples to see how it works

Command Line Usage

# Run the complete analysis
python example_usage.py
# Explore data distribution
python analyze_ratings.py

Programming Interface

fromDataPreprocessimportReviewDataPreprocessor# Initialize and processpreprocessor=ReviewDataPreprocessor('booking_reviews copy.csv')
X, y=preprocessor.prepare_data()
# Results: X = processed text, y = sentiment labelsprint(f"Dataset: {len(X)} reviews")
print(f"Positive sentiment: {y.mean():.1%}")

�️ Interactive GUI Features

The sentiment analysis GUI provides a comprehensive, user-friendly interface for analyzing hotel reviews with detailed explanations.

🎯 Key GUI Features

1. Intelligent Sentiment Analysis

  • Real-time Analysis: Type any hotel review and get instant sentiment classification
  • Confidence Scores: See how confident the AI is in its prediction (0-100%)
  • Visual Results: Clear positive/negative indicators with color coding

2. AI Explanation System

  • Word-level Analysis: See which specific words influenced the decision
  • Impact Scores: Understand how much each word contributed to the final sentiment
  • Model Transparency: Detailed breakdown of the AI's decision-making process

3. Interactive Model Training

  • One-click Training: Train the AI model on 26,000+ hotel reviews
  • Progress Tracking: Real-time progress bar during model training
  • Performance Metrics: See accuracy, precision, recall, and confusion matrix
  • Custom Data: Load your own CSV files for analysis

4. Example Reviews & Testing

  • Pre-loaded Examples: Try positive, negative, and neutral review samples
  • Custom Input: Analyze any hotel review text you want to test
  • Processed Text View: See how the AI cleans and processes your input

5. Educational Value

  • Step-by-step Explanations: Learn how sentiment analysis works
  • Feature Importance: Understand which words matter most
  • Model Architecture: See the technical details behind the predictions

📱 GUI Screenshots & Workflow

🖥️ Main Interface Layout:
├── 🎛️ Control Panel: Train model, load data, view status
├── ✍️ Input Section: Enter reviews, load examples
├── 📊 Analysis Tab: Sentiment results and confidence
├── 🧠 Explanation Tab: Why this sentiment? (Word analysis)
└── 📈 Model Info Tab: Performance metrics and details

🎓 How the Explanation System Works

When you analyze a review, the GUI shows:

  1. Overall Sentiment: Positive or Negative with confidence percentage
  2. Key Influencing Words:
    • ✅ Words that made it seem positive (e.g., "excellent", "friendly", "clean")
    • ❌ Words that made it seem negative (e.g., "terrible", "dirty", "rude")
  3. Impact Scores: Numerical values showing how much each word mattered
  4. Processing Steps: How the raw text was cleaned and prepared
  5. Model Details: Technical information about the AI algorithm

💡 Example Analysis

Input Review: "The hotel was absolutely fantastic! Great location and friendly staff."

AI Analysis:

  • 😊 Sentiment: POSITIVE (89.2% confidence)
  • Key Positive Words: "fantastic" (+0.245), "great" (+0.156), "friendly" (+0.134)
  • 🧠 Explanation: The model detected strong positive language with words like "fantastic" and "great" that are highly associated with positive hotel experiences in the training data.

�🔧 Technical Features

Modern Python Architecture

  • Object-Oriented Design: Clean, maintainable class structure
  • Type Hints: Full static type checking support
  • Error Handling: Graceful failure with meaningful messages
  • Logging: Structured debug information
  • Documentation: Comprehensive docstrings

Advanced Text Preprocessing

# What the preprocessing does:"<p>Great hotel! Visit https://example.com</p>""great hotel visit"# Removes: HTML tags, URLs, punctuation, stopwords, short words# Keeps: Meaningful content words for sentiment analysis

Smart Data Handling

  • Column Auto-Detection: Works with any CSV structure
  • Missing Data: Robust handling of null/invalid entries
  • Rating Flexibility: Configurable sentiment thresholds
  • Stratified Splitting: Maintains class balance in train/test

📈 Results & Performance

Preprocessing Output

✅ Successfully processed: 26,363 reviews
📊 Sentiment distribution: 95.6% positive, 4.4% negative 🔧 Train/test split: 21,090 / 5,273 samples
⚡ Processing time: ~30 seconds

Sample Processed Text

Original: "The hotel was absolutely fantastic! Great location near the beach. Staff were super helpful. Would definitely recommend! 😊"
Processed: "hotel absolutely fantastic great location near beach staff super helpful would definitely recommend"

🎯 Next Steps: Building ML Models

The preprocessed data is ready for machine learning:

1. Text Vectorization

fromsklearn.feature_extraction.textimportTfidfVectorizervectorizer=TfidfVectorizer(max_features=5000)
X_vectorized=vectorizer.fit_transform(X_train)

2. Model Training

  • Logistic Regression: Fast, interpretable baseline
  • Random Forest: Handles feature interactions
  • SVM: Good for text classification
  • Neural Networks: LSTM/BERT for advanced performance

3. Handling Class Imbalance

  • SMOTE: Synthetic minority oversampling
  • Class weights: Penalize majority class
  • Threshold tuning: Optimize decision boundary
  • Ensemble methods: Combine multiple approaches

� Troubleshooting

IssueSolution
NLTK download failsScript includes fallback stopword lists
Column not foundUse analyze_ratings.py to check structure
Memory issuesProcess data in chunks for large datasets
Encoding errorsEnsure CSV is UTF-8 encoded

GUI-Specific Issues

Problem: "GUI won't launch on remote server"
Solution: The GUI requires a graphical display. Use the command-line tools instead: python example_usage.py

Problem: "Model training takes too long"
Solution: Training on 26,000 reviews takes 30-60 seconds on modern hardware. The progress bar shows activity.

Problem: "Analysis seems inaccurate"
Solution: Remember the model is trained on hotel reviews specifically and may not work well for other domains. myself).

📝 Dependencies

pandas >= 2.0.0 # Data manipulation
nltk >= 3.8.0 # Natural language processing
scikit-learn >= 1.3.0 # Machine learning tools
numpy >= 1.24.0 # Numerical computing

About

Modern hotel review sentiment analysis with interactive GUI, AI explanations, and educational features. Python/ML/NLP project.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages