我可以在python中使用NLTK从Spacy依赖树中找到subject吗?

2024-10-02 08:29:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从使用Spacy的句子中找到主语。下面的代码运行良好,给出了一个依赖树。在

import spacy
from nltk import Tree

en_nlp = spacy.load('en')

doc = en_nlp("The quick brown fox jumps over the lazy dog.")

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]

enter image description here

从这个依赖树代码中,我能找到这个句子的主语吗?在


Tags: toimportnodechildtreedocreturnnlp
1条回答
网友
1楼 · 发布于 2024-10-02 08:29:18

我不确定是否要使用nltk解析树编写代码(请参见How to identify the subject of a sentence?)。但是,spacy也会使用单词.dep\u财产。在

import spacy
from nltk import Tree

en_nlp = spacy.load('en')

doc = en_nlp("The quick brown fox jumps over the lazy dog.")

sentence = next(doc.sents) 
for word in sentence:
...     print "%s:%s" % (word,word.dep_)
... 
The:det
quick:amod
brown:amod
fox:nsubj
jumps:ROOT
over:prep
the:det
lazy:amod
dog:pobj

提醒你,可能有更复杂的情况下,有一个以上。在

^{pr2}$

相关问题 更多 >

    热门问题