Azure AI 文本分析 Python SDK
Azure AI Text Analytics Python SDK
Azure AI Language 服务 NLP 功能的客户端库,包括情感分析、实体识别、关键短语等。
安装
pip install azure-ai-textanalytics环境变量
AZURE_LANGUAGE_ENDPOINT=https://<resource>.cognitiveservices.azure.com
AZURE_LANGUAGE_KEY=<your-api-key> # 如果使用 API 密钥身份验证
API 密钥
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))
Entra ID (推荐)
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import DefaultAzureCredential
client = TextAnalyticsClient(
endpoint=os.environ["AZURE_LANGUAGE_ENDPOINT"],
credential=DefaultAzureCredential()
)
情感分析
documents = [
"I had a wonderful trip to Seattle last week!",
"The food was terrible and the service was slow."
]
result = client.analyze_sentiment(documents, show_opinion_mining=True)
for doc in result:
if not doc.is_error:
print(f"Sentiment: {doc.sentiment}")
print(f"Scores: pos={doc.confidence_scores.positive:.2f}, "
f"neg={doc.confidence_scores.negative:.2f}, "
f"neu={doc.confidence_scores.neutral:.2f}")
# 观点挖掘 (基于方面的情感分析)
for sentence in doc.sentences:
for opinion in sentence.mined_opinions:
target = opinion.target
print(f" Target: '{target.text}' - {target.sentiment}")
for assessment in opinion.assessments:
print(f" Assessment: '{assessment.text}' - {assessment.sentiment}")
实体识别
documents = ["Microsoft was founded by Bill Gates and Paul Allen in Albuquerque."]
result = client.recognize_entities(documents)
for doc in result:
if not doc.is_error:
for entity in doc.entities:
print(f"Entity: {entity.text}")
print(f" Category: {entity.category}")
print(f" Subcategory: {entity.subcategory}")
print(f" Confidence: {entity.confidence_score:.2f}")
PII 检测
documents = ["My SSN is 123-45-6789 and my email is [email protected]"]
result = client.recognize_pii_entities(documents)
for doc in result:
if not doc.is_error:
print(f"Redacted: {doc.redacted_text}")
for entity in doc.entities:
print(f"PII: {entity.text} ({entity.category})")
关键短语提取
documents = ["Azure AI provides powerful machine learning capabilities for developers."]
result = client.extract_key_phrases(documents)
for doc in result:
if not doc.is_error:
print(f"Key phrases: {doc.key_phrases}")
语言检测
documents = ["Ce document est en francais.", "This is written in English."]
result = client.detect_language(documents)
for doc in result:
if not doc.is_error:
print(f"Language: {doc.
primary_language.name} ({doc.primary_language.iso6391_name})")
print(f"Confidence: {doc.primary_language.confidence_score:.2f}")
## 医疗文本分析documents = ["Patient has diabetes and was prescribed metformin 500mg twice daily."]
poller = client.begin_analyze_healthcare_entities(documents)
result = poller.result()
for doc in result:
if not doc.is_error:
for entity in doc.entities:
print(f"Entity: {entity.text}")
print(f" Category: {entity.category}")
print(f" Normalized: {entity.normalized_text}")
# 实体链接 (UMLS 等)
for link in entity.data_sources:
print(f" Link: {link.name} - {link.entity_id}")
## 多重分析(批处理)from azure.ai.textanalytics import (
RecognizeEntitiesAction,
ExtractKeyPhrasesAction,
AnalyzeSentimentAction
)
documents = ["Microsoft announced new Azure AI features at Build conference."]
poller = client.begin_analyze_actions(
documents,
actions=[
RecognizeEntitiesAction(),
ExtractKeyPhrasesAction(),
AnalyzeSentimentAction()
]
)
results = poller.result()
for doc_results in results:
for result in doc_results:
if result.kind == "EntityRecognition":
print(f"Entities: {[e.text for e in result.entities]}")
elif result.kind == "KeyPhraseExtraction":
print(f"Key phrases: {result.key_phrases}")
elif result.kind == "SentimentAnalysis":
print(f"Sentiment: {result.sentiment}")
## 异步客户端from azure.ai.textanalytics.aio import TextAnalyticsClient
from azure.identity.aio import DefaultAzureCredential
async def analyze():
async with TextAnalyticsClient(
endpoint=endpoint,
credential=DefaultAzureCredential()
) as client:
result = await client.analyze_sentiment(documents)
# 处理结果...
``
客户端类型
| 客户端 | 用途 |
|--------|---------|
| TextAnalyticsClient | 所有文本分析操作 |TextAnalyticsClient
| (aio) | 异步版本 |
可用操作
| 方法 | 描述 |
|--------|-------------|
| analyze_sentiment | 包含观点挖掘的情感分析 |recognize_entities
| | 命名实体识别 |recognize_pii_entities
| | PII(个人可识别信息)检测与脱敏 |recognize_linked_entities
| | 实体链接至 Wikipedia |extract_key_phrases
| | 关键短语提取 |detect_language
| | 语言检测 |begin_analyze_healthcare_entities
| | 医疗 NLP(长耗时操作) |begin_analyze_actions` | 批处理执行多项分析 |
|
最佳实践
1. 使用批处理操作处理多个文档(每次请求最多 10 个)
2. 启用观点挖掘以获取详细的基于方面的 sentiment 分析
3. 使用异步客户端以应对高吞吐量场景
4. 处理文档错误 —— 结果列表中可能包含部分文档的错误信息
5. 在已知语言时指定语言以提高准确率
6. 使用上下文管理器或显式关闭客户端
适用场景
本技能适用于执行概览中所描述的工作流或操作。局限性
- 仅在任务明确符合上述范围时使用此技能。
- 不要将输出结果视为环境特定验证、测试或专家评审的替代方案。
- 如果缺失必要的输入、权限、安全边界或成功标准,请停止并请求澄清。