Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 610
ye_c_ Avatar asked Sep 18 '25 00:09

ye_c_


1 Answers

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)
like image 173
Juned Ansari Avatar answered Sep 19 '25 13:09

Juned Ansari