A modular Python project for extracting keywords from BBC News articles using NLTK, spaCy, and scikit-learn. Extract keywords via word frequency, TF-IDF scores, and noun phrases.
- π Multiple Extraction Methods
- Word Frequency (NLTK)
- TF-IDF Scores (scikit-learn)
- Noun Phrases (spaCy)
- π§Ή Text Preprocessing: Tokenization, cleaning, stopword removal (NLTK)
- π Data Visualization: Built-in plotting utilities
- ποΈ Modular Design: Reusable functions and clear package structure
- π Exploratory Analysis: Jupyter notebook for interactive exploration
- β Well Tested: Unit tests for core functionality
git clone <https://github.com/DebanganMALI/Keyword-Extractor.git>
cd Keyword-Extractor# Windows (PowerShell)
python -m venv venv
& '.\venv\Scripts\Activate.ps1'
# macOS/Linux
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txt# NLTK data (tokenization, stopwords)
python -c "import nltk; nltk.download('punkt'); nltk.download('stopwords'); nltk.download('punkt_tab')"
# spaCy English model
python -m spacy download en_core_web_smThe project expects data/bbc_news.csv with columns like title, description, etc.
- Download the BBC News dataset from Kaggle
- Or use any CSV with a
text,description, orcontentcolumn
# From project root (recommended)
python run.py
# Or run as a module
python -m src.mainPreprocessing texts using NLTK...
Extracting top keywords with frequency (NLTK) for first 3 docs:
Doc 1: ['ukrainian', 'president', 'says', 'country', 'forgive', 'forget', 'murder', 'civilians']
Doc 2: ['jeremy', 'bowen', 'frontline', 'irpin', 'residents', 'came', 'russian', 'fire', 'trying', 'flee']
Doc 3: ['one', 'worlds', 'biggest', 'fertiliser', 'firms', 'says', 'conflict', 'could', 'deliver', 'shock']
Extracting top TF-IDF keywords (NLTK) for first 3 docs:
Doc 1: ['forgive', 'forget', 'civilians', 'murder', 'ukrainian', 'country', 'president', 'says']
...
Keyword-Extractor/
βββ src/ # Main package
β βββ __init__.py # Package exports
β βββ main.py # Entry point
β βββ data_preprocessing.py # Text cleaning & tokenization
β βββ keyword_extraction_nltk.py # NLTK-based extraction
β βββ keyword_extraction_spacy.py # spaCy noun phrase extraction
β βββ utils.py # Data loading, plotting, saving
βββ notebooks/
β βββ exploratory_analysis.ipynb # Interactive data exploration
βββ data/
β βββ bbc_news.csv # BBC News dataset
βββ tests/
β βββ __init__.py
β βββ test_preprocessing.py # Tests for preprocessing
β βββ test_extraction.py # Tests for extraction
βββ .github/
β βββ workflows/
β βββ test.yml # GitHub Actions CI/CD
βββ run.py # Convenience runner
βββ requirements.txt # Python dependencies
βββ .gitignore # Git ignore rules
βββ LICENSE # MIT License
βββ README.md # This file
Handles text cleaning and tokenization:
from src.data_preprocessing import preprocess, preprocess_bulk
tokens = preprocess("Hello world!") # ['hello', 'world']
tokenized = preprocess_bulk(["Doc 1", "Doc 2"])Extract keywords using frequency and TF-IDF:
from src.keyword_extraction_nltk import extract_freq_keywords, extract_tfidf_keywords
keywords = extract_freq_keywords(tokens, top_n=10)
tfidf_kws = extract_tfidf_keywords(texts, top_n=10)Extract noun phrases:
from src.keyword_extraction_spacy import extract_noun_phrases, top_noun_phrases
phrases = extract_noun_phrases("The quick brown fox jumps")
top = top_noun_phrases(texts, top_n=10)Utilities for data I/O and visualization:
from src.utils import load_data, plot_keywords, save_keywords
df = load_data('data/articles.csv')
plot_keywords(keywords, title="Keyword Frequency", top_n=10)
save_keywords(['python', 'javascript'], 'output/keywords.txt')# Run all tests
pytest tests/ -v
# Run with coverage report
pytest tests/ --cov=src --cov-report=html- pandas: Data loading and manipulation
- nltk: Tokenization and stopword removal
- spacy: NLP and noun phrase extraction
- scikit-learn: TF-IDF vectorization
- matplotlib: Visualization
- seaborn: Statistical visualization
- jupyter: Interactive notebooks
See requirements.txt for exact versions.
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Commit changes:
git commit -m "Add my feature" - Push to branch:
git push origin feature/my-feature - Open a Pull Request
- Follow PEP 8
- Add docstrings to all functions
- Write unit tests for new features
- Ensure tests pass:
pytest tests/
Found a bug? Have a suggestion? Please open an issue.
This project is licensed under the MIT License β see LICENSE for details.
Debangan Mali
Happy Keyword Extracting! π