liveBook · Manning

spaCy

As explained in Generative AI in Action

spaCy is an open-source Python library for efficient natural-language processing, including linguistic annotation and text segmentation.

spaCy is a Python NLP library used for context-aware text segmentation in retrieval-augmented generation. It can split documents at linguistic boundaries such as sentences, clauses, phrases, or entities. section 7.7.5

A spaCy-based chunking pipeline can:

  • Load a language model such as en_core_web_sm and process text into a document. section 7.7.5
  • Extract sentence boundaries from doc.sents and count tokens for each sentence. section 7.7.5
  • Assemble chunks under a maximum token limit while preserving configurable sentence overlap. section 7.7.5
Listing 7.5 Sentence chunking using spaCy
def split_sentences_by_spacy(text, max_tokens, overlap=0, model="en_core_web_sm"):
    nlp = spacy.load(model)    #1

    doc = nlp(text)            #2
    sentences = [sent.text for sent in doc.sents]

    tokens_lengths = [count_tokens(sent) for sent in sentences]   #3

    chunks = []
    start_idx = 0

    while start_idx < len(sentences):
        current_chunk = []
        current_token_count = 0
        for idx in range(start_idx, len(sentences)):
            if current_token_count + tokens_lengths[idx] > max_tokens:
                break
            current_chunk.append(sentences[idx])
            current_token_count += tokens_lengths[idx]

        chunks.append(" ".join(current_chunk))

        if overlap >= len(current_chunk):   #4
            start_idx += 1
        else:
            start_idx += len(current_chunk) - overlap

    return chunks

The listing demonstrates model loading, sentence extraction, token-aware chunk construction, and overlap management. section 7.7.5

Choosing spaCy for chunking

Use spaCy when retrieval or generation depends on preserving linguistic context. Compared with fixed-length chunking, it may require more computational resources and domain knowledge; the comparison records four spaCy chunks, 5.8 seconds of execution time, and 5,876 tokens. section 7.7.5

How Generative AI in Action covers it

How deep it goes
Implements model loading, sentence extraction, token counting, and overlap, without detailing linguistic-model internals.
Examples
Demonstrates sentence chunking with en_core_web_sm, configurable overlap, and a performance comparison against NLTK and simpler methods.
What you can do after
Apply spaCy via Python to create token-bounded, sentence-aware text chunks.
How it connects
Connects NLP-based chunking with sentence splitting, NLTK, fixed-length chunking, and retrieval-augmented generation.

Also explained in

Compare how 5 books explain spaCy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.