spaCy Cheat Sheet
spaCy reference covering pretrained pipelines, tokenization, part-of-speech tagging, dependency parsing, named entity recognition, and similarity scoring.
Pipeline Basics
Load a pipeline and process text.
import spacynlp = spacy.load("en_core_web_sm") # python -m spacy download en_core_web_smdoc = nlp("Apple is looking at buying a UK startup for $1 billion.")for token in doc: print(token.text, token.pos_, token.dep_, token.lemma_)
Entities & Noun Chunks
Extract named entities and noun phrases.
for ent in doc.ents: print(ent.text, ent.label_) # e.g. "Apple" ORG, "UK" GPE, "$1 billion" MONEYfor chunk in doc.noun_chunks: print(chunk.text, chunk.root.text)from spacy import displacydisplacy.render(doc, style="ent", jupyter=True)
Similarity & Custom Components
Compare documents and extend the pipeline.
doc1 = nlp("I like cats")doc2 = nlp("I like dogs")print(doc1.similarity(doc2)) # requires vectors (md/lg models)@spacy.Language.component("custom_component")def custom_component(doc): print("Doc length:", len(doc)) return docnlp.add_pipe("custom_component", last=True)
Pipeline Components
Stages inside a spaCy nlp pipeline.
- tokenizer- splits text into Token objects
- tagger- assigns part-of-speech tags
- parser- dependency parsing and sentence boundaries
- ner- named entity recognition
- lemmatizer- reduces words to their base form
- textcat- text classification component
- en_core_web_sm/md/lg- small/medium/large pretrained English pipelines (md/lg include word vectors)
Rule-Based Matcher & PhraseMatcher
Match token patterns and exact phrase lists without training a model.
from spacy.matcher import Matcher, PhraseMatchermatcher = Matcher(nlp.vocab)pattern = [{"LOWER": "hello"}, {"IS_PUNCT": True, "OP": "?"}, {"LOWER": "world"}]matcher.add("HELLO_WORLD", [pattern])doc = nlp("Hello, world! Hello world")for match_id, start, end in matcher(doc): print(nlp.vocab.strings[match_id], doc[start:end].text)phrase_matcher = PhraseMatcher(nlp.vocab, attr="LOWER")phrase_matcher.add("FRUITS", [nlp.make_doc(t) for t in ["apple", "banana", "kiwi"]])
EntityRuler for Hybrid NER
Combine rule-based patterns with the statistical NER model.
ruler = nlp.add_pipe("entity_ruler", before="ner")patterns = [ {"label": "PRODUCT", "pattern": "iPhone 15"}, {"label": "ORG", "pattern": [{"LOWER": "open"}, {"LOWER": "ai"}]},]ruler.add_patterns(patterns)doc = nlp("OpenAI released the iPhone 15 review.")print([(ent.text, ent.label_) for ent in doc.ents]) # ruler runs before statistical NER, filling gaps it misses
Custom Extension Attributes
Attach custom data and computed properties to Doc, Span, and Token.
from spacy.tokens import Doc, Span, TokenDoc.set_extension("reading_time", getter=lambda doc: len(doc) / 200)Token.set_extension("is_tech_term", default=False)Span.set_extension("word_count", getter=lambda span: len(span))doc = nlp("spaCy makes NLP pipelines fast.")doc[0]._.is_tech_term = Trueprint(doc._.reading_time, doc[0]._.is_tech_term, doc[1:3]._.word_count)
DocBin Serialization
Efficiently serialize and reload batches of processed Docs.
from spacy.tokens import DocBindoc_bin = DocBin(attrs=["LEMMA", "ENT_IOB", "ENT_TYPE"], store_user_data=True)for doc in nlp.pipe(texts): doc_bin.add(doc)doc_bin.to_disk("./train.spacy")# reload without re-running the pipelineloaded_bin = DocBin().from_disk("./train.spacy")docs = list(loaded_bin.get_docs(nlp.vocab))
Training CLI & Config
Commands and objects behind spaCy's config-driven training workflow.
- spacy init config- generates a base config.cfg for training a pipeline
- spacy init fill-config- fills a partial config with default values
- spacy convert- converts annotation formats (e.g. CoNLL) into spaCy's binary .spacy format
- spacy train config.cfg --output ./output- runs training end-to-end, driven entirely by the config file
- spacy.training.Example- pairs a predicted Doc with gold-standard annotations for training and scoring
- spacy evaluate- scores a trained pipeline against labeled evaluation data
- spacy project- reproducible end-to-end workflow templates you can clone, run, and push
en_core_web_sm has no real word vectors (only context-dependent tensors), so Doc.similarity() scores are unreliable with it — install en_core_web_md or en_core_web_lg if you need meaningful similarity comparisons.