A small, friendly sentiment analysis demo in Python. This repository contains a compact end-to-end example that:
- Preprocesses text (cleaning, stopwords, stemming)
- Vectorizes text with TF-IDF
- Trains a simple classifier (Multinomial Naive Bayes / Logistic Regression)
- Evaluates and visualizes results (confusion matrix + accuracy chart)
This project is perfect as a quick demo, teaching aid, or starting point for experimentation and model improvements.
- Minimal, easy-to-read code in
sentiment_analysis.py. - Small synthetic dataset included for quick experimentation.
- Produces a visualization file:
sentiment_analysis_results.png. - Works inside a virtual environment (
env/) — examples in this README assume PowerShell on Windows.
Open PowerShell in the project folder and run the script using the bundled venv Python:
# Activate the venv (if not already active)
& .\env\Scripts\Activate.ps1
# Run the demo script
& .\env\Scripts\python.exe .\sentiment_analysis.pyExpected outcome: the script will preprocess the sample data, train a model, print evaluation metrics, save sentiment_analysis_results.png, and display example predictions.
If you prefer to recreate the environment or run with your own Python setup:
# Create a venv
python -m venv .venv
# Activate it
& .\.venv\Scripts\Activate.ps1
# Install dependencies
pip install -r requirements.txtNote: This repository already includes a working env/ virtual environment. If you use it, you do not need to reinstall the packages.
If you want a quick requirements.txt, you can generate one from the venv with:
# from within the venv
pip freeze > requirements.txt- Edit
sentiment_analysis.pyto change the sample dataset, tweak preprocessing, or swap model types (naive_bayesorlogistic_regression). - The script saves a plot to
sentiment_analysis_results.png. Use that image for visual inspection or documentation. - Example: Change the
test_textslist near the bottom of the file to try different inputs.
sentiment_analysis.py— main script (preprocessing, vectorization, training, evaluation, plotting)sentiment_analysis_results.png— output plot (generated by running the script)env/— included virtual environment (Windows)
- Vectorization:
TfidfVectorizer(max_features=5000) - Models included:
MultinomialNB(default) andLogisticRegression(option) - Preprocessing: lowercasing, URL/mention/hashtag removal, non-alpha stripping, stopword removal, Porter stemming
Caveats:
- The toy dataset is small and synthetic — results are for demonstration only.
- Evaluation metrics may show warnings for classes with extremely few or no predictions.
- Replace stemming with lemmatization (NLTK/WordNet or spaCy) for better canonical forms.
- Expand dataset and balance classes to improve evaluation reliability.
- Try word embeddings (Word2Vec, GloVe) or transformer embeddings (e.g., from Hugging Face) for better performance.
- Add unit tests for preprocessing and model training.
Contributions welcome! Open an issue or PR with a descriptive title and short summary of your change. Small, focused PRs are easiest to review.
If you add a new dependency, please update requirements.txt.
This project is provided as-is for demo and educational purposes. Add a license file (e.g., LICENSE) to formalize terms if you plan to publish or distribute.
Made with care � — happy experimenting!