Skip to main content

Split text into semantic chunks, up to a desired chunk size. Supports calculating length by characters and tokens (when used with large language models).

Project description

semantic-text-splitter

Documentation Status Licence

Large language models (LLMs) can be used for many tasks, but often have a limited context size that can be smaller than documents you might want to use. To use documents of larger length, you often have to split your text into chunks to fit within this context size.

This crate provides methods for splitting longer pieces of text into smaller chunks, aiming to maximize a desired chunk size, but still splitting at semantically sensible boundaries whenever possible.

Get Started

By Number of Characters

from semantic_text_splitter import CharacterTextSplitter

# Maximum number of characters in a chunk
max_characters = 1000
# Optionally can also have the splitter not trim whitespace for you
splitter = CharacterTextSplitter(trim_chunks=False)

chunks = splitter.chunks("your document text", max_characters)

Using a Range for Chunk Capacity

You also have the option of specifying your chunk capacity as a range.

Once a chunk has reached a length that falls within the range it will be returned.

It is always possible that a chunk may be returned that is less than the start value, as adding the next piece of text may have made it larger than the end capacity.

from semantic_text_splitter import CharacterTextSplitter

# Optionally can also have the splitter trim whitespace for you
splitter = CharacterTextSplitter()

# Maximum number of characters in a chunk. Will fill up the
# chunk until it is somewhere in this range.
chunks = splitter.chunks("your document text", chunk_capacity=(200, 1000))

Method

To preserve as much semantic meaning within a chunk as possible, a recursive approach is used, starting at larger semantic units and, if that is too large, breaking it up into the next largest unit. Here is an example of the steps used:

  1. Split the text by a given level
  2. For each section, does it fit within the chunk size?
    • Yes. Merge as many of these neighboring sections into a chunk as possible to maximize chunk length.
    • No. Split by the next level and repeat.

The boundaries used to split the text if using the top-level chunks method, in descending length:

  1. Descending sequence length of newlines. (Newline is \r\n, \n, or \r) Each unique length of consecutive newline sequences is treated as its own semantic level.
  2. Unicode Sentence Boundaries
  3. Unicode Word Boundaries
  4. Unicode Grapheme Cluster Boundaries
  5. Characters

Splitting doesn't occur below the character level, otherwise you could get partial bytes of a char, which may not be a valid unicode str.

Note on sentences: There are lots of methods of determining sentence breaks, all to varying degrees of accuracy, and many requiring ML models to do so. Rather than trying to find the perfect sentence breaks, we rely on unicode method of sentence boundaries, which in most cases is good enough for finding a decent semantic breaking point if a paragraph is too large, and avoids the performance penalties of many other methods.

Inspiration

This crate was inspired by LangChain's TextSplitter. But, looking into the implementation, there was potential for better performance as well as better semantic chunking.

A big thank you to the unicode-rs team for their unicode-segmentation crate that manages a lot of the complexity of matching the Unicode rules for words and sentences.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semantic_text_splitter-0.2.0.tar.gz (224.5 kB view hashes)

Uploaded Source

Built Distributions

semantic_text_splitter-0.2.0-cp37-abi3-win_amd64.whl (1.4 MB view hashes)

Uploaded CPython 3.7+ Windows x86-64

semantic_text_splitter-0.2.0-cp37-abi3-win32.whl (1.3 MB view hashes)

Uploaded CPython 3.7+ Windows x86

semantic_text_splitter-0.2.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

semantic_text_splitter-0.2.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (3.1 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ s390x

semantic_text_splitter-0.2.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (3.2 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64le

semantic_text_splitter-0.2.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (2.6 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARMv7l

semantic_text_splitter-0.2.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.8 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

semantic_text_splitter-0.2.0-cp37-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (2.8 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.12+ i686

semantic_text_splitter-0.2.0-cp37-abi3-macosx_11_0_arm64.whl (1.6 MB view hashes)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

semantic_text_splitter-0.2.0-cp37-abi3-macosx_10_7_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page