Struct Splitter

Source
pub struct Splitter<T: Sizer> { /* private fields */ }
Expand description

A struct for splitting code into chunks.

Implementations§

Source§

impl<T> Splitter<T>
where T: Sizer,

Source

pub fn new(language: Language, sizer: T) -> Result<Self>

Create a new Splitter that counts the size of code chunks with the given sizer.

§Example: split by characters
use code_splitter::{CharCounter, Splitter};

let lang = tree_sitter_md::language();
let splitter = Splitter::new(lang, CharCounter).unwrap();
let chunks = splitter.split(b"hello, world!").unwrap();
§Example: split by words
use code_splitter::{Splitter, WordCounter};

let lang = tree_sitter_md::language();
let splitter = Splitter::new(lang, WordCounter).unwrap();
let chunks = splitter.split(b"hello, world!").unwrap();
§Example: split by tokens with huggingface tokenizer
use code_splitter::Splitter;
use tokenizers::Tokenizer;

let lang = tree_sitter_md::language();
let tokenizer = Tokenizer::from_pretrained("bert-base-cased", None).unwrap();
let splitter = Splitter::new(lang, tokenizer).unwrap();
let chunks = splitter.split(b"hello, world!").unwrap();
§Example: split by tokens with tiktoken core BPE
use code_splitter::Splitter;
use tiktoken_rs::cl100k_base;

let lang = tree_sitter_md::language();
let bpe = cl100k_base().unwrap();
let splitter = Splitter::new(lang, bpe).unwrap();
let chunks = splitter.split(b"hello, world!").unwrap();
Source

pub fn with_max_size(self, max_size: usize) -> Self

Set the maximum size of a chunk. The default is 512.

§Example: set the maximum size to 256
use code_splitter::{CharCounter, Splitter};

let lang = tree_sitter_md::language();
let splitter = Splitter::new(lang, CharCounter)
  .unwrap()
  .with_max_size(256);
let chunks = splitter.split(b"hello, world!").unwrap();
Source

pub fn split(&self, code: &[u8]) -> Result<Vec<Chunk>>

Split the code into chunks with no larger than max_size.

Auto Trait Implementations§

§

impl<T> Freeze for Splitter<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Splitter<T>
where T: RefUnwindSafe,

§

impl<T> Send for Splitter<T>
where T: Send,

§

impl<T> Sync for Splitter<T>
where T: Sync,

§

impl<T> Unpin for Splitter<T>
where T: Unpin,

§

impl<T> UnwindSafe for Splitter<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.