Extracting Noun Phrases with spaCy

  • Share this:

Code introduction


This function uses the spaCy library to extract noun phrases from the given text. It first loads the English model, then processes the text for tokenization, part-of-speech tagging, syntactic parsing, etc., and finally extracts noun phrases from the analysis results.


Technology Stack : spaCy

Code Type : Function

Code Difficulty : Intermediate


                
                    
def extract_noun_phrases(text):
    """
    This function uses spaCy's NLP capabilities to extract noun phrases from a given text.

    :param text: str - the text from which noun phrases are to be extracted
    :return: list - a list of noun phrases extracted from the text
    """
    import spacy

    # Load the English tokenizer, tagger, parser, NER, and word vectors
    nlp = spacy.load('en_core_web_sm')

    # Process the text
    doc = nlp(text)

    # Extract noun phrases
    noun_phrases = [chunk.text for chunk in doc.noun_chunks]

    return noun_phrases                
              
Tags: