Text Analytics API是一种自然语言处理服务,能够分析文本中的情感倾向、提取关键短语、识别命名实体等。情感分析(Sentiment Analysis)是其中的核心功能之一,它可以判断一段文本表达的情绪是正面、负面还是中性。
https://[your-region].api.cognitive.microsoft.com/text/analytics/v3.0/sentiment
)原因:
解决方案:
原因:
解决方案:
原因:
解决方案:
原因:
解决方案:
import requests
import json
# 设置API密钥和终结点
subscription_key = "你的API密钥"
endpoint = "https://[your-region].api.cognitive.microsoft.com/text/analytics/v3.0/sentiment"
# 准备请求数据
documents = {
"documents": [
{
"language": "en",
"id": "1",
"text": "I had a wonderful experience! The rooms were wonderful and the staff was helpful."
},
{
"language": "en",
"id": "2",
"text": "I had a terrible time at the hotel. The staff was rude and the food was awful."
}
]
}
# 设置请求头
headers = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": subscription_key
}
# 发送请求
response = requests.post(endpoint, headers=headers, json=documents)
# 处理响应
if response.status_code == 200:
results = response.json()
for document in results["documents"]:
print(f"Document ID: {document['id']}")
print(f"Sentiment: {document['sentiment']}")
print(f"Confidence Scores: {document['confidenceScores']}")
print("-" * 50)
else:
print(f"Error: {response.status_code}")
print(response.text)
通过以上方法和示例,您可以轻松使用Text Analytics API和Postman进行情感分析,获取文本的情感倾向和置信度评分。