薬剤抽出例 🏥
LangExtractは臨床テキストから構造化された医療情報を抽出することに優れており、ヘルスケアアプリケーションに特に有用です。
WARNING
免責事項: このデモンストレーションは、LangExtractの基本機能を説明する目的のみです。完成品または承認された製品を表すものではなく、いかなる疾患または状態の診断または治療の提案を目的としたものではなく、医療アドバイスとして使用すべきではありません。
基本的な固有表現認識 (NER)
この基本的な医療例では、LangExtractが構造化された薬剤情報を抽出します:
python
import langextract as lx
# 薬剤を含むテキスト
input_text = "患者は400mg イブプロフェンを経口で4時間ごとに2日間服用した。"
# 抽出プロンプトを定義
prompt_description = "テキストに出現する順序で、薬剤名、用量、投与経路、頻度、期間を含む薬剤情報を抽出してください。"
# 出現順序での例データを定義
examples = [
lx.data.ExampleData(
text="患者は250mg セファゾリンを静注で1日3回、1週間投与された。",
extractions=[
lx.data.Extraction(extraction_class="dosage", extraction_text="250mg"),
lx.data.Extraction(extraction_class="route", extraction_text="静注"),
lx.data.Extraction(extraction_class="medication", extraction_text="セファゾリン"),
lx.data.Extraction(extraction_class="frequency", extraction_text="1日3回"),
lx.data.Extraction(extraction_class="duration", extraction_text="1週間")
]
)
]
result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt_description,
examples=examples,
model_id="gemini-2.5-pro",
)
# 位置とともにエンティティを表示
print(f"入力: {input_text}\n")
print("抽出されたエンティティ:")
for entity in result.extractions:
position_info = ""
if entity.char_interval:
start, end = entity.char_interval.start_pos, entity.char_interval.end_pos
position_info = f" (位置: {start}-{end})"
print(f"• {entity.extraction_class}: {entity.extraction_text}{position_info}")
# 結果を保存して可視化
lx.io.save_annotated_documents([result], output_name="medical_ner_extraction.jsonl", output_dir=".")
# インタラクティブな可視化を生成
html_content = lx.visualize("medical_ner_extraction.jsonl")
with open("medical_ner_visualization.html", "w") as f:
if hasattr(html_content, 'data'):
f.write(html_content.data)
else:
f.write(html_content)
print("インタラクティブな可視化を medical_ner_visualization.html に保存しました")関係抽出 (RE)
エンティティ間の関係を含むより複雑な抽出には、LangExtractは薬剤とその関連属性を抽出できます:
python
import langextract as lx
# 複数の薬剤を含むテキスト
input_text = """
患者は先月リシノプリルとメトホルミンを処方されました。
彼は高血圧のためリシノプリル10mgを毎日服用していますが、
糖尿病のために1日2回服用すべきメトホルミン500mgをよく飲み忘れます。
"""
# 抽出プロンプトを定義
prompt_description = """
属性を使用して関連情報をグループ化し、薬剤とその詳細を抽出:
1. テキストに出現する順序でエンティティを抽出
2. 各エンティティには薬剤にリンクする 'medication_group' 属性が必要
3. 薬剤に関するすべての詳細は同じ medication_group 値を共有
"""
examples = [
lx.data.ExampleData(
text="患者は心臓の健康のためアスピリン100mgを毎日、就寝時にシンバスタチン20mgを服用している。",
extractions=[
lx.data.Extraction(
extraction_class="medication",
extraction_text="アスピリン",
attributes={"medication_group": "アスピリン"}
),
lx.data.Extraction(
extraction_class="dosage",
extraction_text="100mg",
attributes={"medication_group": "アスピリン"}
),
lx.data.Extraction(
extraction_class="frequency",
extraction_text="毎日",
attributes={"medication_group": "アスピリン"}
),
lx.data.Extraction(
extraction_class="medication",
extraction_text="シンバスタチン",
attributes={"medication_group": "シンバスタチン"}
),
lx.data.Extraction(
extraction_class="dosage",
extraction_text="20mg",
attributes={"medication_group": "シンバスタチン"}
),
lx.data.Extraction(
extraction_class="frequency",
extraction_text="就寝時",
attributes={"medication_group": "シンバスタチン"}
)
]
)
]
result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt_description,
examples=examples,
model_id="gemini-2.5-pro",
)
# 薬剤ごとにグループ化して表示
print("抽出された薬剤:")
medication_groups = {}
for extraction in result.extractions:
if extraction.attributes and "medication_group" in extraction.attributes:
group_name = extraction.attributes["medication_group"]
medication_groups.setdefault(group_name, []).append(extraction)
for med_name, extractions in medication_groups.items():
print(f"\n* {med_name}")
for extraction in extractions:
print(f" • {extraction.extraction_class}: {extraction.extraction_text}")主な特徴
- 固有表現認識: タイプ付きエンティティを抽出(薬剤、用量、経路など)
- 関係抽出: 属性を使用して関連エンティティをグループ化
- 位置追跡: ソーステキスト内のエンティティの正確な位置を記録
- 構造化出力: 医療アプリケーションに適した形式で情報を整理
- インタラクティブな可視化: 複雑な医療抽出を探索するHTML可視化を生成