日本語テキスト抽出
LangExtractは日本語、中国語、韓国語を含む非英語言語でシームレスに動作します。
考慮事項
- トークン化: LangExtractはマルチバイト文字を自動的に処理します。
- プロンプト: 最良の結果を得るには、ソーステキストと同じ言語でプロンプトを使用してください。
コード例
python
import langextract as lx
# 1. 日本語入力テキスト
input_text = """
東京都は、日本の首都であり、人口は約1400万人です。
知事は小池百合子氏です。
"""
# 2. 日本語プロンプト
prompt_description = """
以下の情報を抽出してください:
- 都市名 (City name)
- 人口 (Population)
- 知事 (Governor)
テキストから正確に抽出し、言い換えは行わないでください。
"""
# 3. 例データを定義
examples = [
lx.data.ExampleData(
text="大阪府は人口約880万人で、知事は吉村洋文氏です。",
extractions=[
lx.data.Extraction(extraction_class="city", extraction_text="大阪府"),
lx.data.Extraction(extraction_class="population", extraction_text="約880万人"),
lx.data.Extraction(extraction_class="governor", extraction_text="吉村洋文")
]
)
]
# 4. 抽出を実行
result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt_description,
examples=examples,
model_id="gemini-2.5-flash",
)
# 5. 結果を表示
print("抽出されたエンティティ:")
for entity in result.extractions:
print(f"• {entity.extraction_class}: {entity.extraction_text}")期待される出力
抽出されたエンティティ:
• city: 東京都
• population: 約1400万人
• governor: 小池百合子