Extract entities from Multiple Subject passive sentence by Spacy

1.6k views Asked by At

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

There are 1 answers

0
utah05 On

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]