using AIStudio.Tools.Rust;
namespace AIStudio.Tools.DocumentSegments;
///
/// A format of documents which are text themselves, e.g., Markdown, LaTeX, or web pages, whose words a model works on.
///
///
///
/// The new document is the source with the words of each segment replaced; everything else stays byte for byte, in
/// the encoding of the source, see TextDocumentFile and TextSegmentWriter. A format brings how it reads its segments,
/// how new words fit into it, and, when its files name their encoding, how to find that name.
///
///
/// The segments reach the model in the order of the document, whatever order the reader of the format finds them in,
/// e.g., Markdig puts the footnotes of a Markdown file last.
///
///
public abstract class TextSegmentFormat : IDocumentSegmentFormat
{
#region Implementation of IDocumentSegmentFormat
///
public abstract FileTypeFilter FileType { get; }
///
public abstract string LimitsNotice { get; }
///
public long MaxTextSize => DocumentMemoryBudget.MaxTextSize(this.MemoryFactor);
///
public long TextSizeOf(string path) => new FileInfo(path).Length;
///
public async Task> ReadAsync(string path, ILogger logger, CancellationToken token)
{
var file = await this.ReadFileAsync(path, token);
return this.ReadSegments(file.Text).OrderBy(segment => segment.Start).Select(segment => segment.Segment).ToList();
}
///
public virtual async Task WriteAsync(string sourcePath, string targetPath, SegmentResults results, string languageTag, ILogger logger, CancellationToken token)
{
var file = await this.ReadFileAsync(sourcePath, token);
var result = TextSegmentWriter.Apply(file.Text, this.ReadSegments(file.Text), results, this.EncodedWords, out var text);
if (result.Outcome is SegmentWriteOutcome.WRITTEN)
await file.WriteAsync(targetPath, text, token);
return result;
}
#endregion
///
/// How many times its size a document of this format takes in memory while it is read and written, measured with
/// a dense document of many short paragraphs, see DocumentMemoryBudget.
///
protected abstract int MemoryFactor { get; }
///
/// Reads the segments of a text of this format, in any order.
///
/// The text of the document.
protected abstract IReadOnlyList ReadSegments(string text);
///
/// Makes new words fit into a text of this format, e.g., escapes the characters which would read as markup.
///
/// One run of new words between two tags, see TextSegmentWriter.Apply.
protected abstract string EncodedWords(string words);
///
/// Finds the encoding which a text of this format names for itself, e.g., the charset of a web page.
///
/// The text of the document.
/// The name of the encoding; null when the text names none, or the format has no way to name one.
protected virtual string? DeclaredEncodingOf(string text) => null;
///
/// Reads a document of this format, which has to be in Unicode and no larger than MaxTextSize, see TextDocumentFile.
///
/// The document.
/// Cancels the reading.
protected Task ReadFileAsync(string path, CancellationToken token) => TextDocumentFile.ReadAsync(path, this.MaxTextSize, this.DeclaredEncodingOf, token);
}