【问题标题】:Can I find subject from Spacy Dependency tree using NLTK in python?我可以在 python 中使用 NLTK 从 Spacy 依赖树中找到主题吗?
【发布时间】:2020-07-29 19:38:43
【问题描述】:

我想从使用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]

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

【问题讨论】:

  • this 有帮助吗?
  • @mbatchkarov 您建议的链接位于Stanford Corenlp。但我想要NLTK

标签: python nlp spacy


【解决方案1】:

我不确定您是否想使用 nltk 解析树编写代码(请参阅 How to identify the subject of a sentence? )。但是,spacy 也使用 word.dep_ 属性的“nsubj”标签生成它。

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

提醒,可能存在多个更复杂的情况。

>>> doc2 = en_nlp(u'When we study hard, we usually do well.')
>>> sentence2 = next(doc2.sents)
>>> for word in sentence2:
...     print "%s:%s" %(word,word.dep_)
... 
When:advmod
we:nsubj
study:advcl
hard:advmod
,:punct
we:nsubj
usually:advmod
do:ROOT
well:advmod
.:punct

【讨论】:

  • +1 以获得低声誉分数的真棒答案。(新成员。:))+ 接受。我现在可以玩树了。
【解决方案2】:

与 Leavesof3 一样,我更喜欢将 spaCy 用于这种目的。它具有更好的可视化,即

主题将是具有依赖属性“nsubj”或“普通主题”的单词或短语(如果您使用名词分块)

You can access displaCy (spaCy visualization) demo here

【讨论】:

    【解决方案3】:

    试试这个:

    import spacy
    import en_core_web_sm
    nlp = spacy.load('en_core_web_sm')
    sent = "I need to be able to log into the Equitable siteI tried my username and password from the AXA Equitable site which worked fine yesterday but it won't allow me to log in and when I try to change my password it says my answer is incorrect for the secret question I just need to be able to log into the Equitable site"
    nlp_doc=nlp(sent)
    subject = [tok for tok in nlp_doc if (tok.dep_ == "nsubj") ]
    print(subject)
    

    【讨论】:

    • 考虑一下“曼彻斯特城在一场没有令人失望的票房冲突中落后于利物浦,赢得一分”这句话。还有那个句子中的动词“earn”:有 2 个标记有 tok.dep_ == "nsubj" 但只有一个是 Earn 的主语。
    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2016-08-05
    • 2018-08-23
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2017-06-12
    相关资源
    最近更新 更多