Skip to content
  • spaCy Basics · Data Science

spaCy Basics

LearnViaNotes @learnvianotes· 1 min read

spaCy is an industrial-strength NLP library: one call runs tokenization, part-of-speech tagging, sentence splitting, lemmatization and named-entity recognition.

Setup
pip install spacy
python -m spacy download en_core_web_sm

Tokens

Python
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The cost of Iphone in U.K is 699$")
print([t.text for t in doc])            # ['The', 'cost', 'of', 'Iphone', 'in', 'U.K', 'is', '699', '$']

Parts of speech & lemmas

Python
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The cost of Iphone in U.K is 699$")
print([(t.text, t.pos_) for t in doc][:4])   # [('The', 'DET'), ('cost', 'NOUN'), ('of', 'ADP'), ('Iphone', 'PROPN')]
print([t.lemma_ for t in nlp("She was running faster")])   # ['she', 'be', 'run', 'fast']
TagMeaningTagMeaning
NOUNNounVERBVerb
PROPNProper nounAUXAuxiliary (is, was)
ADJAdjectiveADPPreposition
DETDeterminer (the, a)NUMNumber

Sentences & entities

Python
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("This is the first sentence. Please check it. Let's study now")
print([s.text for s in doc.sents])     # ['This is the first sentence.', 'Please check it.', "Let's study now"]
ents = nlp("Sundar Pichai joined Google in 2004 in California.").ents
print([(e.text, e.label_) for e in ents])   # [('Sundar Pichai', 'PERSON'), ('Google', 'ORG'), ('2004', 'DATE'), ('California', 'GPE')]

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report