使用 Spacy 进行标记化 - 如何将标记放在标记的左侧/右侧

数据挖掘 Python nlp
2022-03-03 21:30:24

我正在使用 Spacy 进行文本标记化并陷入困境:

import spacy
nlp = spacy.load("en_core_web_sm")
mytext = "This is some sentence that spacy will not appreciate"
doc = nlp(mytext)

for token in doc:
    print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, token.is_alpha, token.is_stop)

返回一些在我看来是成功的标记化:

This this DET DT nsubj Xxxx True False 
is be VERB VBZ ROOT xx True True 
some some DET DT det xxxx True True 
sentence sentence NOUN NN attr xxxx True False 
that that ADP IN mark xxxx True True 
spacy spacy NOUN NN nsubj xxxx True False 
will will VERB MD aux xxxx True True 
not not ADV RB neg xxx True True 
appreciate appreciate VERB VB ccomp xxxx True False

但另一方面

[token.text for token in doc[2].lefts]

返回一个空列表。左/右有错误吗?

自然语言处理初学者,希望我没有落入概念陷阱。使用 Spacy v'2.0.4'。

1个回答

token.lefts和属性在句法依赖解析token.rights返回单词的直接子代的生成器它不仅返回给定令牌左右两侧的令牌。

见:https ://spacy.io/api/token#rights

如果您想要文档的相邻标记,您可以执行以下操作:

for i in range(len(doc))[1:-1]:
    print(doc[i-1], doc[i+1])

它将打印文档中所有标记的相邻标记,从第二个标记开始,到倒数第二个标记结束。