Backup automatico script del 2026-08-23 12:04
This commit is contained in:
1 parent
11823447a2
commit
795a15a7b6
66 files changed
+8546
No files matched your search
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Utility condivise per chunking testo RAG."""
|
||||
|
||||
import re
|
||||
|
||||
CHUNK_SIZE = 900
|
||||
CHUNK_OVERLAP = 150
|
||||
|
||||
|
||||
def chunk_text(text: str) -> list[str]:
|
||||
text = re.sub(r"\n{3,}", "\n\n", text.strip())
|
||||
if len(text) <= CHUNK_SIZE:
|
||||
return [text] if text else []
|
||||
chunks = []
|
||||
start = 0
|
||||
while start < len(text):
|
||||
end = min(len(text), start + CHUNK_SIZE)
|
||||
chunks.append(text[start:end])
|
||||
if end >= len(text):
|
||||
break
|
||||
start = max(end - CHUNK_OVERLAP, start + 1)
|
||||
return chunks
|
||||
Reference in new issue
Block a user