Extracting Noun Phrases with spaCy

  • Share this:

Code introduction


This function uses the spaCy library to extract noun phrases from a given text. Noun phrases are typically phrases that are composed of a noun as the subject.


Technology Stack : spaCy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import spacy
from spacy.symbols import NOUN, VERB, ADJ

def extract_noun_phrases(text, nlp):
    doc = nlp(text)
    noun_phrases = [token.text for token in doc if token.pos_ == NOUN and token.dep_ == 'nsubj']
    return noun_phrases                
              
Tags: