-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_train.py
More file actions
80 lines (65 loc) · 2.58 KB
/
Copy pathmodel_train.py
File metadata and controls
80 lines (65 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import joblib
import jieba
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# === 配置 ===
MODEL_DIR = 'models'
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
# === 1. 构造训练数据 (包含一些厦门大学特有的关键词) ===
# 标签: 1=积极, 0=中立, -1=消极
data_samples = [
# 积极
("厦门大学是最美校园,芙蓉湖太漂亮了", 1),
("嘉庚风格的建筑真的很有特色,爱了爱了", 1),
("图书馆的学习氛围很好,空调也很足", 1),
("勤业餐厅的沙茶面味道一绝,推荐", 1),
("感谢志愿者同学,核酸检测很有秩序", 1),
("终于抢到选修课了,开心!", 1),
("白城沙滩的夕阳太美了", 1),
# 中立
("请问翔安校区怎么去本部?", 0),
("明天上午有一节高数课", 0),
("谁有二手自行车出吗?", 0),
("关于国庆期间图书馆开放时间的通知", 0),
("刚刚看到一只猫在路边睡觉", 0),
("求问教务处的电话是多少", 0),
# 消极
("游客太多了,根本没法在学校里骑车", -1),
("网速太慢了,选课系统根本进不去,崩溃", -1),
("宿舍停水了,真的很烦躁", -1),
("食堂的菜越来越贵,量还少", -1),
("这种形式主义的讲座能不能少一点", -1),
("外卖又被偷了,无语", -1),
("这学期的课表排得太满了,累死", -1)
]
# 简单扩充数据量以支持训练
X_raw, y_raw = zip(*data_samples)
X_raw = list(X_raw) * 50
y_raw = list(y_raw) * 50
print(f"正在准备训练数据,共 {len(X_raw)} 条样本...")
# === 2. 分词预处理 ===
def clean_text(text):
words = jieba.cut(text)
return " ".join(words)
X_processed = [clean_text(text) for text in X_raw]
# === 3. 训练模型 ===
print("开始训练朴素贝叶斯模型...")
X_train, X_test, y_train, y_test = train_test_split(X_processed, y_raw, test_size=0.2, random_state=42)
model = make_pipeline(
TfidfVectorizer(token_pattern=r"(?u)\b\w+\b"),
MultinomialNB(alpha=0.1)
)
model.fit(X_train, y_train)
# === 4. 评估与保存 ===
y_pred = model.predict(X_test)
print("模型评估报告:")
print(classification_report(y_test, y_pred, target_names=['消极', '中立', '积极']))
model_path = os.path.join(MODEL_DIR, 'sentiment_nb.pkl')
joblib.dump(model, model_path)
print(f"✅ 模型已保存至: {model_path}")