spaCy 用一个词性标记每个Tokens中的每个s Document(以两种不同的格式,一种存储在posand和pos_属性中Token,另一种存储在tag和tag_属性中)以及对其.head标记的语法依赖(存储在dep和dep_属性中) ).
其中一些标签是不言自明的,即使是像我这样没有语言学背景的人:
>>> import spacy
>>> en_nlp = spacy.load('en')
>>> document = en_nlp("I shot a man in Reno just to watch him die.")
>>> document[1]
shot
>>> document[1].pos_
'VERB'
Run Code Online (Sandbox Code Playgroud)
其他......不是:
>>> document[1].tag_
'VBD'
>>> document[2].pos_
'DET'
>>> document[3].dep_
'dobj'
Run Code Online (Sandbox Code Playgroud)
更糟糕的是,官方文档甚至不包含大多数这些属性的可能标记列表,也不包含其中任何属性的含义.他们有时会提到他们使用的标记化标准,但这些声明目前还不完全准确,而且最重要的是标准很难追踪.
什么是可能的值tag_,pos_和dep_性质,以及它们意味着什么?
我有句子约翰在商店看到一个华丽的帽子
如何将其表示为依赖树,如下所示?
(S
(NP (NNP John))
(VP
(VBD saw)
(NP (DT a) (JJ flashy) (NN hat))
(PP (IN at) (NP (DT the) (NN store)))))
Run Code Online (Sandbox Code Playgroud)
我从这里得到了这个脚本
import spacy
from nltk import Tree
en_nlp = spacy.load('en')
doc = en_nlp("John saw a flashy hat at the store")
def to_nltk_tree(node):
if node.n_lefts + node.n_rights > 0:
return Tree(node.orth_, [to_nltk_tree(child) for child in node.children])
else:
return node.orth_
[to_nltk_tree(sent.root).pretty_print() for sent in doc.sents]
Run Code Online (Sandbox Code Playgroud)
我得到以下,但我正在寻找树(NLTK)格式.
saw
____|_______________
| | at
| | |
| …Run Code Online (Sandbox Code Playgroud)