named-entity-extractor について
Named Entity Extractorは、テキストから人物、組織、場所、日付、連絡先などのエンティティを抽出・整理するPythonツールです。spaCyと正規表現のモードがあり、一括処理や分類、JSON・CSV出力に対応しています。
主な機能
- 01人物、場所、日付などの抽出
- 02spaCyと正規表現のモード
- 03文書の一括処理
- 04エンティティの関連付けと分類
- 05JSON・CSV出力
- 06HTMLでのエンティティ強調表示
活用シーン
- 01記事に登場する人物や組織の分析
- 02テキストから連絡先を抽出
- 03固有エンティティによる文書タグ付け
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill named-entity-extractorインストール後、Claude Code で/named-entity-extractorを入力して読み込まれたか確認します。
同梱ファイルscripts/entity_extractor.pyscripts/requirements.txt
詳細
固有表現抽出器
人物、組織、場所、日付など、テキストから固有表現を抽出します。
特長
- エンティティタイプ:人物、組織、場所、日付、金額、割合
- 複数のモデル:精度重視の spaCy と、速度重視の regex
- バッチ処理:複数のドキュメントを処理
- エンティティのリンク:テキスト全体で同一のエンティティをグループ化
- エクスポート:JSON、CSV 形式で出力
- 可視化:エンティティをハイライト表示
クイックスタート
from entity_extractor import EntityExtractor
extractor = EntityExtractor()
text = "Apple Inc. was founded by Steve Jobs in Cupertino, California in 1976."
entities = extractor.extract(text)
for entity in entities:
print(f"{entity['text']}: {entity['type']}")
# Output:
# Apple Inc.: ORG
# Steve Jobs: PERSON
# Cupertino: GPE
# California: GPE
# 1976: DATE
CLI の使用方法
# Extract from text
python entity_extractor.py --text "Steve Jobs founded Apple in California."
# Extract from file
python entity_extractor.py --input document.txt
# Batch process folder
python entity_extractor.py --input ./documents/ --output entities.csv
# Filter by entity type
python entity_extractor.py --input document.txt --types PERSON,ORG
# Use regex mode (faster, less accurate)
python entity_extractor.py --input document.txt --mode regex
# JSON output
python entity_extractor.py --input document.txt --json
API リファレンス
EntityExtractor クラス
class EntityExtractor:
def __init__(self, mode: str = "spacy", model: str = "en_core_web_sm")
# Extraction
def extract(self, text: str) -> list
def extract_file(self, filepath: str) -> list
def extract_batch(self, folder: str) -> dict
# Filtering
def filter_entities(self, entities: list, types: list) -> list
def get_unique_entities(self, entities: list) -> list
def group_by_type(self, entities: list) -> dict
# Analysis
def entity_frequency(self, text: str) -> dict
def find_relationships(self, text: str) -> list
# Export
def to_csv(self, entities: list, output: str) -> str
def to_json(self, entities: list, output: str) -> str
def highlight_text(self, text: str) -> str
エンティティタイプ
標準エンティティタイプ(spaCy)
| タイプ | 説明 | 例 |
|---|---|---|
| PERSON | 架空の人物を含む人物 | "Steve Jobs" |
| ORG | 企業、機関、組織 | "Apple Inc." |
| GPE | 国、都市、州 | "California" |
| LOC | GPE 以外の場所、山、水域 | "Pacific Ocean" |
| DATE | 日付、期間 | "January 2024" |
| TIME | 時刻 | "3:30 PM" |
| MONEY | 金額 | "$1.5 million" |
| PERCENT | 割合 | "20%" |
| PRODUCT | 製品 | "iPhone" |
| EVENT | イベント | "World Cup" |
| WORK_OF_ART | 書籍、楽曲など | "The Great Gatsby" |
| LAW | 法律、規制 | "GDPR" |
| LANGUAGE | 言語 | "English" |
| NORP | 国籍、グループ | "American" |
Regex モードのエンティティ
regex パターンを使って、より高速に抽出します。
| タイプ | 説明 |
|---|---|
| メールアドレス | |
| PHONE | 電話番号 |
| URL | Web URL |
| DATE | 一般的な日付形式 |
| MONEY | 通貨額 |
| PERCENTAGE | 割合 |
出力形式
エンティティの結果
{
"text": "Steve Jobs",
"type": "PERSON",
"start": 10,
"end": 20,
"confidence": 0.95
}
抽出結果の全体
{
"text": "Original text...",
"entities": [
{"text": "Steve Jobs", "type": "PERSON", "start": 10, "end": 20},
{"text": "Apple Inc.", "type": "ORG", "start": 30, "end": 40}
],
"summary": {
"total_entities": 5,
"unique_entities": 4,
"by_type": {
"PERSON": 2,
"ORG": 1,
"GPE": 2
}
}
}
フィルタリングとグループ化
タイプでフィルタリング
entities = extractor.extract(text)
# Get only people and organizations
filtered = extractor.filter_entities(entities, ["PERSON", "ORG"])
一意なエンティティを取得
# Remove duplicates, keep first occurrence
unique = extractor.get_unique_entities(entities)
タイプ別にグループ化
grouped = extractor.group_by_type(entities)
# Returns:
{
"PERSON": ["Steve Jobs", "Tim Cook"],
"ORG": ["Apple Inc."],
"GPE": ["California", "Cupertino"]
}
エンティティの頻度
frequency = extractor.entity_frequency(text)
# Returns:
{
"Steve Jobs": {"count": 5, "type": "PERSON"},
"Apple": {"count": 8, "type": "ORG"},
"California": {"count": 2, "type": "GPE"}
}
バッチ処理
フォルダーを処理
results = extractor.extract_batch("./documents/")
# Returns:
{
"doc1.txt": {
"entities": [...],
"summary": {...}
},
"doc2.txt": {
"entities": [...],
"summary": {...}
}
}
CSV にエクスポート
extractor.to_csv(results, "entities.csv")
# Creates CSV with columns:
# filename, entity_text, entity_type, start, end
テキストのハイライト表示
エンティティをハイライト表示した HTML を生成します。
html = extractor.highlight_text(text)
# Returns HTML with colored spans for each entity type
ワークフローの例
ドキュメント分析
extractor = EntityExtractor()
# Analyze a document
text = open("article.txt").read()
result = extractor.extract(text)
# Get key people mentioned
people = extractor.filter_entities(result, ["PERSON"])
print(f"People mentioned: {len(people)}")
# Get frequency
freq = extractor.entity_frequency(text)
top_entities = sorted(freq.items(), key=lambda x: x[1]["count"], reverse=True)[:10]
連絡先情報の抽出
extractor = EntityExtractor(mode="regex")
text = """
Contact John Smith at [email protected]
or call (555) 123-4567.
"""
entities = extractor.extract(text)
# Finds: EMAIL, PHONE entities
コンテンツのタグ付け
extractor = EntityExtractor()
articles = ["article1.txt", "article2.txt", "article3.txt"]
tags = {}
for article in articles:
entities = extractor.extract_file(article)
tags[article] = extractor.get_unique_entities(entities)
依存関係
- spacy>=3.7.0
- pandas>=2.0.0
- en_core_web_sm(spaCy モデル)
注:モデルをインストールするには、python -m spacy download en_core_web_sm を実行してください。
コメント
Markdown 対応