相关疑难解决方法(0)

如何在没有 IOB 标签的情况下使用 Hugging Face 的转换器管道重建文本实体?

我一直在寻找将 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

nlp transformer-model tokenize ner huggingface-transformers

5
推荐指数
3
解决办法
4433
查看次数

推理后如何将标记化的单词转换回原始单词?

我正在为已经训练好的 NER 模型编写推理脚本,但我在将编码标记(它们的 id)转换为原始单词时遇到了麻烦。

# example input
df = pd.DataFrame({'_id': [1], 'body': ['Amazon and Tesla are currently the best picks out there!']})

# calling method that handles inference:
ner_model = NER()
ner_model.recognize_from_df(df, 'body')

# here is only part of larger NER class that handles the inference:
def recognize_from_df(self, df: pd.DataFrame, input_col: str):
    predictions = []
    df = df[['_id', input_col]].copy()
    dataset = Dataset.from_pandas(df)
    # tokenization, padding, truncation:
    encoded_dataset = dataset.map(lambda examples: self.bert_tokenizer(examples[input_col], 
                                      padding='max_length', truncation=True, max_length=512), batched=True)
    encoded_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask'], device=device)
    dataloader …
Run Code Online (Sandbox Code Playgroud)

python pytorch huggingface-transformers huggingface-tokenizers huggingface-datasets

5
推荐指数
1
解决办法
1624
查看次数