location - 给定一段文本标记位置文本
问题描述
我想看看是否有一个方便的python库可以接受一个字符串并返回代表一个位置的单词索引?
例如,输入文本“New York is a state in United States”
返回令牌
美国纽约
或它们各自在文本中的索引。
我想用它在我的网站中自动突出显示文本中的位置。
谢谢,拉利斯
解决方案
SpaCy 是用于此任务的库。
您应该安装en_core_web_lg
(最大的英文模型)以获得最佳的命名实体识别 (NER) 准确性。
然后运行以下代码来收集位置实体。
import spacy
nlp = spacy.load('en_core_web_lg')
text = "New York is a state in United States"
doc = nlp(text)
# GPE = Countries, cities, states, LOC = Non-GPE locations, mountain ranges, bodies of water
locations = [ent for ent in doc.ents if ent.label_ in ['GPE', 'LOC']]
print(locations)
输出:
[New York, United States]
推荐阅读
- clickhouse - 在 clickhouse 中枢轴
- java - 如何在android studio中使用液体滑动?
- javascript - 在 Spark AR Studio 中有延迟的人脸跟踪器的父对象?
- html - html 两个相邻且高度相等的 span 元素
- autodesk - Model Derivative API:桶的范围是什么?
- visual-studio - Xamarin Visual Studio 模拟器问题
- c# - SELECT 语句在 INSERT 提交事务后第一次不会返回行
- python - 使用 plot() 函数时 matplotlib 中的 RGBA 颜色“ro”无效
- kubernetes - 当 pod 或服务或端点发生变化时如何获得通知?
- html - 使用 jQuery 脚本专注于 Enter 键上的下一个输入