vd.text#
Text preprocessing and chunking utilities for vd.
Provides functions to clean, normalize, and chunk text before adding to vector databases.
Functions
|
Chunk multiple documents while preserving metadata. |
|
Chunk text into smaller pieces. |
|
Clean and normalize text. |
|
Extract metadata from text. |
|
Normalize whitespace in text. |
|
Truncate text to maximum length. |
- vd.text.chunk_documents(documents, chunk_size=500, *, overlap=50, strategy='chars', id_template='{doc_id}_chunk_{chunk_num}', preserve_metadata=True)[source]#
Chunk multiple documents while preserving metadata.
- Parameters:
documents (
Iterator[tuple[str,str|dict]]) – Iterator of (doc_id, text) or (doc_id, text, metadata) tupleschunk_size (
int) – Size of each chunkoverlap (
int) – Overlap between chunksstrategy (
str) – Chunking strategy (see chunk_text)id_template (
str) – Template for chunk IDs. Can use {doc_id} and {chunk_num}preserve_metadata (
bool) – Whether to copy metadata to all chunks
- Yields:
tuple – (chunk_id, chunk_text, metadata) tuples
- Return type:
Examples
>>> docs = [('doc1', 'Long text...', {'author': 'Alice'})] >>> chunks = list(chunk_documents(docs, chunk_size=20)) >>> len(chunks) >= 1 True
- vd.text.chunk_text(text, chunk_size=500, *, overlap=50, strategy='chars', preserve_sentences=True)[source]#
Chunk text into smaller pieces.
- Parameters:
text (
str) – Text to chunkchunk_size (
int) – Target size of each chunk (in characters or tokens depending on strategy)overlap (
int) – Number of characters/tokens to overlap between chunksstrategy (
str) –Chunking strategy:
’chars’: Character-based chunking
’words’: Word-based chunking
’sentences’: Sentence-based chunking
’paragraphs’: Paragraph-based chunking
preserve_sentences (
bool) – Try to avoid breaking sentences when using chars/words strategy
- Returns:
List of text chunks
- Return type:
Examples
>>> text = "This is sentence one. This is sentence two. This is sentence three." >>> chunks = chunk_text(text, chunk_size=30, strategy='chars') >>> len(chunks) >= 2 True
>>> chunks = chunk_text(text, strategy='sentences') >>> len(chunks) 3
- vd.text.clean_text(text, *, lowercase=False, remove_extra_whitespace=True, remove_urls=False, remove_emails=False, remove_numbers=False, remove_punctuation=False)[source]#
Clean and normalize text.
- Parameters:
- Returns:
Cleaned text
- Return type:
Examples
>>> text = "Hello World! Visit https://example.com" >>> clean_text(text, remove_urls=True) 'Hello World! Visit' >>> clean_text(text, lowercase=True, remove_punctuation=True) 'hello world visit httpsexamplecom'
- vd.text.extract_metadata(text, *, extract_title=True, extract_length=True, extract_word_count=True, extract_language=False)[source]#
Extract metadata from text.
- Parameters:
- Returns:
Extracted metadata
- Return type:
Examples
>>> text = "My Title\n\nThis is the content." >>> meta = extract_metadata(text) >>> meta['title'] 'My Title' >>> meta['char_count'] 30
- vd.text.normalize_whitespace(text)[source]#
Normalize whitespace in text.
Replaces tabs, multiple spaces, and multiple newlines with single versions.
Examples
>>> normalize_whitespace("Hello\t\tWorld \n\n\nTest") 'Hello World \nTest'