How can I extract nouns excluding their articles and possessive cases?

Viewed 291

Background

I would like to know how to separate nouns and their decorations, such as articles and possessive cases.

Example

#sentence
The man with the star regarded her with his expressionless eyes.


# what to extract 
man
star
eyes

Problem

As the below image created using disPlacy tool, "The man, the star, and his expressionless eyes" are united as NOUN.

the visualizar of part of speech and dependencies

https://explosion.ai/demos/displacy

the result of the sample sentence

What I tried

I've run the sample code introduced on the spaCy page.

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("The man with the star regarded her with his calm, expressionless eyes.")
for token in doc:
    print(token.text, token.dep_, token.head.text, token.head.pos_,
            [child for child in token.children])

Using the below result or other ways, how can I extract nouns themselves excluding their articles and possessive case?

$ python sample.py
The det man NOUN []
man nsubj regarded VERB [The, with]
with prep man NOUN [star]
the det star NOUN []
star pobj with ADP [the]
regarded ROOT regarded VERB [man, her, with, .]
her dobj regarded VERB []
with prep regarded VERB [eyes]
his poss eyes NOUN []
calm amod eyes NOUN [,]
, punct calm ADJ []
expressionless amod eyes NOUN []
eyes pobj with ADP [his, calm, expressionless]
. punct regarded VERB []
1 Answers

Try something like this to achieve the desired output you provide in your initial example:

import spacy

nlp = spacy.load('en')

text = "The man with the star regarded her with his expressionless eyes."

for word in nlp(text):
  if word.pos_ == 'NOUN':
    print(word.text)

Output:

man
star
eyes

You could also consider using the nltk package instead, since it may be faster, for this use case:

import nltk

text = "The man with the star regarded her with his expressionless eyes."

for word, pos in nltk.pos_tag(nltk.word_tokenize(text)):
  if pos[0] == 'N':
    print(word)

Output:

man
star
eyes
Related