text-summarizer について
text-summarizer は、文書の抽出型要約を行う Python ツールおよびコマンドラインツールです。複数のアルゴリズムを備え、要約の長さや要点抽出、バッチ処理を指定できます。
主な機能
- 01抽出型要約
- 02TextRank、LSA、頻度ベースの手法
- 03比率・文数・単語数による長さ指定
- 04要点の抽出
- 05テキストとファイルの一括処理
活用シーン
- 01ニュース記事を指定した文数に要約
- 02研究論文をアブストラクト相当の長さに要約
- 03会議メモから要点を抽出
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill text-summarizerインストール後、Claude Code で/text-summarizerを入力して読み込まれたか確認します。
同梱ファイルscripts/requirements.txtscripts/text_summarizer.py
詳細
テキスト要約
抽出型要約を使って、長いテキスト文書から簡潔な要約を作成します。意味を保ちながら、最も重要な文を特定して抽出します。
クイックスタート
from scripts.text_summarizer import TextSummarizer
# Summarize text
summarizer = TextSummarizer()
summary = summarizer.summarize(long_text, ratio=0.2) # 20% of original
print(summary)
# Summarize file
summary = summarizer.summarize_file("article.txt", num_sentences=5)
機能
- 抽出型要約:元のテキストから重要な文を選択します
- 長さの調整:比率、文数、または単語数で指定できます
- 複数のアルゴリズム:TextRank、LSA、頻度ベース
- 要点:箇条書き形式の要約を抽出します
- バッチ処理:複数の文書を要約します
- 構造の維持:文の順序を維持するオプションがあります
API リファレンス
初期化
summarizer = TextSummarizer(
method="textrank", # textrank, lsa, frequency
language="english"
)
要約
# By ratio (20% of original length)
summary = summarizer.summarize(text, ratio=0.2)
# By sentence count
summary = summarizer.summarize(text, num_sentences=5)
# By word count
summary = summarizer.summarize(text, max_words=100)
要点の抽出
# Get bullet points
points = summarizer.extract_key_points(text, num_points=5)
for point in points:
print(f"• {point}")
バッチ処理
# Summarize multiple texts
texts = [text1, text2, text3]
summaries = summarizer.summarize_batch(texts, ratio=0.2)
# Summarize files in directory
summaries = summarizer.summarize_directory("./articles/", ratio=0.3)
オプション
# Preserve original sentence order
summary = summarizer.summarize(text, preserve_order=True)
# Include title/first sentence
summary = summarizer.summarize(text, include_first=True)
# Minimum sentence length filter
summarizer.min_sentence_length = 10
CLI の使用方法
# Summarize text file
python text_summarizer.py --input article.txt --ratio 0.2
# Specific sentence count
python text_summarizer.py --input article.txt --sentences 5
# Extract key points
python text_summarizer.py --input article.txt --points 5
# Batch process
python text_summarizer.py --input-dir ./docs --output-dir ./summaries --ratio 0.3
# Output to file
python text_summarizer.py --input article.txt --output summary.txt --ratio 0.2
CLI 引数
| 引数 | 説明 | デフォルト |
|---|---|---|
--input | 入力ファイルのパス | 必須 |
--output | 出力ファイルのパス | stdout |
--input-dir | ファイルのディレクトリ | - |
--output-dir | 出力ディレクトリ | - |
--ratio | 要約の比率(0.0-1.0) | 0.2 |
--sentences | 文の数 | - |
--words | 単語数の上限 | - |
--points | N 個の要点を抽出 | - |
--method | 使用するアルゴリズム | textrank |
--preserve-order | 文の順序を維持 | False |
使用例
ニュース記事の要約
summarizer = TextSummarizer()
article = """
[Long news article text...]
"""
# Get a 3-sentence summary
summary = summarizer.summarize(article, num_sentences=3)
print("Summary:")
print(summary)
# Get key points
points = summarizer.extract_key_points(article, num_points=5)
print("\nKey Points:")
for i, point in enumerate(points, 1):
print(f"{i}. {point}")
研究論文の要旨
summarizer = TextSummarizer(method="lsa")
paper = open("research_paper.txt").read()
# Create abstract-length summary
abstract = summarizer.summarize(paper, max_words=250)
print(abstract)
会議メモの要約
summarizer = TextSummarizer()
notes = """
Meeting started at 2pm. John presented Q3 results showing 15% growth.
Sarah raised concerns about supply chain delays affecting Q4 projections.
The team discussed mitigation strategies including dual-sourcing.
Budget allocation for marketing was approved at $50k.
Next steps include vendor outreach by Friday.
Follow-up meeting scheduled for next Tuesday.
"""
summary = summarizer.summarize(notes, num_sentences=3)
points = summarizer.extract_key_points(notes, num_points=4)
print("Summary:", summary)
print("\nAction Items:")
for point in points:
print(f"• {point}")
文書のバッチ要約
summarizer = TextSummarizer()
import os
for filename in os.listdir("./documents"):
if filename.endswith(".txt"):
text = open(f"./documents/{filename}").read()
summary = summarizer.summarize(text, ratio=0.2)
with open(f"./summaries/{filename}", "w") as f:
f.write(summary)
print(f"Summarized: {filename}")
アルゴリズムの比較
| アルゴリズム | 速度 | 品質 | 最適な用途 |
|---|---|---|---|
| TextRank | 中 | 高 | 一般的なテキスト |
| LSA | 高速 | 良好 | 技術文書 |
| Frequency | 高速 | 中 | 素早い要約 |
依存関係
nltk>=3.8.0
numpy>=1.24.0
scikit-learn>=1.2.0
制限事項
- 抽出型のみ(言い換えや新しいテキストの生成は行いません)
- 構造の整ったテキスト(段落や明確な文)で最も効果を発揮します
- 非常に短いテキストは、うまく要約できない場合があります
- 文脈を深く理解するわけではないため、ニュアンスを見落とすことがあります
コメント
Markdown 対応