我有一个XML格式的带注释数据集:见下面的例子
Treatment of <annotation cui="C0267055">Erosive Esophagitis</annotation> in patients
其中标记的单词位于XML标记中,如图所示。我需要将其转换为BRAT格式,例如:
T1 annotation 14 33 Erosive Esophagitis
更多示例可以在http://brat.nlplab.org/standoff.html
中找到我可以在Python中使用正则表达式提取注释,但我不确定如何将其转换为正确的BRAT格式。有没有可能的工具?
答案 0 :(得分:0)
如果有人仍然需要这个问题的答案,这是一个解决方案。
假设XML文件sample.xml
具有以下结构:
<root>
<p n='1'>Hi, my name is <fname>Mickey</fname> <lname>Mouse</lname>, and what about yourself?</p>
<p n='2'>Nice meeting you, <fname>Mickey</fname>! I am <fname>Minnie</lname>!</p>
</root>
这是Python解决方案:
# leave empty if there are no tags that should not be interpreted as named entities; or add more
ignoretags = ['root', 'p']
# dictionary, in case some named entities have to be mapped; or just a list of tags that represent NEs
replacetags = {
"fname": "PERS",
"lname": "PERS"
}
# read content
content = open('sample.xml', encoding='utf-8').read()
# output files for BRAT: txt and annotations
f_txt = open('sample.txt', 'w')
f_ann = open('sample.ann', 'w')
# from txt file remove NE tags
clean_content = content
for replacetag in replacetags:
clean_content = clean_content.replace('<{}>'.format(replacetag), '')
clean_content = clean_content.replace('</{}>'.format(replacetag), '')
# write content to file
f_txt.write(clean_content)
# char by char
n = len(content)
i = - 1
# token id
tid = 0
# other - for output
start = -1
end = - 1
token = None
tag = None
# let's start parsing! character by character
skipped_chars = 0
number_of_tags = 0
token_buffer = ''
while i < n - 1:
i += 1
c = content[i]
# beginning of an entity
if c == '<':
# probably the most important part: always track the count of skipped characters
start = i - skipped_chars
# get name of the entity
tag_buffer = ''
i += 1
while content[i] != '>':
tag_buffer += content[i]
i += 1
tag = tag_buffer
# skip tags that are not NEs
if tag not in replacetags:
continue
# get entity itself
ent_buffer = ''
i += 1
while content[i] != '<':
ent_buffer += content[i]
i += 1
token = ent_buffer
# determine positions
end = start + len(token)
number_of_tags += 1
# <fname></fname> i.e. 1 + len('fname') + 1 + 1 + 1 + len('fname') + 1
skipped_chars += 1 + len(tag) + 1 + 1 + 1 + len(tag) + 1
tid += 1
# write annotation
f_ann.write('T{}\t{} {} {}\t{}\n'.format(tid, replacetags[tag], start, end, token))
# navigate to the end of the entity span, e.g. go behind <fname>...</fname>
i += 1 + len(tag) + 1
sample.txt
<root>
<p n='1'>Hi, my name is Mickey Mouse, and what about yourself?</p>
<p n='2'>Nice meeting you, Mickey! I am Minnie!</p>
</root>
sample.ann
的内容:
T1 PERS 31 37 Mickey
T2 PERS 38 43 Mouse
T3 PERS 101 107 Mickey
T4 PERS 114 120 Minnie
在视觉上使用BRAT:
在属性的情况下,需要进行一些细微的调整(我在replacetags
字典中添加了另一个键'att',即一对"fname": {"tag": "PERS", "att": "value of attribute"}
然后,如果标签具有属性,则会另外写一行。
希望有人会对此有所帮助!