Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subject object identification in python

Tags:

python

spacy

I want to identify subject and objects of a set of sentences . My actual work is to identify cause and effect from a set of review data.

I am using Spacy Package to chunk and parse data. But not actually reaching my goal. Is there any way to do so?

E.g.:

 I thought it was the complete set

out:

subject  object
I        complete set
like image 204
S.De Avatar asked Aug 30 '25 18:08

S.De


2 Answers

In the simplest way. The dependencies are accessed by token.dep_ Having imported spacy:

import spacy
nlp = spacy.load('en')
parsed_text = nlp(u"I thought it was the complete set")

#get token dependencies
for text in parsed_text:
    #subject would be
    if text.dep_ == "nsubj":
        subject = text.orth_
    #iobj for indirect object
    if text.dep_ == "iobj":
        indirect_object = text.orth_
    #dobj for direct object
    if text.dep_ == "dobj":
        direct_object = text.orth_

print(subject)
print(direct_object)
print(indirect_object)
like image 196
Simian Auxiliary Avatar answered Sep 02 '25 07:09

Simian Auxiliary


You can use noun chunk.

Code

doc = nlp("I thought it was the complete set")
for nc in doc.noun_chunks:
    print(nc.text)

Result:

I
it
the complete set

To pick only "I" instead of both "I" and "it", you can write a test first to take the nsubj left of ROOT.

like image 34
Sohel Khan Avatar answered Sep 02 '25 08:09

Sohel Khan