Skip to content

Repository files navigation

CCL2025 细粒度中文仇恨言论检测

本项目为 CCL2025 Task 10: 细粒度中文仇恨言论检测 的参赛方案。

任务简介

给定一段中文文本,系统需要完成以下细粒度仇恨言论分析:

  1. 仇恨判别:判断文本是否包含仇恨言论(hate / non-hate)
  2. 目标群体识别:识别被攻击的群体类别(Region / Racism / Sexism / LGBTQ / others)
  3. 目标抽取:抽取被攻击的具体对象(Target)
  4. 论点抽取:抽取针对目标的仇恨观点(Argument)

输出格式为结构化四元组:(Target, Argument, Targeted Group, Hateful)

方法概述

本方案采用 SRAG + TR(Structured Retrieval-Augmented Generation + Triple Reasoning) 框架:

  • TR 三元组推理:将四元组抽取简化为三元组(Target, Argument, Group),降低任务复杂度
  • SRAG 检索增强:基于 embedding 相似度检索训练集中的相似样本作为 few-shot 示例
  • SFT 微调:使用 SRAG+TR 格式的训练数据对模型进行监督微调
  • MAV 多轮投票:通过多轮推理投票提升预测稳定性

项目结构

├── data/                 数据目录
├── scripts/              独立脚本
│   ├── build_srag_triple_data.py   生成 SRAG+TR 训练数据
│   ├── train_full_sft.py           全参数 SFT 训练
│   ├── train_lora_sft.py           LoRA SFT 训练
│   └── predict_local_sft.py        本地 SFT 模型推理
├── docs/                 文档与实验记录
├── main.py               CLI 入口(预测/评估/切分/提交)
├── predict.py            推理主流程
├── preprocess.py         few-shot / 检索 / 数据处理
├── prompts.py            prompt 模板
├── mav.py                多轮投票(MAV)
├── evaluate.py           本地评估(hard/soft F1)
├── model_api.py          模型 API 封装
├── data_utils.py         数据格式解析与转换
├── config.py             全局配置
├── parser_and_validator.py  输出解析与校验
├── error_analysis.py     错误分析报告生成
└── experiment_runner.py  批量实验运行器

快速开始

环境安装

pip install -r requirements.txt

配置 API

通过环境变量配置模型 API:

export BASELINE_API_KEY="your-api-key"
export BASELINE_MODEL_NAME="qwen-plus"
export BASELINE_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"

数据切分

python main.py split --input data/train.json \
    --train-output outputs/splits/train_3200.json \
    --dev-output outputs/splits/dev_800.json \
    --dev-ratio 0.2 --seed 42

生成 SRAG+TR 训练数据

python scripts/build_srag_triple_data.py \
    --input data/train.json \
    --output outputs/splits/train_srag_triple.json \
    --top-k 2

预测(TR zero-shot)

python main.py predict --input data/test1.json \
    --output outputs/predictions/test1_predictions.json \
    --mode zero_shot --task-mode triple

评估

python main.py eval \
    --gold outputs/splits/dev_800.json \
    --pred outputs/predictions/dev_predictions.json

SFT 训练

python scripts/train_full_sft.py \
    --train-input outputs/splits/train_srag_triple.json \
    --train-data-format instruction \
    --dev-input outputs/splits/dev_800.json \
    --dev-data-format competition \
    --task-mode triple \
    --output-dir outputs/models/srag_triple_sft

生成提交文件

python main.py submit --pred outputs/predictions/test1_predictions.json \
    --output outputs/submissions/submission.txt

评估指标

采用官方 CCL25 Task 10 评估标准:

  • Hard F1:严格匹配的 F1 分数
  • Soft F1:基于 SequenceMatcher 相似度(阈值 0.5)的 F1 分数
  • Average Score:Hard F1 和 Soft F1 的平均值

依赖

  • Python >= 3.10
  • openai >= 1.30.0
  • tqdm >= 4.66.0
  • pandas >= 2.0.0
  • transformers >= 4.40.0
  • accelerate >= 0.30.0

许可证

MIT License

About

该仓库是我在【打榜赛】CCL25-Eval 任务10:细粒度中文仇恨识别评测上的练习仓库

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages