BotSharp/src/Infrastructure/BotSharp.Core/Knowledges/Services/TextChopperService.cs

24 lines
700 B
C#
Raw Normal View History

2023-06-18 18:15:00 +00:00
using BotSharp.Abstraction.Knowledges.Models;
namespace BotSharp.Core.Knowledges.Services;
public class TextChopperService : ITextChopper
{
public List<string> Chop(string content, ChunkOption option)
{
var chunks = new List<string>();
var currentPos = 0;
while (currentPos < content.Length)
{
var len = content.Length - currentPos > option.Size ?
option.Size :
content.Length - currentPos;
var chunk = content.Substring(currentPos, len);
chunks.Add(chunk);
// move backward
currentPos += option.Size - option.Conjunction;
}
return chunks;
}
}