using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HeuristicLab.Common; namespace HeuristicLab.Problems.GrammaticalOptimization { public class SentenceSetStatistics { public int NumberOfSentences { get; private set; } public string BestSentence { get; private set; } public string FirstSentence { get; private set; } public string LastSentence { get; private set; } public double BestSentenceQuality { get; private set; } public double BestSentenceIndex { get; private set; } public double FirstSentenceQuality { get; private set; } public double LastSentenceQuality { get; private set; } public double AverageQuality { get { return sumQualities / NumberOfSentences; } } // public double MedianQuality { get; private set; } private double sumQualities; private readonly double bestKnownQuality; public SentenceSetStatistics(double bestKnownQuality = 1.0) { this.bestKnownQuality = bestKnownQuality; BestSentenceQuality = double.NegativeInfinity; BestSentence = string.Empty; FirstSentence = string.Empty; LastSentence = string.Empty; } public void AddSentence(string sentence, double quality) { sumQualities += quality; NumberOfSentences++; if (NumberOfSentences == 1) { FirstSentence = sentence; FirstSentenceQuality = quality; } if (quality > BestSentenceQuality) { BestSentence = sentence; BestSentenceQuality = quality; BestSentenceIndex = NumberOfSentences; } LastSentence = sentence; LastSentenceQuality = quality; } public override string ToString() { return string.Format("Sentences: {0,10} avg.-quality {1,7:F5} best {2,7:F5} {3,2} {4,10} {5,30} first {6,7:F5} {7,20} last {8,7:F5} {9,20}", NumberOfSentences, AverageQuality, BestSentenceQuality, DoubleExtensions.IsAlmost(BestSentenceQuality, bestKnownQuality) ? 1.0 : 0.0, BestSentenceIndex, TrimToSize(BestSentence, 30), FirstSentenceQuality, TrimToSize(FirstSentence, 20), LastSentenceQuality, TrimToSize(LastSentence, 20) ); } private string TrimToSize(string s, int len) { if (s.Length < len) return s; else { var sb = new StringBuilder(len); sb.Append(s.Substring(0, len - 3)); sb.Append("..."); return sb.ToString(); } } } }