python - 如何在 spacy 中获得合取的跨度?
问题描述
我使用 spacytoken.conjuncts
来获取每个标记的合取。
但是,返回类型token.conjuncts
是tuple
,但我想获取span
类型,例如:
import spacy
nlp = spacy.load("en_core_web_lg")
sentence = "I like to eat food at the lunch time, or even at the time between a lunch and a dinner"
doc = nlp(sentence)
for token in doc:
conj = token.conjuncts
print(conj)
#output: <class 'tuple'>
有谁知道如何将其转换tuple
为span
类型?
或者也许我怎样才能直接获得span
连词的类型?
我需要span
type 的原因是,我想使用conjuncts (span)
来定位这个连词的位置,例如,这个连词属于哪个名词块或一个拆分(无论我用什么方式拆分它们)。
目前,我将tuple
to转换str
为迭代所有拆分或名词块以搜索拆分/名词块是否包含 this conjunct
。
但是,存在一个错误,例如,当一个conjunct
(令牌的)出现在多个拆分/名词块中时,定位包含它的确切拆分将是一个问题conjunct
。因为我只考虑str
而不考虑index
orid
的conjunct
。如果我能拥有span
这个conjunct
,那么我就可以定位到这个的确切位置conjunct
。
请随时发表评论,在此先感谢!
解决方案
token.conjuncts
返回一个令牌元组。要获得跨度,请致电doc[conj.i: conj.i+1]
import spacy
nlp = spacy.load('en_core_web_sm')
sentence = "I like oranges and apples and lemons."
doc = nlp(sentence)
for token in doc:
if token.conjuncts:
conjuncts = token.conjuncts # tuple of conjuncts
print("Conjuncts for ", token.text)
for conj in conjuncts:
# conj is type of Token
span = doc[conj.i: conj.i+1] # Here's span
print(span.text, type(span))
推荐阅读
- bash - 在 Bash 中的脚本输出中添加标题行(仅当脚本生成输出时)
- python - Tensorflow 2.3.0 - 警告:get_next_as_optional(来自 tensorflow.python.data.ops.iterator_ops)已弃用,将在未来版本中删除
- vba - 在 Solidworks VBA 中创建一条线
- python - 向玩家发牌并使用 player:hand 更新字典
- python-3.x - 有没有办法解压缩嵌套冗余列表的列表?
- python - 检查两个列表中是否有任何共同元素:Python
- c# - 使用 Linq 从同名中选择自定义 ID
- python - Python 出错:TypeError:fit() 缺少 1 个必需的位置参数:'y'
- javascript - updating components when new params are passed to them (VUE JS)
- angular - 有角度的多个流,一个可观察的