Shihanmax's blog

< Back

实体、关系标注工具brat的使用

brat是一个基于web的文本标注工具,可以对文本进行结构化注释。能够用于NLP中实体、关系、事件的数据标注,使用简单,可扩展性较高,这里简单记录一下安装和使用方法。

1. 安装

brat是一个基于web的标注平台,目前仅支持类unix平台,我尝试了在Mac系统上安装使用,Linux类似。

首先,安装apache2

1
brew install apache2

启动服务:

1
brew services start httpd

官网下载brat,如果访问缓慢,可以去github克隆源代码。我个人从官网下载失败了,使用的是第二种途径。

代码克隆下来之后,目录如下:

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
total 352
-rw-rw-r--@  1 shihanmax  staff   2.7K  6 28 17:08 CONTRIBUTING.md
-rw-rw-r--@  1 shihanmax  staff   1.4K  6 28 17:08 CONTRIBUTORS.md
-rw-rw-r--@  1 shihanmax  staff   5.5K  6 28 17:08 LICENSE.md
-rw-rw-r--@  1 shihanmax  staff   4.8K  6 28 17:08 README.md
drwxr-xr-x   3 shihanmax  staff    96B 10  2 20:07 __pycache__
-rwxr-xr-x@  1 shihanmax  staff   2.5K  6 28 17:08 ajax.cgi
-rwxr-xr-x@  1 shihanmax  staff   2.1K  6 28 17:08 ajax.fcgi
-rwxr-xr-x@  1 shihanmax  staff   177B  6 28 17:08 apache-group.sh
-rwxr-xr-x@  1 shihanmax  staff   246B  6 28 17:08 apache-user.sh
drwxrwxr-x@  4 shihanmax  staff   128B  6 28 17:08 client
-rw-r--r--   1 shihanmax  staff   3.9K 10  2 20:03 config.py
-rw-rw-r--@  1 shihanmax  staff   3.7K  6 28 17:08 config_template.py
drwxrwxr-x@ 46 shihanmax  staff   1.4K  6 28 17:08 configurations
drwxrwxr-x  11 shihanmax  staff   352B 10  2 20:21 data
-rw-rw-r--@  1 shihanmax  staff    38K  6 28 17:08 diff.xhtml
drwxrwxr-x@  5 shihanmax  staff   160B  6 28 17:08 example-data
drwxrwxr-x@  7 shihanmax  staff   224B  6 28 17:08 external
-rw-rw-r--@  1 shihanmax  staff   1.4K  6 28 17:08 favicon.ico
-rw-rw-r--@  1 shihanmax  staff   231B  6 28 17:08 index.html
-rw-rw-r--@  1 shihanmax  staff    40K  6 28 17:08 index.xhtml
-rwxr-xr-x@  1 shihanmax  staff   5.2K  6 28 17:08 install.sh
-rw-rw-r--@  1 shihanmax  staff   2.5K  6 28 17:08 lighttpd.conf
-rwxr-xr-x@  1 shihanmax  staff   2.4K  6 28 17:08 lighttpd_fcgi.conf
-rw-rw-r--@  1 shihanmax  staff   4.0K  6 28 17:08 offline.xhtml
drwxrwxr-x@  5 shihanmax  staff   160B  6 28 17:08 res
drwxrwxr-x@  4 shihanmax  staff   128B  6 28 17:08 server
-rwxr-xr-x@  1 shihanmax  staff   8.6K  6 28 17:08 standalone.py
drwxrwxr-x@ 10 shihanmax  staff   320B  6 28 17:08 static
-rwxr-xr-x@  1 shihanmax  staff   2.1K  6 28 17:08 testserver.py
drwxrwxr-x@ 59 shihanmax  staff   1.8K  6 28 17:08 tools
-rwxr-xr-x@  1 shihanmax  staff   2.3K  6 28 17:08 tutorial.cgi
drwxrwxr-x   6 shihanmax  staff   192B 10  2 20:56 work

进入这个目录,安装brat:

1
bash install.sh

跟随提示,需要设置用户名、密码、邮箱等信息,然后提示安装完成。

2. 配置

在使用之前,我们需要编写一下标注相关的设置。

如前述,brat支持实体、关系、事件及属性的标注,我使用实体、关系标注进行了试验:

下面是一段新闻:

1
外交部发言人汪文斌主持例行记者会。有记者提问,有报道称阿富汗塔利班方面已经邀请了中国等国参加其新政府组成的有关活动,中方对此有何回应?

定义两个实体类型:人物职位,一个关系类型:职务

我们首先在刚才的brat根目录中找到./data目录,其内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ tree -d
.
├── examples
│   ├── BioNLP-ST_2011
│   │   ├── BioNLP-ST_2011_EPI
│   │   ├── BioNLP-ST_2011_GE
│   │   ├── BioNLP-ST_2011_ID
│   │   └── BioNLP-ST_2011_REL
│   ├── CoNLL-ST_2002
│   │   ├── esp
│   │   └── ned
│   ├── CoNLL-ST_2006
│   ├── NCBI-disease
│   └── TDT
├── my
└── tutorials
    ├── bio
    └── news

16 directories

其中包含了一些演示用的数据集,如BioNLPCoNLLBCBI等。后文会提到,brat启动的时候,文件目录也会指到./data目录下。

./data目录下新建一个my目录,用于保存这次演示的文件。

一个典型的标注任务主要包含以下几类文件:

1
2
3
4
5
6
7
8
9
10
11
12
$ tree
.
├── annotation.conf
├── test1.ann
├── test1.txt
├── test2.ann
├── test2.txt
├── test_news.ann
├── test_news.txt
└── visual.conf

0 directories, 8 files

*.txt:原始数据,文本文件,一条标注数据为一个文件:

1
外交部发言人汪文斌主持例行记者会。有记者提问,有报道称阿富汗塔利班方面已经邀请了中国等国参加其新政府组成的有关活动,中方对此有何回应?

Annatation.conf:实体、关系、事件定义。在[entities]下面定义实体,这里定义两个:人物职位;在[relations]下定义关系,这里定义一个:头衔Arg1Arg2为关系的subject和object。[event][attributes]这里没有使用,前者是定义事件的,后者则可以为实体、关系、事件定义属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
[entities]
人物
职位

[relations]
头衔    Arg1:人物, Arg2:职位

[events]
# (no events defined for REL.)

[attributes]

# (no attributes defined for REL.)

visual.conf:样式自定义。[labels]下方为标签的定义,应当和annotations.txt中定义的类型相同。这里可以为实体类型、关系类型等设置别名(当标签拥挤,显示重叠时,会自动切换到别名标签)。详细设置可以参考官方文档

1
2
3
4
5
6
7
8
9
10
11
12
$ more visual.conf

[labels]
人物 | 人物 | per
职位 | 职位 | zw

[drawing]
SPAN_DEFAULT fgColor:black, bgColor:lightgreen, borderColor:darken
ARC_DEFAULT color:black, arrowHead:triangle-5

职位 bgColor:yellow, borderColor:red
人物 bgColor:pink, borderColor:red

3. 使用

进入brat根目录,使用python3执行文件:

1
python standalone.py

浏览器访问运行结果中的地址,双击选择对应的文件目录,即可进入编辑界面:

brat

划词进行实体标注,从一个实体向另一个实体拖动即可建立实体关系。

在标注完一条数据后,会生成一份同名但不同后缀的.ann文件,其内容如下:

1
2
3
4
$ more test_news.ann
T1      职位 179 185    外交部发言人
T2      人物 185 188    汪文斌
R1      头衔 Arg1:T2 Arg2:T1

对于实体:第一列是实体的编号,第二列是标签名,第三列和第四列是起始和结束位置的索引(左闭右开),最后一列是实体文本。

对于关系:第一列是关系编号,第二列是关系类型,第三列 和第四列分别是subject和object的实体引用。

到这里便完成了brat的简单使用。

refs

  1. brat
  2. brat git-repo