Archived
Added document generation
This commit is contained in:
@@ -1,74 +1,77 @@
|
||||
using Common.Extensions;
|
||||
using ReportGenerator.Generator.Abstract;
|
||||
using ReportGenerator.Generator.Interface;
|
||||
using ReportGenerator.Generator.Model;
|
||||
using ReportGenerator.Models;
|
||||
using System.Text;
|
||||
|
||||
namespace ReportGenerator;
|
||||
|
||||
public class ReportGenerator
|
||||
public class ReportGenerator : FileSerializableBase
|
||||
{
|
||||
private IDocumentGenerator Document { get; }
|
||||
private ICollection<ImageStats> Images { get; }
|
||||
|
||||
private readonly StringBuilder _sb = new();
|
||||
|
||||
private ReportGenerator(IEnumerable<ImageStats> stats) => Images = stats.ToArray();
|
||||
|
||||
public void ToFile(string path) =>
|
||||
File.WriteAllText(path, ToString(), Encoding.UTF8);
|
||||
public ReportGenerator(IEnumerable<ImageStats> stats, IDocumentGenerator document)
|
||||
{
|
||||
Images = stats.ToArray();
|
||||
Document = document;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() =>
|
||||
_sb.ToString();
|
||||
public override string ToString() => Document.ToString();
|
||||
|
||||
#region Fluent definition
|
||||
|
||||
public ReportGenerator WithTitle(string text)
|
||||
{
|
||||
_sb.AppendHeading(1, text);
|
||||
Document.AppendHeading(1, text);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReportGenerator WithFullStatistic(string title)
|
||||
{
|
||||
_sb.AppendHeading(2, title);
|
||||
Document.AppendHeading(2, title);
|
||||
|
||||
foreach (var stat in Images)
|
||||
{
|
||||
_sb.AppendHeading(3, stat.ImageName);
|
||||
_sb.AppendParagraph(HtmlImage(Path.Combine("img", stat.ImageName), 350, 350));
|
||||
Document
|
||||
.AppendHeading(3, stat.ImageName)
|
||||
.AppendParagraph(
|
||||
Document.FormatImage(Path.Combine("img", stat.ImageName), new Bounds(0, 350))
|
||||
)
|
||||
.AppendTable(
|
||||
stat.Reference.Count + 5,
|
||||
table =>
|
||||
{
|
||||
table.AppendHeader(stat
|
||||
.Reference
|
||||
.Prepend("Image")
|
||||
.Prepend("CER (avg)")
|
||||
.Prepend("WER")
|
||||
.Prepend("Elapsed")
|
||||
.Prepend("Processor")
|
||||
);
|
||||
|
||||
AppendRow(stat
|
||||
.Reference
|
||||
.Prepend("Image")
|
||||
.Prepend("CER (avg)")
|
||||
.Prepend("WER")
|
||||
.Prepend("Elapsed")
|
||||
.Prepend("Processor")
|
||||
);
|
||||
var processors = stat.Processors
|
||||
.OrderBy(s => s.Distance)
|
||||
.ThenBy(s => s.ProcessingTime);
|
||||
|
||||
AppendRowSeparator(stat.Reference.Count + 5);
|
||||
foreach (var processor in processors)
|
||||
{
|
||||
var imgPath = Path.Combine("results", $"{processor.Name}.00.{stat.ImageName}.png");
|
||||
|
||||
var processors = stat.Processors
|
||||
.OrderBy(s => s.Distance)
|
||||
.ThenBy(s => s.ProcessingTime);
|
||||
|
||||
foreach (var processor in processors)
|
||||
{
|
||||
var imgPath = Path.Combine("results", $"{processor.Name}.00.{stat.ImageName}.png");
|
||||
|
||||
AppendRow(processor.Words
|
||||
.Select(s => s.ToString() ?? string.Empty)
|
||||
.Prepend(HtmlImage(imgPath, 150, 150))
|
||||
.Prepend(processor.Words.Average(s => s.Distance).ToString("F2"))
|
||||
.Prepend($"{processor.Distance * 100:F1}%")
|
||||
.Prepend($"{processor.ProcessingTime * 1000:F1}ms")
|
||||
.Prepend(processor.Name)
|
||||
table.AppendRow(processor.Words
|
||||
.Select(s => s.ToString() ?? string.Empty)
|
||||
.Prepend(Document.FormatImage(imgPath, new Bounds(0, 150)))
|
||||
.Prepend(processor.Words.Average(s => s.Distance).ToString("F2"))
|
||||
.Prepend($"{processor.Distance * 100:F1}%")
|
||||
.Prepend($"{processor.ProcessingTime * 1000:F1}ms")
|
||||
.Prepend(processor.Name)
|
||||
);
|
||||
}
|
||||
})
|
||||
.AppendParagraph(
|
||||
$"*Comparison data generated based on {stat.Reference.Count} tagged words.*"
|
||||
);
|
||||
}
|
||||
|
||||
_sb.AppendLine();
|
||||
_sb.AppendParagraph(
|
||||
$"*Comparison data generated based on {stat.Reference.Count} tagged words.*"
|
||||
);
|
||||
}
|
||||
|
||||
return this;
|
||||
@@ -77,7 +80,7 @@ public class ReportGenerator
|
||||
|
||||
public ReportGenerator WithBestOf(string title, int context = 5)
|
||||
{
|
||||
_sb.AppendHeading(2, title);
|
||||
Document.AppendHeading(2, title);
|
||||
|
||||
var lookup = Images
|
||||
.SelectMany(s => s.Processors)
|
||||
@@ -107,7 +110,7 @@ public class ReportGenerator
|
||||
|
||||
void AppendComparison(
|
||||
int level,
|
||||
string tableTitle,
|
||||
string title,
|
||||
IEnumerable<(string, double)> values,
|
||||
string valueUnit = ""
|
||||
)
|
||||
@@ -115,60 +118,32 @@ public class ReportGenerator
|
||||
var tValues = values.ToArray();
|
||||
var tContext = Math.Min(tValues.Length / 2, context);
|
||||
|
||||
_sb.AppendHeading(level, tableTitle);
|
||||
|
||||
AppendRow(new[] { "Processor", "Average" });
|
||||
AppendRowSeparator(2);
|
||||
AppendRows(tValues.Take(tContext).Select(v => new[]
|
||||
{
|
||||
v.Item1,
|
||||
v.Item2.ToString("F2") + valueUnit
|
||||
}));
|
||||
AppendRowSeparator(2, "...");
|
||||
AppendRows(tValues.TakeLast(tContext).Select(v => new[]
|
||||
{
|
||||
v.Item1,
|
||||
v.Item2.ToString("F2") + valueUnit
|
||||
}));
|
||||
Document
|
||||
.AppendHeading(level, title)
|
||||
.AppendTable(2, table => table
|
||||
.AppendHeader(new[] { "Processor", "Average" })
|
||||
.AppendRows(tValues
|
||||
.Take(tContext).Select(v => new[]
|
||||
{
|
||||
v.Item1,
|
||||
v.Item2.ToString("F2") + valueUnit
|
||||
}))
|
||||
.AppendRow("...")
|
||||
.AppendRows(tValues.TakeLast(tContext).Select(v => new[]
|
||||
{
|
||||
v.Item1,
|
||||
v.Item2.ToString("F2") + valueUnit
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
#region Overrides of FileSerializableBase
|
||||
|
||||
private void AppendRow(IEnumerable<string> row)
|
||||
{
|
||||
const string separator = " | ";
|
||||
_sb.AppendLine(separator + string.Join(" | ", row) + separator);
|
||||
}
|
||||
|
||||
private void AppendRows(IEnumerable<IEnumerable<string>> rows)
|
||||
{
|
||||
foreach (var row in rows)
|
||||
{
|
||||
AppendRow(row);
|
||||
}
|
||||
}
|
||||
|
||||
private static string HtmlImage(string path, int maxWidth, int maxHeight)
|
||||
{
|
||||
if (!path.EndsWith(".png"))
|
||||
{
|
||||
path += ".png";
|
||||
}
|
||||
|
||||
return $"<img src=\"{path}\" style=\"max-width:{maxWidth}px;max-height:{maxHeight}px;\" />";
|
||||
}
|
||||
|
||||
private void AppendRowSeparator(int columns, string content = "---") =>
|
||||
AppendRow(Enumerable.Range(0, columns).Select(_ => content));
|
||||
/// <inheritdoc />
|
||||
public override string FileExtension => Document.FileExtension;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Factory Methods
|
||||
|
||||
public static ReportGenerator FromData(IEnumerable<ImageStats> stats) => new(stats);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user