python - 如何在字典中保存空间渲染标签
问题描述
https://spacy.io/usage/visualizers#ent
我正在尝试使用 spaCy 将句子中的实体可视化。在上面的链接中,您可以看到一个示例。
现在我的问题。如何将这些实体保存在字典中?
我想分析 100 个句子并保存这些实体的频率,以查看哪些术语最常见。
例如: dict = {"PERSON": 23, "ORG": 2, "LOC": 19}
有人可以帮忙吗?
解决方案
您可以将实体标签保存到频率字典中。
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}
推荐阅读
- python - 为什么腌制和加载非空列表会改变其大小?
- aem - 如何在 AEM 6.4 中的 MSM 中的特定目标页面上使用 Rollout Config Action Class 放弃推出
- apache - Apache 作为反向代理和负载均衡器需要使用粘性会话
- vb.net - 在所需位置的 ImageBox 上绘图
- c - 超过4个参数的“snprintf”有什么用?
- java - 矩阵上的 Dijkstra 算法
- typescript - 如何使用打字稿模块扩充修补类构造函数?
- docker - Nginx 代理管理器(NGINX 反向代理)可以连接到 Cloudflare Argo 隧道吗?
- mongodb - Mongoose如何将元素推送到数组数组的模式
- c# - c# 将字符串解析为日期。'yyyyMMddHHmmss.ffffff+zzz'