给定一条 commit 标题(type(scope): description),在去掉 type 前缀之后,预测它属于哪一类:feat / fix / docs / chore / refactor。
模型只看描述部分(以及可选的 scope),所以它不是在“抄答案”,而是真的从描述推断类型。适合用来给暂未写类型的提交建议一个类型,或检查 type 与描述是否匹配。
cc-type-classifier| 项 | 内容 |
|---|---|
| 输入 | commit 描述,例如 fix crash when parsing an empty toml file |
| 输出 | 5 个类别上的概率分布,取最大者为预测类型 |
| 类别 | feat fix docs chore refactor |
| 语言 | 中英混合(训练数据以英文为主,含中文提交) |
数据来自 AtomGit 上 4 个仓颉社区仓库的公开提交历史,通过 AtomGit API 分页抓取(脚本见 collect_commits.py):
| 仓库 | 保留的样本 |
|---|---|
Cangjie/cangjie_compiler | 785 |
Cangjie/cangjie_tools | 515 |
Cangjie/cangjie_stdx | 437 |
Cangjie/cangjie_docs | 186 |
| 合计 | 1923 |
feat / fix / docs / chore / refactor / perf / test / build / ci / style / revert)的提交;合并提交(!612 merge ...)等非规范信息被丢弃。perf(14) / test(4) / build(3) / ci(2) 因样本过少未作为类别,对应的提交不参与训练。fix 1225 条,feat 292,chore 218,docs 123,refactor 42。数据集随模型一起发布:data/commits.jsonl(每行一条,含 repo / sha / date / type / scope / description / subject)。
char_wb n-gram(1,4) 与空白切分词 n-gram(1,2),min_df=2、sublinear_tf=True;scope 作为独立 token(scope_<name>)拼在末尾。LogisticRegression(max_iter=2000, class_weight="balanced")。C ∈ {1,2,4,8,16} 上按验证集 macro-F1 选参,最终用 80% 数据重训。| 指标 | 数值 |
|---|---|
| 测试集大小 | 380 |
| Accuracy | 0.6737 |
多数类基线(全部猜 fix) | 0.6263 |
| Macro F1 | 0.4411 |
| Weighted F1 | 0.6437 |
各类别表现(测试集):
| 类别 | Precision | Recall | F1 | 支持数 |
|---|---|---|---|---|
| chore | 0.89 | 0.76 | 0.82 | 41 |
| docs | 0.36 | 0.10 | 0.16 | 40 |
| feat | 0.31 | 0.29 | 0.30 | 52 |
| fix | 0.73 | 0.86 | 0.79 | 238 |
| refactor | 0.20 | 0.11 | 0.14 | 9 |
混淆矩阵(行=真实,列=预测,顺序 chore docs feat fix refactor):
[[ 31 0 1 9 0]
[ 3 4 6 26 1]
[ 1 1 15 34 1]
[ 0 6 25 205 2]
[ 0 0 1 7 1]]完整指标见 model/metrics.json,可用 train.py 复现。
pip install -r requirements.txt
# 命令行
python predict.py "fix: crash when parsing an empty toml file"
python predict.py "补充中心仓的使用说明"
# 管道
git log --format=%s -n 20 | python predict.py
# 输出 JSON(每行一条,便于脚本消费)
python predict.py --json "fix: crash when parsing an empty toml file"
git log --format=%s -n 20 | python predict.py --jsonimport joblib
model = joblib.load("model/cc-type-clf.joblib")
text = "crash when parsing an empty toml file" # 已去掉 type 前缀
ranked = sorted(zip(model.classes_, model.predict_proba([text])[0]),
key=lambda kv: -kv[1])
print(ranked[0]) # ('fix', 0.85...)模型权重以 Git LFS 存储,克隆后请先执行 git lfs pull 再加载 model/cc-type-clf.joblib。
predict.py 会自动剥离 type(scope): 前缀再推理,所以可以直接把完整的 commit 标题交给它。
python -m unittest discover -s tests -v测试覆盖 predict.py 的前缀剥离、scope token、无前缀标题和概率排序四个场景;使用假模型,不依赖 LFS 权重,因此克隆后无需先 git lfs pull 即可运行。
docs / refactor 的召回率很低,多数被误判为 fix。fix 与 feat(例如 “support xxx”),这类样本即便人工标注也有歧义,模型无法可靠区分。LICENSE 为完整文本。| 文件 | 说明 |
|---|---|
model-config.yaml | 模型元信息(名称、版本、框架、任务、依赖) |
model/cc-type-clf.joblib | 训练好的 sklearn Pipeline(特征 + 分类器),通过 Git LFS 存储 |
model/metrics.json | 完整评估指标、混淆矩阵、选参记录 |
data/commits.jsonl | 训练/评估数据集(1923 条) |
collect_commits.py | 从 AtomGit API 抓取提交标题 |
make_dataset.py | 解析前缀、生成 data/commits.jsonl |
train.py | 训练与评估,产出 model/ |
predict.py | 命令行推理 |
tests/test_predict.py | predict.py 的前缀解析与排序单测 |
python collect_commits.py <ATOMGIT_TOKEN> "cangjie_tools,cangjie_docs,cangjie_stdx,cangjie_compiler" 30 commit_subjects.jsonl
python make_dataset.py
python train.py由于抓取结果会随仓库历史增长而变化,重新生成的数据集不会与随包发布的 data/commits.jsonl 逐字节一致;评估数字也会略有浮动。