namespace AIStudio.Tools.DocumentSegments; /// /// Finds where the formatting of words changes, for formats which give each change a pair of tags, e.g., /// a word in bold in the middle of a sentence. /// /// /// The words in the most common formatting stay words, and those in any other formatting get a pair of their /// own. Spaces never get one, since nobody sees how a space is formatted. /// public static class FormatSpans { /// /// How many changes of formatting one segment may carry as pairs of tags. /// /// /// Every pair is one more thing a model has to keep in place. A segment whose formatting changes more often, /// e.g., a list of terms in bold, keeps one formatting for its new words instead and counts as simplified. /// public const int MAX_PAIRS = 12; /// /// Finds the formatting most words have, by their letters. /// /// The words with the keys of their formatting. /// How the format describes formatting, e.g., the properties of a run. /// The key of the most common formatting; the first one on a tie; empty for no words. public static string BaseKeyOf(IEnumerable> words) => words .GroupBy(word => word.FormatKey) .OrderByDescending(group => group.Sum(word => word.Text.LetterCount)) .Select(group => group.Key) .FirstOrDefault() ?? string.Empty; /// /// Joins words which follow each other into spans of the same formatting. /// /// /// Spaces in a formatting of their own join their neighbor, since a pair around them would only burden the /// model. Spaces in the most common formatting stay apart, e.g., between a word in bold and one in italics. /// /// The words in their order, with their formatting and its key. /// The key of the most common formatting, see BaseKeyOf. /// How the format describes formatting, e.g., the properties of a run. public static List> Of(IEnumerable> words, string baseFormatKey) { var spans = new List>(); foreach (var (text, format, formatKey) in words) { var isBlankOfItsOwn = string.IsNullOrWhiteSpace(text) && formatKey != baseFormatKey; if (spans.Count > 0 && (spans[^1].FormatKey == formatKey || isBlankOfItsOwn)) spans[^1] = spans[^1] with { Text = spans[^1].Text + text }; else if (spans.Count > 0 && string.IsNullOrWhiteSpace(spans[^1].Text) && spans[^1].FormatKey != baseFormatKey) spans[^1] = new FormatSpan(formatKey, format, spans[^1].Text + text); else spans.Add(new FormatSpan(formatKey, format, text)); } return spans; } }