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?
Asked
Active
Viewed 488 times
1 Answers
4
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)

Juned Ansari
- 5,035
- 7
- 56
- 89
-
The first solution gives `True` but the second solution gives `False`. The following code fixes it. "India" gets evaluated as a proper noun, so the boolean logic needs to include either. `print(len([token.pos_ for token in doc if token.pos_=="NOUN" or "PROPN"])>0)` – datajoel Oct 10 '22 at 12:44