Views
No views yet
1from transformers import pipeline
2
3# 创建分类器
4classifier = pipeline("text-classification", model="WJL110/emotion-classifier")
5
6# 标签映射
7label_map = {
8 "LABEL_0": "快乐",
9 "LABEL_1": "愤怒",
10 "LABEL_2": "悲伤"
11}
12
13# 测试文本
14test_texts = [
15 "今天真是太开心了!",
16 "这件事让我很生气。",
17 "听到这个消息很难过。"
18]
19
20print("=== 情感分析测试 ===")
21for text in test_texts:
22 result = classifier(text)[0] # 获取第一个(也是唯一的)结果
23 emotion = label_map[result['label']]
24 confidence = result['score']
25
26 print(f"\n输入文本: {text}")
27 print(f"预测情感: {emotion}")
28 print(f"置信度: {confidence:.2f}")