Mitchell Knight
Mitchell Knight

Reputation: 1

Can Spacy be used to find a sentence subject that's multiple words long?

I've recently started playing around with Python's Spacy library. I wanted to use it to find the subject of a sentence. I used the method detailed in this post, but it doesn't find the entire subject.

For Example:

doc = nlp(u"Mr Bob Bobson enjoyed hunting and fishing")
sub_toks = [tok for tok in doc if (tok.dep_ == "nsubj")]
print(sub_toks)

Results in

[Bobson]

However, the result I want is

[Mr Bob Bobson]

Upvotes: 0

Views: 1273

Answers (1)

Xeoncross
Xeoncross

Reputation: 57184

You want the tok.subtree API docs.

np = doc[tok.left_edge.i : tok.right_edge.i + 1]

Upvotes: 1

Related Questions