As a best-selling author, I invite you to explore my books on Amazon. Don't forget to follow me on Medium and show your support. Thank you! Your support means the world!
When I first started working with natural language processing, I was immediately drawn to spaCy for its speed and simplicity. Over the years, I've used it in countless projects, from building chatbots to analyzing customer feedback. What makes spaCy stand out is its balanced approach—it's powerful enough for production systems yet accessible for beginners. In this article, I'll share eight techniques that have been instrumental in my NLP work, complete with code examples and insights from my experience.
Tokenization is often the first step in any NLP pipeline. It breaks down text into individual units like words, punctuation, and symbols. I remember working on a project where accurate tokenization was crucial for processing legal documents. spaCy handles this seamlessly, even dealing with tricky cases like contractions and hyphenated words. The Doc object it creates stores all the token information, making it easy to access later.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Let's meet at 3 p.m. for a quick chat—don't be late!")
for token in doc:
    print(f"Text: {token.text}, Lemma: {token.lemma_}, POS: {token.pos_}, Alpha: {token.is_alpha}, Stop: {token.is_stop}")
Part-of-speech tagging assigns grammatical labels to each token, such as noun, verb, or adjective. This was a game-changer when I built a grammar checker tool. By understanding the role of each word, I could identify errors and suggest corrections. spaCy's models are trained on diverse datasets, so they handle various writing styles well.
doc = nlp("She quickly ran to the store and bought fresh bread.")
for token in doc:
    print(f"{token.text:<8} {token.pos_:<10} {token.tag_:<10} Description: {spacy.explain(token.tag_)}")
Named entity recognition identifies and categorizes proper nouns and specific terms. In a recent project analyzing news articles, I used NER to extract companies, dates, and locations. This helped in tracking trends over time. spaCy's pre-trained models recognize a wide range of entities, and you can fine-tune them for specialized domains.
doc = nlp("Microsoft announced a new AI tool in Seattle last January, valued at $500 million.")
for ent in doc.ents:
    print(f"Entity: {ent.text}, Label: {ent.label_}, Meaning: {spacy.explain(ent.label_)}")
Dependency parsing reveals the grammatical structure of a sentence by showing how words relate to each other. I used this in a sentiment analysis system to understand the subject and object of opinions. The parse tree makes it clear who is doing what to whom, which is vital for accurate interpretation.
doc = nlp("The diligent student completed the challenging assignment before the deadline.")
for token in doc:
    children_text = [child.text for child in token.children]
    print(f"Token: {token.text:<10} Dependency: {token.dep_:<12} Head: {token.head.text:<8} Children: {children_text}")
Text classification allows you to categorize entire documents into labels like positive/negative sentiment or topic categories. I trained a custom classifier for a client to sort support tickets automatically. spaCy's textcat component is efficient and integrates smoothly into the pipeline.
from spacy.training import Example
import random
TRAIN_DATA = [
    ("This product exceeded my expectations", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
    ("I'm disappointed with the service", {"cats": {"POSITIVE": 0.0, "NEGATIVE": 1.0}}),
    ("The delivery was fast and reliable", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
    ("Poor quality and bad customer support", {"cats": {"POSITIVE": 0.0, "NEGATIVE": 1.0}})
]
if "textcat" not in nlp.pipe_names:
    textcat = nlp.add_pipe("textcat", last=True)
    textcat.add_label("POSITIVE")
    textcat.add_label("NEGATIVE")
optimizer = nlp.begin_training()
for epoch in range(10):
    random.shuffle(TRAIN_DATA)
    losses = {}
    for text, annotations in TRAIN_DATA:
        example = Example.from_dict(nlp.make_doc(text), annotations)
        nlp.update([example], losses=losses)
    print(f"Epoch {epoch}, Losses: {losses}")
test_doc = nlp("I love this amazing product")
print(test_doc.cats)
Similarity comparison measures how alike two pieces of text are, based on their vector representations. I used this in a recommendation system to suggest similar articles to users. spaCy's similarity method uses word vectors, so it captures semantic meaning rather than just word overlap.
doc1 = nlp("I enjoy reading science fiction")
doc2 = nlp("I love sci-fi books")
doc3 = nlp("The weather is sunny today")
print(f"Similarity between doc1 and doc2: {doc1.similarity(doc2):.3f}")
print(f"Similarity between doc1 and doc3: {doc1.similarity(doc3):.3f}")
Custom pipeline components let you add your own processing steps to the spaCy pipeline. I once built a component to handle domain-specific abbreviations in medical texts. By creating a custom function, I could modify the tokens before further processing.
from spacy.language import Language
@Language.component("abbreviation_expander")
def expand_abbreviations(doc):
    abbreviation_map = {"Dr.": "Doctor", "approx.": "approximately"}
    for token in doc:
        if token.text in abbreviation_map:
            token.lemma_ = abbreviation_map[token.text]
    return doc
nlp.add_pipe("abbreviation_expander", after="tagger")
doc = nlp("Please consult Dr. Smith for approx. two weeks")
print([token.lemma_ for token in doc])
Training custom models is essential when working with specialized vocabularies or languages. I trained a model for a project in the financial sector to improve entity recognition for stock tickers and financial terms. spaCy's training config system makes it straightforward to define and optimize your model.
# Example of preparing data and starting training
import spacy
from spacy.tokens import DocBin
import random
# Sample training data for a new entity type
TRAIN_DATA = [
    ("Apple stock rose by 5%", {"entities": [(0, 5, "STOCK")]}),
    ("Buy shares of Tesla now", {"entities": [(13, 18, "STOCK")]})
]
nlp = spacy.blank("en")
doc_bin = DocBin()
for text, annotations in TRAIN_DATA:
    doc = nlp.make_doc(text)
    entities = annotations["entities"]
    doc.ents = [doc.char_span(start, end, label=label) for start, end, label in entities]
    doc_bin.add(doc)
doc_bin.to_disk("./train.spacy")
# Typically, you'd run training via CLI, but here's a simplified version
# python -m spacy train config.cfg --output ./model --paths.train ./train.spacy --paths.dev ./dev.spacy
In my experience, choosing the right technique depends on the specific problem. For instance, if you're building a search engine, similarity and NER might be more important than dependency parsing. spaCy's modular design lets you pick and choose components, so you don't waste resources on unnecessary steps.
I often advise starting with the pre-trained models and then customizing as needed. The small model is great for prototyping, while the large model offers higher accuracy for production. Remember to consider computational limits—some techniques, like training custom models, require more memory and time.
When I work on multilingual projects, I appreciate that spaCy supports multiple languages out of the box. However, for low-resource languages, training from scratch might be necessary. The community around spaCy is vibrant, with plenty of resources and extensions to help you along the way.
Finally, testing your pipeline with real-world data is crucial. I've seen many projects fail because the models were only tested on clean, ideal text. Always include edge cases and noisy data in your evaluations to ensure robustness.
These techniques have served me well across various applications, from academic research to industrial systems. By mastering them, you can build sophisticated NLP solutions that handle the complexities of human language. I encourage you to experiment with the code examples and adapt them to your own projects. The best way to learn is by doing, and spaCy makes that process enjoyable and efficient.
📘 Checkout my latest ebook for free on my channel!
Be sure to like, share, comment, and subscribe to the channel!
101 Books
101 Books is an AI-driven publishing company co-founded by author Aarav Joshi. By leveraging advanced AI technology, we keep our publishing costs incredibly low—some books are priced as low as $4—making quality knowledge accessible to everyone.
Check out our book Golang Clean Code available on Amazon.
Stay tuned for updates and exciting news. When shopping for books, search for Aarav Joshi to find more of our titles. Use the provided link to enjoy special discounts!
Our Creations
Be sure to check out our creations:
Investor Central | Investor Central Spanish | Investor Central German | Smart Living | Epochs & Echoes | Puzzling Mysteries | Hindutva | Elite Dev | Java Elite Dev | Golang Elite Dev | Python Elite Dev | JS Elite Dev | JS Schools
We are on Medium
Tech Koala Insights | Epochs & Echoes World | Investor Central Medium | Puzzling Mysteries Medium | Science & Epochs Medium | Modern Hindutva
              
    
Top comments (0)