Search code examples
pythonnlpspacydependency-parsing

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?


Solution

  • 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)