从 Spacy 中提取实体并放入新的数据框

Extracting the entities from Spacy and putting in new dataframe

所以我按照这个问题的答案 (Extract entity from dataframe using spacy) 解决了我能够在 DF 上迭代的问题。

我面临的问题是尝试获取这些结果,从原始 df 添加一列,然后将所有这些放入新的 df 中。我想要来自原始 df 的 DOI,来自 NER 的实体文本和实体标签。

要抓取并放入列表的代码:

entities=[]
nlp = spacy.load("en_ner_bionlp13cg_md")
for i in df['Abstract'].tolist():
    doc = nlp(i)
    for entity in doc.ents:
        entities.append((df.DOI, entity.text , entity.label_))

然后我将实体列表输入新的 df:

df_ner = pd.DataFrame.from_records(entities, columns =['DOI', 'ent_name', 'ent_type'])

不幸的是,只有第一条记录被加载到 df 中。我错过了什么?

DOI ent_name    ent_type
0   3 10.7501/j.issn.0253-2670.2020.... COVID-19    GENE_OR_GENE_PRODUCT
1   3 10.7501/j.issn.0253-2670.2020.... ACE2    GENE_OR_GENE_PRODUCT
2   3 10.7501/j.issn.0253-2670.2020.... angiotensin converting enzyme II    GENE_OR_GENE_PRODUCT
3   3 10.7501/j.issn.0253-2670.2020.... ACE2    GENE_OR_GENE_PRODUCT
4   3 10.7501/j.issn.0253-2670.2020.... UniProt GENE_OR_GENE_PRODUCT

这有效:

getattr(tqdm, '_instances', {}).clear()  # ⬅ clear the progress
spacy.prefer_gpu()
nlp = spacy.load("en_ner_bionlp13cg_md")
entities=[]
uuid = str(df.uuid)
for i in tqdm(df['Abstract'].tolist(),bar_format="{l_bar}%s{bar}%s{r_bar}" % (Fore.GREEN, Fore.RESET)):
    doc = nlp(i)
    for entity in doc.ents:
        entities.append((i, entity.text, entity.label_, uuid))