namespace AIStudio.Tools;
public static class IConfidenceExtensions
{
///
/// Determine the optimal confidence threshold for a list of items
/// in order to match a target window of number of items.
///
/// The list of confidence items to analyze.
/// The minimum number of items in the target window. Should be at least 2 and more than numMinItems.
/// The maximum number of items in the target window.
/// The minimum number of items to match the threshold. Should be at least 1 and less than targetWindowMin.
/// The maximum number of steps to search for the threshold.
/// The type of items in the list.
/// The confidence threshold.
public static float GetConfidenceThreshold(this IList items, int targetWindowMin = 2, int targetWindowMax = 3, int numMinItems = 1, int maxSteps = 10) where T : IConfidence
{
var confidenceValues = items.Select(x => x.Confidence).ToList();
var lowerBound = confidenceValues.Min();
var upperBound = confidenceValues.Max();
//
// We search for a threshold so that we have between
// targetWindowMin and targetWindowMax items. When not
// possible, we take all items (i.e., threshold = 0f)
//
var threshold = 0.0f;
// Check the case where the confidence values are too close:
if (upperBound - lowerBound >= 0.01)
{
var previousThreshold = 0.0f;
for (var i = 0; i < maxSteps; i++)
{
threshold = lowerBound + (upperBound - lowerBound) * i / maxSteps;
var numMatches = items.Count(x => x.Confidence >= threshold);
if (numMatches <= numMinItems)
{
threshold = previousThreshold;
break;
}
if (numMatches <= targetWindowMax && numMatches >= targetWindowMin)
break;
previousThreshold = threshold;
}
}
return threshold;
}
}