pybrat
is a reader/parser for reading/parsing data annotated by brat.
pip install pybrat
pip install git+https://github.com/Yevgnen/pybrat
git clone https://github.com/nlplab/brat.git
Below is an example of parsing BioNLP-ST_2011 data:
# -*- coding: utf-8 -*-
import dataclasses
from pybrat.parser import BratParser, Entity, Event, Example, Relation
# Initialize a parser.
brat = BratParser(error="ignore")
examples = brat.parse("brat/example-data/corpora/BioNLP-ST_2011")
# The praser returns dataclasses.
assert len(examples) == 80
assert all(isinstance(x, Example) for x in examples)
assert all(isinstance(e, Entity) for x in examples for e in x.entities)
assert all(isinstance(e, Relation) for x in examples for e in x.relations)
assert all(isinstance(e, Event) for x in examples for e in x.events)
id_ = "BioNLP-ST_2011_EPI/PMID-19377285"
example = next(x for x in examples if x.id == id_)
print(example.text)
print(len(example.entities), next(iter(example.entities)))
print(len(example.relations), next(iter(example.relations)))
print(len(example.events), next(iter(example.events)))
# Use dataclasses.asdict to convert examples to dictionaries.
examples = [*map(dataclasses.asdict, examples)]
assert all(isinstance(x, dict) for x in examples)
assert all(isinstance(e, dict) for x in examples for e in x["entities"])
assert all(isinstance(e, dict) for x in examples for e in x["relations"])
assert all(isinstance(e, dict) for x in examples for e in x["events"])
print(examples[0])
The pybrat-convert
script can be used to convert Brat examples into JSON files.
pybrat-convert -i brat/example-data/corpora/BioNLP-ST_2011 -o ./output --error ignore
To ensure the codebase complies with a style guide, please use flake8, black and isort tools to format and check codebase for compliance with PEP8.