Lea*_*ple 5 nlp transformer-model tokenize ner huggingface-transformers
我一直在寻找将 Hugging Face 的 Pipelines 用于 NER(命名实体识别)。但是,它以内部-外部-开始 (IOB) 格式返回实体标签,但没有 IOB 标签。所以我无法将管道的输出映射回我的原始文本。此外,输出以 BERT 标记化格式进行屏蔽(默认模型为 BERT-large)。
例如:
from transformers import pipeline
nlp_bert_lg = pipeline('ner')
print(nlp_bert_lg('Hugging Face is a French company based in New York.'))
Run Code Online (Sandbox Code Playgroud)
输出是:
[{'word': 'Hu', 'score': 0.9968873858451843, 'entity': 'I-ORG'},
{'word': '##gging', 'score': 0.9329522848129272, 'entity': 'I-ORG'},
{'word': 'Face', 'score': 0.9781811237335205, 'entity': 'I-ORG'},
{'word': 'French', 'score': 0.9981815814971924, 'entity': 'I-MISC'},
{'word': 'New', 'score': 0.9987512826919556, 'entity': 'I-LOC'},
{'word': 'York', 'score': 0.9976728558540344, 'entity': 'I-LOC'}]
Run Code Online (Sandbox Code Playgroud)
如您所见,纽约分为两个标签。
如何将 Hugging Face 的 NER 管道映射回我的原始文本?
变形金刚版本:2.7
Fuc*_*cio 14
5 月 17 日,一个新的拉取请求https://github.com/huggingface/transformers/pull/3957与您所要求的内容已合并,因此现在我们的生活更轻松了,您可以在管道中使用它
ner = pipeline('ner', grouped_entities=True)
Run Code Online (Sandbox Code Playgroud)
并且您的输出将如预期的那样。目前你必须从 master 分支安装,因为还没有新版本。你可以通过
pip install git+git://github.com/huggingface/transformers.git@48c3a70b4eaedab1dd9ad49990cfaa4d6cb8f6a0
Run Code Online (Sandbox Code Playgroud)
小智 10
如果你在 2022 年看到这个:
grouped_entities关键字现已弃用aggregation_strategy:默认是None,你正在寻找simpleorfirst或averageor max-> 请参阅该类的AggregationStrategy文档from transformers import pipeline
import pandas as pd
text = 'Hugging Face is a French company based in New York.'
tagger = pipeline(task='ner', aggregation_strategy='simple')
named_ents = tagger(text)
pd.DataFrame(named_ents)
Run Code Online (Sandbox Code Playgroud)
[{'entity_group': 'ORG',
'score': 0.96934015,
'word': 'Hugging Face',
'start': 0,
'end': 12},
{'entity_group': 'MISC',
'score': 0.9981816,
'word': 'French',
'start': 18,
'end': 24},
{'entity_group': 'LOC',
'score': 0.9982121,
'word': 'New York',
'start': 42,
'end': 50}]
Run Code Online (Sandbox Code Playgroud)
不幸的是,截至目前(2.6 版,我认为即使是 2.7 版),pipeline仅凭该功能还无法做到这一点。由于__call__管道调用的函数只是返回一个列表,请参阅此处的代码。这意味着您必须使用“外部”标记器执行第二个标记化步骤,这完全违背了管道的目的。
但是,相反,您可以使用文档中发布的第二个示例,就在与您的示例类似的示例下方。为了将来的完整性,这里是代码:
from transformers import AutoModelForTokenClassification, AutoTokenizer
import torch
model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
label_list = [
"O", # Outside of a named entity
"B-MISC", # Beginning of a miscellaneous entity right after another miscellaneous entity
"I-MISC", # Miscellaneous entity
"B-PER", # Beginning of a person's name right after another person's name
"I-PER", # Person's name
"B-ORG", # Beginning of an organisation right after another organisation
"I-ORG", # Organisation
"B-LOC", # Beginning of a location right after another location
"I-LOC" # Location
]
sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very" \
"close to the Manhattan Bridge."
# Bit of a hack to get the tokens with the special tokens
tokens = tokenizer.tokenize(tokenizer.decode(tokenizer.encode(sequence)))
inputs = tokenizer.encode(sequence, return_tensors="pt")
outputs = model(inputs)[0]
predictions = torch.argmax(outputs, dim=2)
print([(token, label_list[prediction]) for token, prediction in zip(tokens, predictions[0].tolist())])
Run Code Online (Sandbox Code Playgroud)
这正是您正在寻找的东西。请注意,ConLL 注释方案在其原始论文中列出了以下内容:
每行包含四个字段:单词、词性标签、词块标签和命名实体标签。用 O 标记的词在命名实体之外,而 I-XXX 标记用于 XXX 类型命名实体内的词。每当 XXX 类型的两个实体紧挨在一起时,第二个实体的第一个单词将被标记为 B-XXX,以表明它开始另一个实体。数据包含四种类型的实体:人员 (PER)、组织 (ORG)、位置 (LOC) 和杂名 (MISC)。这种标记方案是最初由 Ramshaw 和 Marcus (1995) 提出的 IOB 方案。
意思是,如果您对(仍然拆分的)实体不满意,您可以连接所有后续I-标记实体,或B-后跟I-标记。在这个方案中,两个不同的(直接相邻的)实体都只用I-标签标记是不可能的。
| 归档时间: |
|
| 查看次数: |
4433 次 |
| 最近记录: |