You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将DeepPavlov NER标注结果转换为Python字典

DeepPavlov NER标注结果转字典解决方案

先明确语法限制

Python 字典的键必须为可哈希的不可变类型,你预期格式中用列表作为键是语法错误,无法直接实现,有两种替代方案:

  • 用原始文本字符串作为键(最常用)
  • 将分词后的列表转换为元组作为键(元组不可变,支持作为字典键)

报错原因说明

你之前的两种错误写法都是搞错了操作对象:不需要修改传入ner_model的输入参数,原有调用方式ner_model([d])是符合要求的,你只需要对模型返回的结果做解析整理即可:

  • 传入zip(d)报错:ner_model要求输入为字符串组成的列表,zip对象不符合输入格式,触发TypeError
  • 传入dict(d)报错:无法直接将单个字符串转换为字典,也不符合模型输入要求,触发ValueError

正确实现代码

from deeppavlov import configs, build_model
ner_model = build_model(configs.ner.ner_ontonotes_bert_mult, download=True)
data_list = ["New Zealand's Prime Minister Jacinda Ardern has announced a nationwide lockdown after the country confirmed one coronavirus case -- the first locally transmitted Covid-19 case in the community since February.", 
             "Finmark is now a remote-only company, with 33 employees across the United States, England and Pakistan.", 
             "As he slipped through the kelp forest to the bottom of the Atlantic Ocean, Kamau Sadiki's eyes hooked onto something resembling the item he and fellow divers had been searching for."]

ner_result = {}
for text in data_list:
    # 调用模型获取结果,返回结构为 [分词结果嵌套列表, 标注结果嵌套列表]
    tokens_batch, tags_batch = ner_model([text])
    # 解包外层多余的列表维度,拿到单条样本的分词和标注
    tokens = tokens_batch[0]
    tags = tags_batch[0]
    # 方案1:用原始文本作为键
    ner_result[text] = tags
    # 方案2:用分词元组作为键,需要用的时候转列表即可 list(key)
    # ner_result[tuple(tokens)] = tags

输出示例(方案1)

{
    "Finmark is now a remote-only company, with 33 employees across the United States, England and Pakistan.": ['B-ORG', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-CARDINAL', 'O', 'O', 'B-GPE', 'I-GPE', 'I-GPE', 'O', 'B-GPE', 'O', 'B-GPE', 'O'],
    # 其余两条文本的结果省略
}

内容的提问来源于stack exchange,提问作者alyferryhalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 05:27:03