keyword-extractor について
keyword-extractorは、テキストからキーワードやフレーズを抽出して順位付けするPythonツールです。TF-IDF、RAKE、頻度分析のほか、可視化、一括処理、エクスポートに対応します。
主な機能
- 01TF-IDF、RAKE、頻度分析
- 02複数語のフレーズ抽出
- 03関連度スコアとn-gram
- 04組み込みおよびカスタムストップワード
- 05ワードクラウドとキーワードチャート
- 06一括処理とファイル出力
活用シーン
- 01記事や文書の重要語句を抽出
- 02複数文書間のキーワードを比較
- 03ウェブページの内容からフレーズを調査
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill keyword-extractorインストール後、Claude Code で/keyword-extractorを入力して読み込まれたか確認します。
同梱ファイルscripts/keyword_extractor.pyscripts/requirements.txt
詳細
キーワード抽出
複数のアルゴリズムを使用して、テキスト文書から重要なキーワードやキーフレーズを抽出します。TF-IDF、RAKE、シンプルな頻度分析に対応し、ワードクラウドで可視化できます。
クイックスタート
from scripts.keyword_extractor import KeywordExtractor
# Extract keywords
extractor = KeywordExtractor()
keywords = extractor.extract("Your long text document here...")
print(keywords[:10]) # Top 10 keywords
# From file
keywords = extractor.extract_from_file("document.txt")
extractor.to_wordcloud("keywords.png")
機能
- 複数のアルゴリズム:TF-IDF、RAKE、頻度ベース
- キーフレーズ:単語だけでなく、複数語からなるフレーズも抽出
- スコアリング:順位付けに使用する関連度スコア
- ストップワードのフィルタリング:組み込みのストップワードとカスタムストップワード
- N-gram 対応:ユニグラム、バイグラム、トライグラム
- ワードクラウド:キーワードの重要度を可視化
- バッチ処理:複数の文書を処理
API リファレンス
初期化
extractor = KeywordExtractor(
method="tfidf", # tfidf, rake, frequency
max_keywords=20, # Maximum keywords to return
min_word_length=3, # Minimum word length
ngram_range=(1, 3) # Unigrams to trigrams
)
抽出メソッド
# TF-IDF (best for comparing documents)
keywords = extractor.extract(text, method="tfidf")
# RAKE (best for key phrases)
keywords = extractor.extract(text, method="rake")
# Frequency (simple word counts)
keywords = extractor.extract(text, method="frequency")
結果の形式
keywords = extractor.extract(text)
# Returns list of tuples: [(keyword, score), ...]
# [('machine learning', 0.85), ('data science', 0.72), ...]
# Get just keywords
keyword_list = extractor.get_keywords(text)
# ['machine learning', 'data science', ...]
カスタマイズ
# Add custom stopwords
extractor.add_stopwords(['company', 'product', 'service'])
# Set minimum frequency
extractor.min_frequency = 2
# Filter by part of speech (nouns only)
extractor.pos_filter = ['NN', 'NNS', 'NNP']
可視化
# Generate word cloud
extractor.to_wordcloud("wordcloud.png", colormap="viridis")
# Bar chart of top keywords
extractor.plot_keywords("keywords.png", top_n=15)
エクスポート
# To JSON
extractor.to_json("keywords.json")
# To CSV
extractor.to_csv("keywords.csv")
# To plain text
extractor.to_text("keywords.txt")
CLI の使用方法
# Extract from text
python keyword_extractor.py --text "Your text here" --top 10
# Extract from file
python keyword_extractor.py --input document.txt --method tfidf --output keywords.json
# Generate word cloud
python keyword_extractor.py --input document.txt --wordcloud cloud.png
# Batch process directory
python keyword_extractor.py --input-dir ./docs --output keywords_all.csv
CLI 引数
| 引数 | 説明 | デフォルト |
|---|---|---|
--text | 分析するテキスト | - |
--input | 入力ファイルのパス | - |
--input-dir | ファイルが格納されたディレクトリ | - |
--output | 出力ファイル | - |
--method | アルゴリズム(tfidf、rake、frequency) | tfidf |
--top | キーワード数 | 20 |
--ngrams | N-gram の範囲(例:「1,2」) | 1,3 |
--wordcloud | ワードクラウドを生成 | - |
--stopwords | カスタムストップワードファイル | - |
例
記事からのキーワード抽出
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}")
複数の文書を比較
extractor = KeywordExtractor(method="tfidf")
docs = [
open("doc1.txt").read(),
open("doc2.txt").read(),
open("doc3.txt").read()
]
# Extract keywords from each
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 キーワード調査
extractor = KeywordExtractor(
method="rake",
ngram_range=(2, 4), # Focus on phrases
max_keywords=30
)
webpage_content = open("page.html").read()
keywords = extractor.extract(webpage_content)
# Filter by score threshold
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}")
アルゴリズムの比較
| アルゴリズム | 最適な用途 | 長所 |
|---|---|---|
| TF-IDF | 文書の比較 | 独自性の高い用語を見つけられ、検索に適しています |
| RAKE | キーフレーズ | 複数語からなる概念を抽出します |
| 頻度 | 手早い概要の把握 | シンプル、高速で、結果を解釈しやすい |
依存関係
scikit-learn>=1.2.0
nltk>=3.8.0
pandas>=2.0.0
matplotlib>=3.7.0
wordcloud>=1.9.0
制限事項
- 英語に最適化されています(他の言語では、その言語に固有のストップワードが必要です)
- 非常に短いテキストでは、TF-IDF に十分なデータがない場合があります
- ドメイン固有の専門用語には、カスタムストップワードの設定が必要な場合があります
コメント
Markdown 対応