语言:
- 英文
组件:
- 文本: "蓬塔卡纳是多米尼加共和国最东部的拉阿尔塔格拉西亚省伊圭市的一个度假小镇"
标签:
- 序列到序列
- 关系抽取
数据集:
- Babelscape/rebel-dataset
模型索引:
- 名称: REBEL
结果:
- 任务:
名称: 关系抽取
类型: 关系抽取
数据集:
名称: "CoNLL04"
类型: CoNLL04
指标:
- 名称: RE+ 宏平均F1
类型: re+ 宏平均f1
值: 76.65
- 任务:
名称: 关系抽取
类型: 关系抽取
数据集:
名称: "NYT"
类型: NYT
指标:
- 名称: F1
类型: f1
值: 93.4
许可证: cc-by-nc-sa-4.0

多语言更新!查看 mREBEL,一个涵盖更多关系类型、语言并包含实体类型的多语言版本。
REBEL
: 通过端到端语言生成进行关系抽取
这是EMNLP 2021论文REBEL: 通过端到端语言生成进行关系抽取的模型卡片。我们提出了一种新的线性化方法,并将关系抽取重新定义为序列到序列任务。论文可以在此处找到这里。如果您使用代码,请在您的论文中引用此工作:
@inproceedings{huguet-cabot-navigli-2021-rebel-relation,
title = "{REBEL}: 通过端到端语言生成进行关系抽取",
author = "Huguet Cabot, Pere-Llu{\'\i}s and
Navigli, Roberto",
booktitle = "计算语言学协会EMNLP 2021发现",
month = 十一月,
year = "2021",
address = "蓬塔卡纳, 多米尼加共和国",
publisher = "计算语言学协会",
url = "https://aclanthology.org/2021.findings-emnlp.204",
pages = "2370--2381",
abstract = "从原始文本中提取关系三元组是信息抽取中的关键任务,支持多种应用,如填充或验证知识库、事实核查和其他下游任务。然而,它通常涉及多步骤流程,这些流程会传播错误或仅限于少量关系类型。为了克服这些问题,我们提出了自回归序列到序列模型的使用。此类模型先前不仅在语言生成中表现良好,而且在如实体链接等NLU任务中,通过将其框架化为序列到序列任务,也表现良好。在本文中,我们展示了如何通过将三元组表示为文本序列来简化关系抽取,并介绍了REBEL,这是一个基于BART的序列到序列模型,可以对200多种不同的关系类型进行端到端关系抽取。我们通过在各种关系抽取和关系分类基准上微调模型,展示了其灵活性,其在大多数基准上达到了最先进的性能。",
}
论文的原始存储库可以在此处找到这里
请注意,右侧的推理小部件不输出特殊标记,这些标记是区分主语、宾语和关系类型所必需的。有关REBEL及其预训练数据集的演示,请查看Spaces演示。
管道使用
from transformers import pipeline
triplet_extractor = pipeline('text2text-generation', model='Babelscape/rebel-large', tokenizer='Babelscape/rebel-large')
extracted_text = triplet_extractor.tokenizer.batch_decode([triplet_extractor("蓬塔卡纳是多米尼加共和国最东部的拉阿尔塔格拉西亚省伊圭市的一个度假小镇", return_tensors=True, return_text=False)[0]["generated_token_ids"]])
print(extracted_text[0])
def extract_triplets(text):
triplets = []
relation, subject, relation, object_ = '', '', '', ''
text = text.strip()
current = 'x'
for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").split():
if token == "<triplet>":
current = 't'
if relation != '':
triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
relation = ''
subject = ''
elif token == "<subj>":
current = 's'
if relation != '':
triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
object_ = ''
elif token == "<obj>":
current = 'o'
relation = ''
else:
if current == 't':
subject += ' ' + token
elif current == 's':
object_ += ' ' + token
elif current == 'o':
relation += ' ' + token
if subject != '' and relation != '' and object_ != '':
triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
return triplets
extracted_triplets = extract_triplets(extracted_text[0])
print(extracted_triplets)
使用transformers的模型和分词器
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
def extract_triplets(text):
triplets = []
relation, subject, relation, object_ = '', '', '', ''
text = text.strip()
current = 'x'
for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").split():
if token == "<triplet>":
current = 't'
if relation != '':
triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
relation = ''
subject = ''
elif token == "<subj>":
current = 's'
if relation != '':
triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
object_ = ''
elif token == "<obj>":
current = 'o'
relation = ''
else:
if current == 't':
subject += ' ' + token
elif current == 's':
object_ += ' ' + token
elif current == 'o':
relation += ' ' + token
if subject != '' and relation != '' and object_ != '':
triplets.append({'head': subject.strip(), 'type': relation.strip(),'tail': object_.strip()})
return triplets
tokenizer = AutoTokenizer.from_pretrained("Babelscape/rebel-large")
model = AutoModelForSeq2SeqLM.from_pretrained("Babelscape/rebel-large")
gen_kwargs = {
"max_length": 256,
"length_penalty": 0,
"num_beams": 3,
"num_return_sequences": 3,
}
text = '蓬塔卡纳是多米尼加共和国最东部的拉阿尔塔格拉西亚省伊圭市的一个度假小镇。'
model_inputs = tokenizer(text, max_length=256, padding=True, truncation=True, return_tensors = 'pt')
generated_tokens = model.generate(
model_inputs["input_ids"].to(model.device),
attention_mask=model_inputs["attention_mask"].to(model.device),
**gen_kwargs,
)
decoded_preds = tokenizer.batch_decode(generated_tokens, skip_special_tokens=False)
for idx, sentence in enumerate(decoded_preds):
print(f'预测三元组句子 {idx}')
print(extract_triplets(sentence))