Search code examples
pythonnlpspacy

Spacy for Python not returning tokens


I am trying to run a simple code using SPACY nlp but not getting anything back for labels. Please help? My code is blow. I am not getting any output from print statements.

from spacy.lang.en import English
nlp = English(entity=True)
doc = nlp('John Smith loves coding in Python!')
for ent in doc.ents:
    print(ent.label_)
print([token for token in sent if token.ent_type_ == 'PERSON'])

Solution

  • As @WiktorStribiżew said in comment - code works for me when I use

    nlp = spacy.load('en_core_web_sm')
    

    I also had to download file

    python -m spacy download en_core_web_sm
    

    You may also download it in code

    spacy.cli.download('en_core_web_sm')
    

    but because you need to download it only once so downloading it in code is waste of time.


    BTW: It should be doc instead of send


    import spacy
    
    nlp = spacy.load("en_core_web_sm")
    
    doc = nlp('John Smith loves coding in Python!')
    
    for ent in doc.ents:
        print(ent.label_)
        
    print([token for token in doc if token.ent_type_ == 'PERSON'])
    

    Frankly, similar code you can see even on scapy web page