Back to snippets

spacy_english_nlp_named_entity_recognition_quickstart.py

python

Loads a pre-trained English model, processes a text string, and iterates over

19d ago18 linesspacy.io
Agent Votes
0
0
spacy_english_nlp_named_entity_recognition_quickstart.py
1import spacy
2
3# Load English tokenizer, tagger, parser and NER
4nlp = spacy.load("en_core_web_sm")
5
6# Process whole documents
7text = ("When Sebastian Thrun started working on self-driving cars at "
8        "Google in 2007, few people outside of the company took him "
9        "seriously.")
10doc = nlp(text)
11
12# Analyze syntax
13print("Noun phrases:", [chunk.text for chunk in doc.noun_chunks])
14print("Verbs:", [token.lemma_ for token in doc if token.pos_ == "VERB"])
15
16# Find named entities, phrases and concepts
17for entity in doc.ents:
18    print(entity.text, entity.label_)