如何将空间命名实体链接到嵌套字典中的文本?

2024-06-26 17:50:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含文本的词典列表:

list_dicts = [{'id': 1, 'text': 'hello my name is Carla'}, {'id': 2, 'text': 'hello my name is John' }]

我在嵌套文本上应用了空间命名实体识别,如下所示:

for d in list_dicts: 
    for k,v in d.items():
        if k=='text':
            doc = nlp(v) 
            for ent in doc.ents:
                print([ent.text, ent.label_]) 

输出是命名实体文本及其相应标签的打印输出,例如:

    ['Bob', 'PERSON']
    ['John', 'PERSON']

我想将命名实体添加到每个嵌套字典中对应的文本中,如下所示:

list_dicts = [{'id': 1, 'text': 'hello our names are Carla and Bob', 'entities':[['Carla', 'PERSON'], ['Bob':'PERSON']]}, {'id': 2, 'text': 'hello my name is John', 'entities': [['John', 'PERSON']] }]

现在,我尝试将zip()实现为将实体链接到原始文本的方法,然后将其转换为新的字典列表,但似乎zip()不适用于Spacy对象


Tags: textname文本实体idhelloforis