首页 > 解决方案 > 如何在字典中保存空间渲染标签

问题描述

https://spacy.io/usage/visualizers#ent

我正在尝试使用 spaCy 将句子中的实体可视化。在上面的链接中,您可以看到一个示例。

现在我的问题。如何将这些实体保存在字典中?

我想分析 100 个句子并保存这些实体的频率,以查看哪些术语最常见。

例如: dict = {"PERSON": 23, "ORG": 2, "LOC": 19}

有人可以帮忙吗?

标签: pythonnlpspacy

解决方案


您可以将实体标签保存到频率字典中。

import spacy

nlp = spacy.load("en_core_web_lg")
text = "Apple is looking at buying U.K. startup for $1 billion"
doc = nlp(text)
ent_labels = [e.label_ for e in doc.ents]
freq = dict()
for l in ent_labels:
    freq[l] = ent_labels.count(l)
print(freq)

输出:

{'ORG': 1, 'GPE': 1, 'MONEY': 1}

推荐阅读