Files
myAi/Apis/rag-api/Services/TextChunker.cs
T
claude 75bc9509c5
Build and Push Docker Images / build (push) Successful in 4m35s
Changes
2026-05-14 14:12:29 +03:00

25 lines
751 B
C#

using Api.Services.Contracts;
namespace Api.Services;
public sealed class TextChunker : ITextChunker
{
public IReadOnlyList<string> Chunk(string text, int chunkSize, int overlap)
{
if (string.IsNullOrWhiteSpace(text)) return [];
chunkSize = Math.Clamp(chunkSize, 300, 3000);
overlap = Math.Clamp(overlap, 0, chunkSize / 2);
var chunks = new List<string>();
var start = 0;
while (start < text.Length)
{
var length = Math.Min(chunkSize, text.Length - start);
var chunk = text.Substring(start, length).Trim();
if (!string.IsNullOrWhiteSpace(chunk)) chunks.Add(chunk);
start += chunkSize - overlap;
}
return chunks;
}
}