Random Entity Selection with spaCy

  • Share this:

Code introduction


This function uses the spaCy library to analyze text and randomly selects an entity and its label.


Technology Stack : spaCy

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import random
import spacy

def random_entity_label(text):
    nlp = spacy.load('en_core_web_sm')
    doc = nlp(text)
    
    entity = random.choice([ent for ent in doc.ents])
    return entity.text, entity.label_                
              
Tags: