ye_c_
ye_c_

Reputation: 83

How to find whether a sentence contain a noun using spacy?

Currently doing a project in NLP. I need to find out whether a sentence have a noun in it. How can I achieve this using spacy?

Upvotes: 4

Views: 905

Answers (1)

Juned Ansari
Juned Ansari

Reputation: 5283

Solution 1:

import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp(u'hello india how are you?')
print(len([np.text for np in doc.noun_chunks])>0)

Solution 2:

import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp(u'hello india how are you?')
print(len([token.pos_ for token in doc if token.pos_=="NOUN"])>0)

Upvotes: 4

Related Questions