Extract entities from Multiple Subject passive sentence by Spacy

坚强是说给别人听的谎言 提交于 2020-06-27 04:33:20

问题


Using Python Spacy, I am trying to extract entities from multiple subject passive voice sentence.

Sentence = "John and Jenny were accused of crimes by David"

My intention is to extract both "John and Jenny” from the sentence as nsubjpass and .ent_.

However, I am only able to extract “John” as nsubjpass.

How to extract both them?

Notice that while John is found as an entity in .ents, Jenny is considered as conj instead of nsubjpass. How to improve it?

code

each_sentence3 =  "John and Jenny were accused of crimes by David"
doc=nlp(each_sentence3)

passive_toks=[tok for tok in doc if (tok.dep_ == "nsubjpass") ]
if passive_toks != []:
    print(passive_toks)

Result:

[John]

The entity List shows:

code

`

print(list(doc.ents)

Result

[John, Jenny, David]

Now if we examine the whole sentence, we see as follows:

Code:

for tok in doc:   
        print(tok, tok.dep_)

Result

John nsubjpass
and cc
Jenny conj
were auxpass
accused ROOT
of prep
crimes pobj
by agent
David pobj

Notice that the second passive subject Jenny is identified as conj in Spacy instead of nsubjpass.


回答1:


Here is a sample of using POS tag and dependency parsing to extract subject and all of its conjunctions.

There is also a Token.conjuncts property but it can only get direct conjunction to the token. See https://github.com/explosion/spaCy/issues/795

each_sentence3 = "John and Jenny were accused of crimes by David"
sent = nlp(each_sentence3)

result = []
subj = None
for word in sent:
    if 'subj' in word.dep_:
        subj = word
        result.append(word)
    elif word.dep_ == 'conj' and word.head == subj:
        result.append(word)
print str(result)


[John, Jenny]


来源:https://stackoverflow.com/questions/41208346/extract-entities-from-multiple-subject-passive-sentence-by-spacy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!