| name | keyword-extractor |
| description | Extract keywords and key phrases from text using TF-IDF, RAKE, and frequency analysis. Generate word clouds and export to various formats. |
Keyword Extractor
Extract important keywords and key phrases from text documents using multiple algorithms. Supports TF-IDF, RAKE, and simple frequency analysis with word cloud visualization.
Quick Start
from scripts.keyword_extractor import KeywordExtractor
extractor = KeywordExtractor()
keywords = extractor.extract("Your long text document here...")
print(keywords[:10])
keywords = extractor.extract_from_file("document.txt")
extractor.to_wordcloud("keywords.png")
Features
- Multiple Algorithms: TF-IDF, RAKE, frequency-based
- Key Phrases: Extract multi-word phrases, not just single words
- Scoring: Relevance scores for ranking
- Stopword Filtering: Built-in + custom stopwords
- N-gram Support: Unigrams, bigrams, trigrams
- Word Cloud: Visualize keyword importance
- Batch Processing: Process multiple documents
API Reference
Initialization
extractor = KeywordExtractor(
method="tfidf",
max_keywords=20,
min_word_length=3,
ngram_range=(1, 3)
)
Extraction Methods
keywords = extractor.extract(text, method="tfidf")
keywords = extractor.extract(text, method="rake")
keywords = extractor.extract(text, method="frequency")
Results Format
keywords = extractor.extract(text)
keyword_list = extractor.get_keywords(text)
Customization
extractor.add_stopwords(['company', 'product', 'service'])
extractor.min_frequency = 2
extractor.pos_filter = ['NN', 'NNS', 'NNP']
Visualization
extractor.to_wordcloud("wordcloud.png", colormap="viridis")
extractor.plot_keywords("keywords.png", top_n=15)
Export
extractor.to_json("keywords.json")
extractor.to_csv("keywords.csv")
extractor.to_text("keywords.txt")
CLI Usage
python keyword_extractor.py --text "Your text here" --top 10
python keyword_extractor.py --input document.txt --method tfidf --output keywords.json
python keyword_extractor.py --input document.txt --wordcloud cloud.png
python keyword_extractor.py --input-dir ./docs --output keywords_all.csv
CLI Arguments
| Argument | Description | Default |
|---|
--text | Text to analyze | - |
--input | Input file path | - |
--input-dir | Directory of files | - |
--output | Output file | - |
--method | Algorithm (tfidf, rake, frequency) | tfidf |
--top | Number of keywords | 20 |
--ngrams | N-gram range (e.g., "1,2") | 1,3 |
--wordcloud | Generate word cloud | - |
--stopwords | Custom stopwords file | - |
Examples
Article Keyword Extraction
extractor = KeywordExtractor(method="tfidf")
article = """
Machine learning is transforming data science. Deep learning models
are achieving state-of-the-art results in natural language processing
and computer vision. Neural networks continue to advance...
"""
keywords = extractor.extract(article, top_n=10)
for keyword, score in keywords:
print(f"{score:.3f}: {keyword}")
Compare Multiple Documents
extractor = KeywordExtractor(method="tfidf")
docs = [
open("doc1.txt").read(),
open("doc2.txt").read(),
open("doc3.txt").read()
]
for i, doc in enumerate(docs):
keywords = extractor.extract(doc, top_n=5)
print(f"\nDocument {i+1}:")
for kw, score in keywords:
print(f" {kw}: {score:.3f}")
SEO Keyword Research
extractor = KeywordExtractor(
method="rake",
ngram_range=(2, 4),
max_keywords=30
)
webpage_content = open("page.html").read()
keywords = extractor.extract(webpage_content)
high_value = [(kw, s) for kw, s in keywords if s > 0.5]
print("High-value keywords for SEO:")
for kw, score in high_value:
print(f" {kw}")
Algorithm Comparison
| Algorithm | Best For | Strengths |
|---|
| TF-IDF | Document comparison | Finds unique terms, good for search |
| RAKE | Key phrases | Extracts multi-word concepts |
| Frequency | Quick overview | Simple, fast, interpretable |
Dependencies
scikit-learn>=1.2.0
nltk>=3.8.0
pandas>=2.0.0
matplotlib>=3.7.0
wordcloud>=1.9.0
Limitations
- English optimized (other languages need language-specific stopwords)
- Very short texts may not have enough data for TF-IDF
- Domain-specific jargon may need custom stopword handling