|
1 | 1 | # Stock Sentiment Analyzer |
2 | 2 |
|
3 | | -A simple Python tool that uses FinBERT (Hugging Face Transformers) to classify financial news headlines as **positive**, **negative**, or **neutral**. |
| 3 | +[](https://www.python.org/downloads/) |
| 4 | +[](LICENSE) |
| 5 | +[](https://github.com/psf/black) |
| 6 | +[](https://flake8.pycqa.org/) |
| 7 | +[](https://github.com/MeridianAlgo/Basic-Sentiment-Analysis/actions) |
4 | 8 |
|
5 | | -## Setup |
| 9 | +A production-ready Python tool that uses FinBERT to classify financial news headlines as positive, negative, or neutral sentiment. Built with comprehensive testing, linting, and CI/CD workflows. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Real-time sentiment analysis of financial headlines using FinBERT |
| 14 | +- FinBERT model fine-tuned specifically for financial text |
| 15 | +- Confidence scores for each prediction |
| 16 | +- Summary statistics across multiple headlines |
| 17 | +- Comprehensive test suite with 88% code coverage |
| 18 | +- Type hints and full linting support |
| 19 | +- Organized project structure with src/ and tests/ directories |
| 20 | +- CI/CD workflow with automated testing and linting |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +### Prerequisites |
| 25 | + |
| 26 | +- Python 3.8 or higher |
| 27 | +- pip package manager |
| 28 | + |
| 29 | +### Installation |
6 | 30 |
|
7 | 31 | ```bash |
| 32 | +# Clone the repository |
| 33 | +git clone https://github.com/MeridianAlgo/Basic-Sentiment-Analysis.git |
| 34 | +cd Basic-Sentiment-Analysis |
| 35 | + |
| 36 | +# Create virtual environment |
8 | 37 | python3 -m venv .venv |
| 38 | + |
| 39 | +# Activate virtual environment |
| 40 | +# On macOS/Linux: |
9 | 41 | source .venv/bin/activate |
| 42 | +# On Windows: |
| 43 | +.venv\Scripts\activate |
| 44 | + |
| 45 | +# Install dependencies |
10 | 46 | pip install -r requirements.txt |
11 | 47 | ``` |
12 | 48 |
|
13 | | -### Run |
| 49 | +### Usage |
14 | 50 |
|
15 | 51 | ```bash |
16 | 52 | python main.py |
17 | 53 | ``` |
18 | 54 |
|
19 | | -### Example |
| 55 | +Enter financial headlines one per line. Type `done` when finished: |
| 56 | + |
| 57 | +``` |
| 58 | +Stock News Sentiment Analyzer |
| 59 | +Enter headlines (one per line). Type 'done' when finished. |
20 | 60 |
|
21 | | -```text |
22 | 61 | Headline: NVIDIA beats earnings expectations, stock surges 8% |
23 | | -Sentiment: positive (Confidence: 90.8%) |
| 62 | +Headline: Tesla stock plunges 12% after controversial tweet |
| 63 | +Headline: done |
| 64 | +
|
| 65 | +Analyzing stock news sentiment: |
| 66 | +
|
| 67 | +-------------------------------------------------------------------------------- |
| 68 | +1. Headline: NVIDIA beats earnings expectations, stock surges 8% |
| 69 | + Sentiment: POSITIVE | Confidence: 90.8% |
| 70 | +
|
| 71 | +2. Headline: Tesla stock plunges 12% after controversial tweet |
| 72 | + Sentiment: NEGATIVE | Confidence: 87.3% |
| 73 | +
|
| 74 | +-------------------------------------------------------------------------------- |
| 75 | +
|
| 76 | +Summary (2 headlines analyzed): |
| 77 | + Positive: 1 (50.0%) |
| 78 | + Negative: 1 (50.0%) |
| 79 | + Neutral: 0 (0.0%) |
| 80 | +``` |
| 81 | + |
| 82 | +## Development |
| 83 | + |
| 84 | +### Project Structure |
| 85 | + |
| 86 | +``` |
| 87 | +stock-sentiment-analyzer/ |
| 88 | +├── src/ |
| 89 | +│ ├── __init__.py |
| 90 | +│ └── analyzer.py # Core sentiment analysis logic |
| 91 | +├── tests/ |
| 92 | +│ ├── __init__.py |
| 93 | +│ └── test_analyzer.py # Unit tests |
| 94 | +├── config/ # Configuration files |
| 95 | +├── main.py # Entry point |
| 96 | +├── requirements.txt # Python dependencies |
| 97 | +├── pytest.ini # Pytest configuration |
| 98 | +├── pyproject.toml # Black and tool configuration |
| 99 | +├── .flake8 # Flake8 configuration |
| 100 | +├── README.md # This file |
| 101 | +├── LICENSE # MIT License |
| 102 | +├── CONTRIBUTING.md # Contribution guidelines |
| 103 | +└── CODE_OF_CONDUCT.md # Code of conduct |
| 104 | +``` |
| 105 | + |
| 106 | +### Running Tests |
| 107 | + |
| 108 | +```bash |
| 109 | +# Run all tests |
| 110 | +pytest |
| 111 | + |
| 112 | +# Run with coverage report |
| 113 | +pytest --cov=src --cov-report=html |
| 114 | + |
| 115 | +# Run specific test file |
| 116 | +pytest tests/test_analyzer.py -v |
| 117 | +``` |
| 118 | + |
| 119 | +### Code Quality |
| 120 | + |
| 121 | +```bash |
| 122 | +# Format code with black |
| 123 | +black src/ tests/ main.py |
| 124 | + |
| 125 | +# Sort imports with isort |
| 126 | +isort src/ tests/ main.py |
| 127 | + |
| 128 | +# Lint with flake8 |
| 129 | +flake8 src/ tests/ main.py |
| 130 | + |
| 131 | +# Check with ruff |
| 132 | +ruff check src/ tests/ main.py |
| 133 | + |
| 134 | +# Type checking with mypy |
| 135 | +mypy src/ --ignore-missing-imports |
| 136 | +``` |
| 137 | + |
| 138 | +### Run All Quality Checks |
| 139 | + |
| 140 | +```bash |
| 141 | +# Format |
| 142 | +black src/ tests/ main.py && isort src/ tests/ main.py |
| 143 | + |
| 144 | +# Lint |
| 145 | +flake8 src/ tests/ main.py && ruff check src/ tests/ main.py |
| 146 | + |
| 147 | +# Test |
| 148 | +pytest --cov=src --cov-report=term-missing |
| 149 | +``` |
| 150 | + |
| 151 | +## How It Works |
| 152 | + |
| 153 | +1. **Model Loading**: Uses FinBERT, a BERT model fine-tuned on financial text |
| 154 | +2. **Headline Input**: Accepts user-provided financial news headlines |
| 155 | +3. **Sentiment Analysis**: Classifies each headline as positive, negative, or neutral |
| 156 | +4. **Confidence Scoring**: Returns confidence percentage for each prediction |
| 157 | +5. **Summary Statistics**: Aggregates and displays results across all headlines |
| 158 | + |
| 159 | +## Model Details |
| 160 | + |
| 161 | +- **Model**: ProsusAI/finbert |
| 162 | +- **Task**: Sentiment classification |
| 163 | +- **Labels**: Positive, Negative, Neutral |
| 164 | +- **Framework**: Hugging Face Transformers |
| 165 | +- **Model Size**: ~500MB (downloaded on first run) |
| 166 | + |
| 167 | +## Dependencies |
| 168 | + |
| 169 | +- **transformers** (>=4.30.0): Hugging Face NLP library |
| 170 | +- **torch** (>=2.0.0): PyTorch deep learning framework |
| 171 | +- **pytest** (>=7.4.0): Testing framework |
| 172 | +- **black** (>=23.0.0): Code formatter |
| 173 | +- **flake8** (>=6.0.0): Linter |
| 174 | +- **isort** (>=5.12.0): Import sorter |
| 175 | +- **ruff** (>=0.1.0): Fast Python linter |
| 176 | +- **mypy** (>=1.0.0): Static type checker |
| 177 | + |
| 178 | +## Troubleshooting |
| 179 | + |
| 180 | +### Model Download Issues |
| 181 | + |
| 182 | +The first run downloads the FinBERT model (~500MB). Ensure you have: |
| 183 | +- Stable internet connection |
| 184 | +- Sufficient disk space (at least 1GB free) |
| 185 | +- Proper Hugging Face cache permissions |
| 186 | + |
| 187 | +### CUDA/GPU Issues |
| 188 | + |
| 189 | +If you encounter GPU-related errors, the model will automatically fall back to CPU processing. |
| 190 | + |
| 191 | +### Import Errors |
| 192 | + |
| 193 | +If you get import errors, ensure you've activated the virtual environment and installed all dependencies: |
| 194 | + |
| 195 | +```bash |
| 196 | +source .venv/bin/activate # or .venv\Scripts\activate on Windows |
| 197 | +pip install -r requirements.txt |
| 198 | +``` |
| 199 | + |
| 200 | +## Contributing |
| 201 | + |
| 202 | +We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on: |
| 203 | +- Setting up development environment |
| 204 | +- Making changes |
| 205 | +- Running tests and linting |
| 206 | +- Submitting pull requests |
| 207 | + |
| 208 | +## Code of Conduct |
| 209 | + |
| 210 | +This project adheres to the Contributor Covenant Code of Conduct. See [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) for details. |
| 211 | + |
| 212 | +## License |
| 213 | + |
| 214 | +This project is licensed under the MIT License. See [LICENSE](LICENSE) file for details. |
| 215 | + |
| 216 | +## Citation |
| 217 | + |
| 218 | +If you use this project in your research, please cite: |
| 219 | + |
| 220 | +```bibtex |
| 221 | +@software{stock_sentiment_analyzer, |
| 222 | + title={Stock Sentiment Analyzer}, |
| 223 | + author={Your Name}, |
| 224 | + year={2026}, |
| 225 | + url={https://github.com/MeridianAlgo/Basic-Sentiment-Analysis} |
| 226 | +} |
24 | 227 | ``` |
25 | 228 |
|
| 229 | +## Support |
| 230 | + |
| 231 | +For issues, questions, or suggestions, please open an issue on GitHub. |
| 232 | + |
| 233 | +## Version |
| 234 | + |
| 235 | +Current version: **v1.0.0** |
| 236 | + |
| 237 | +Last updated: January 2026 |
0 commit comments