共计 1921 个字符,预计需要花费 5 分钟才能阅读完成。
Brat 数据标注实战指南:从零搭建高效标注系统
为什么选择 Brat 进行 NLP 标注
在 NLP 项目中,高质量的数据标注是模型效果的基础保障。Brat 作为老牌标注工具,具有以下不可替代的优势:

- 支持复杂的结构化标注(实体、关系、事件)
- 开源免费且支持本地化部署
- 可视化界面友好,支持实时协作
但新手常面临三大痛点:
- 环境配置复杂(需要 Apache+CGI 配置)
- 批量处理能力弱
- 缺乏团队协作规范
主流工具横向对比
| 工具 | 优点 | 缺点 |
|---|---|---|
| Brat | 支持复杂标注结构,开源 | 配置复杂,缺乏现代 UI |
| Prodigy | 交互式标注,主动学习 | 商业软件价格昂贵 |
| LabelStudio | 现代化界面,多模态支持 | 复杂标注配置困难 |
对于需要精细标注的 NLP 研究项目,Brat 仍是性价比最高的选择。
Docker 化部署方案
通过 Docker-Compose 一键部署,避免环境配置噩梦:
version: '3'
services:
brat:
image: cassj/brat
ports:
- "8080:80" # 映射到宿主机的 8080 端口
volumes:
- ./config:/brat/config # 挂载配置文件
- ./data:/brat/data # 挂载标注数据
environment:
- BRAT_USERNAME=admin
- BRAT_PASSWORD=123456
- BRAT_EMAIL=admin@example.com
关键参数说明:
/data目录需要提前设置 777 权限- 首次启动会自动生成 config.py
- 建议通过 Nginx 配置 HTTPS 加密
自动化处理脚本
使用 Python 实现 .ann 与.txt的批量校验:
import os
from pathlib import Path
def validate_brat_files(data_dir):
"""校验成对的 txt 和 ann 文件"""
errors = []
for txt_file in Path(data_dir).glob('*.txt'):
ann_file = txt_file.with_suffix('.ann')
if not ann_file.exists():
errors.append(f'Missing annotation: {txt_file.name}')
continue
with open(txt_file, 'r', encoding='utf-8') as f:
text = f.read()
with open(ann_file, 'r', encoding='utf-8') as f:
anns = f.readlines()
# 校验标注 ID 连续性
ids = [int(line.split('\t')[0][1:]) for line in anns
if line.startswith('T')]
if ids and ids != list(range(1, max(ids)+1)):
errors.append(f'ID discontinuity in {ann_file.name}')
return errors
多用户协作方案
通过 Apache 的 Basic Auth 实现权限控制:
<Directory "/brat/data">
AuthType Basic
AuthName "Brat Annotation"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
# 按目录设置不同权限
<Files "project1/*">
Require user admin reviewer1
</Files>
</Directory>
操作步骤:
- 使用 htpasswd 创建用户
- 将不同项目分配到不同目录
- 设置用户组权限
性能优化技巧
通过 Nginx 缓存提升加载速度:
server {location ~ \.(txt|ann)$ {
expires 1h;
add_header Cache-Control "public";
}
location / {
proxy_pass http://brat:80;
proxy_set_header Host $host;
}
}
实测可降低 50% 以上的重复请求延迟。
避坑指南
- 中文乱码问题:
- 确保所有文件保存为 UTF- 8 编码
-
在 config.py 中设置
DISPLAY_CHARSET = 'utf-8' -
跨域问题:
-
在 Apache 配置中添加
Header set Access-Control-Allow-Origin "*" -
标注重叠报错:
- 修改
config.py中的OVERLAPPING_SPANS参数
实战建议
现在您可以:
- 尝试标注一个医疗 NER 任务(症状、药品实体)
- 导出 JSON 格式与 spaCy 或 HuggingFace 数据集对接
- 在 GitHub 分享您的标注规范文档
期待看到您的标注成果!遇到问题时,Brat 的 GitHub wiki 有详细 FAQ 可供参考。
正文完
