.ipynb
.pdf
Text Splitter
 Contents 
Generic Recursive Text Splitting
Markdown Text Splitter
Latex Text Splitter
Python Code Text Splitter
Character Text Splitting
HuggingFace Length Function
tiktoken (OpenAI) Length Function
NLTK Text Splitter
Spacy Text Splitter
Token Text Splitter
Text Splitter#
When you want to deal with long pieces of text, it is necessary to split up that text into chunks.
As simple as this sounds, there is a lot of potential complexity here. Ideally, you want to keep the semantically related pieces of text together. What “semantically related” means could depend on the type of text.
This notebook showcases several ways to do that.
At a high level, text splitters work as following:
Split the text up into small, semantically meaningful chunks (often sentences).
Start combining these small chunks into a larger chunk until you reach a certain size (as measured by some function).
Once you reach that size, make that chunk its own piece of text and then start creating a new chunk of text with some overlap (to keep context between chunks).
That means there two different axes along which you can customize your text splitter:
How the text is split
How the chunk size is measured
For all the examples below, we will highlight both of these attributes
# This is a long document we can split up.
with open('../../state_of_the_union.txt') as f:
    state_of_the_union = f.read()
Generic Recursive Text Splitting#