namespace AIStudio.Tools.DocumentSegments;
///
/// Puts the words of a paragraph into its segment, with a pair of tags around each change of formatting, for the
/// formats which keep the formatting in the properties of their runs, e.g., Word and PowerPoint.
///
///
///
/// The words in the most common formatting stay words, and the new words there get that formatting; the words in
/// any other formatting get a pair of their own, see FormatSpans. A paragraph whose formatting changes more often
/// than FormatSpans.MAX_PAIRS allows is read once more without pairs of formatting: all its new words then get the
/// most common formatting, and the paragraph counts as simplified.
///
///
/// The most common formatting is the one of the most letters, not the one of the first words. Otherwise, a glossary
/// with a term in bold at the start of each line would turn bold as a whole.
///
///
/// The builder of the segment, which the reader of the paragraph shares.
/// Whether changes of formatting become pairs; false when a paragraph is read once more because it has too many of them.
/// Tells a formatting apart from others: the same key for formatting which looks the same, e.g., apart from its language, and empty for formatting which changes nothing.
/// The properties of a run.
internal sealed class FormatSpanReader(TaggedTextBuilder builder, bool usesFormatPairs, Func keyOf) where TFormat : class
{
private readonly Dictionary keys = new(ReferenceEqualityComparer.Instance);
private readonly Dictionary baseFormats = [];
private readonly HashSet pairsWithBaseFormat = [];
///
/// How many changes of formatting became pairs.
///
public int FormatPairCount { get; private set; }
///
/// Whether words outside of pairs of formatting differ in their formatting, which the new words do not keep.
///
public bool HasMixedFormatting { get; private set; }
///
/// The formatting of new words within each pair which holds words, and with 0 outside of any pair: the most
/// common formatting there.
///
public IReadOnlyDictionary BaseFormats => this.baseFormats;
///
/// Tells a formatting apart from others.
///
/// The properties of a run; null for a run without any.
public string KeyOf(TFormat? format)
{
if (format is null)
return string.Empty;
if (!this.keys.TryGetValue(format, out var key))
{
key = keyOf(format);
this.keys[format] = key;
}
return key;
}
///
/// Finds the most common formatting of words, by their letters.
///
/// The words, with their formatting.
/// The key of the most common formatting, see FormatSpans.BaseKeyOf.
public string BaseKeyOf(IEnumerable> words) => FormatSpans.BaseKeyOf(words.Select(this.Keyed));
///
/// Adds words which follow each other to the segment.
///
/// The words, with their formatting, in their order.
/// The number of the pair the words lie in; 0 for none.
/// The key of the most common formatting within this pair, see BaseKeyOf.
/// Notes what a new pair of formatting stands for, given its number and its formatting.
public void AddWords(IEnumerable> words, int pairId, string baseFormatKey, Action addFormatPair)
{
foreach (var span in FormatSpans.Of(words.Select(this.Keyed), baseFormatKey))
{
if (!usesFormatPairs || span.FormatKey == baseFormatKey)
{
builder.Words(span.Text);
this.NoteFormat(pairId, span, baseFormatKey);
continue;
}
var formatPair = builder.Open();
addFormatPair(formatPair, span.Format);
builder.Words(span.Text);
builder.Close(formatPair);
this.FormatPairCount++;
}
}
private KeyedWords Keyed(FormattedWords words) => new(words.Text, words.Format, this.KeyOf(words.Format));
///
/// Notes the formatting of words outside of a pair of formatting.
///
///
/// The first words in the most common formatting decide. Words in another formatting only get here without
/// pairs of formatting; they stand in until such words come, e.g., when spaces alone are in the most common
/// formatting, and they make the formatting mixed.
///
private void NoteFormat(int pairId, FormatSpan span, string baseFormatKey)
{
if (span.FormatKey == baseFormatKey)
{
if (this.pairsWithBaseFormat.Add(pairId))
this.baseFormats[pairId] = span.Format;
return;
}
this.baseFormats.TryAdd(pairId, span.Format);
if (!string.IsNullOrWhiteSpace(span.Text))
this.HasMixedFormatting = true;
}
}