Archived
Renamed Examples to Implementation
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
namespace Ocr.Report.Models;
|
||||
|
||||
public readonly struct ImageStats
|
||||
{
|
||||
public string ImageName { get; }
|
||||
|
||||
public ICollection<string> Reference { get; }
|
||||
|
||||
public ICollection<ProcessorStat> Processors { get; }
|
||||
|
||||
public ImageStats(
|
||||
string imageName,
|
||||
ICollection<string> taggedWords,
|
||||
IEnumerable<ScanFileInfo> scanResult
|
||||
)
|
||||
{
|
||||
Reference = taggedWords;
|
||||
ImageName = imageName;
|
||||
Processors = scanResult
|
||||
.Select(t =>
|
||||
{
|
||||
var (elapsed, words) = t.GetData();
|
||||
return new ProcessorStat(t.ProcessorName, elapsed, taggedWords, words);
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using Common.Distance;
|
||||
|
||||
namespace Ocr.Report.Models;
|
||||
|
||||
public readonly struct ProcessorStat : IDistanceComparer<IEnumerable<string>>
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the processor
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The total milliseconds it took the processor to process the data
|
||||
/// </summary>
|
||||
public double ProcessingTime { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<string> Reference { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<string>? Hypothesis { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public double Distance { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Information about the processed values
|
||||
/// </summary>
|
||||
public ICollection<IDistanceComparer<string>> Words { get; }
|
||||
|
||||
public ProcessorStat(
|
||||
string name,
|
||||
double processingTime,
|
||||
ICollection<string> reference,
|
||||
ICollection<string> hypothesis
|
||||
)
|
||||
{
|
||||
Name = name;
|
||||
ProcessingTime = processingTime;
|
||||
|
||||
Reference = reference;
|
||||
Hypothesis = hypothesis;
|
||||
|
||||
Distance = Calculator.GetDistance(
|
||||
reference.OrderBy(s => s).ToArray(),
|
||||
hypothesis.OrderBy(s => s).ToArray()
|
||||
) / reference.Count;
|
||||
|
||||
Words = GetDistanceInfos(reference, hypothesis).ToArray();
|
||||
// Words = reference.Select(r => GetDistanceInfo(r, hypothesis)).ToArray();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Compares the <paramref name="referenceCollection"/> with all given values in the
|
||||
/// <paramref name="hypothesisCollection"/>, determining the <see cref="IDistanceComparer{T}"/>
|
||||
/// with the lowest error
|
||||
/// </summary>
|
||||
private static IEnumerable<IDistanceComparer<string>> GetDistanceInfos(
|
||||
ICollection<string> referenceCollection,
|
||||
ICollection<string> hypothesisCollection
|
||||
)
|
||||
{
|
||||
var results = new List<IDistanceComparer<string>>();
|
||||
|
||||
foreach (var reference in referenceCollection)
|
||||
{
|
||||
var tResults = hypothesisCollection
|
||||
.Select(hypothesis => new DistanceComparer<string>(reference, hypothesis))
|
||||
.Cast<IDistanceComparer<string>>()
|
||||
.ToList();
|
||||
|
||||
results.AddRange(tResults);
|
||||
}
|
||||
|
||||
var lookup = results
|
||||
.OrderBy(result => result.Distance)
|
||||
.ToLookup(result => result.Reference);
|
||||
|
||||
foreach (var reference in referenceCollection)
|
||||
{
|
||||
yield return lookup[reference].FirstOrDefault() ?? new DistanceComparer<string>(reference);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Ocr.Report.Models;
|
||||
|
||||
public struct ScanFileInfo
|
||||
{
|
||||
public string Path { get; private init; }
|
||||
|
||||
private static readonly Regex parseRegex = new(
|
||||
@"(?'image'.+)\.(?'processor'.+)\..+",
|
||||
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase
|
||||
);
|
||||
|
||||
public string ProcessorName { get; set; }
|
||||
|
||||
public string ImageName { get; set; }
|
||||
|
||||
public (double Elapsed, ICollection<string> Words) GetData()
|
||||
{
|
||||
using var file = File.OpenRead(Path);
|
||||
var root = JsonDocument.Parse(file).RootElement;
|
||||
|
||||
var words = root
|
||||
.GetProperty("Words")
|
||||
.EnumerateArray()
|
||||
.Select(e => e.GetProperty("Text").GetString() ?? string.Empty)
|
||||
.ToArray();
|
||||
|
||||
var elapsed = root.GetProperty("Elapsed").GetDouble();
|
||||
|
||||
return (elapsed, words);
|
||||
}
|
||||
|
||||
public static ScanFileInfo FromPath(string path)
|
||||
{
|
||||
var match = parseRegex.Match(System.IO.Path.GetFileName(path));
|
||||
return new ScanFileInfo
|
||||
{
|
||||
Path = path,
|
||||
ProcessorName = match.Groups["processor"].Value,
|
||||
ImageName = match.Groups["image"].Value
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => ImageName;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ocr.Report.Models;
|
||||
|
||||
internal struct TagFileInfo
|
||||
{
|
||||
public string Path { get; private init; }
|
||||
|
||||
public string ImageName { get; set; }
|
||||
|
||||
public ICollection<string> GetWords()
|
||||
{
|
||||
using var file = File.OpenRead(Path);
|
||||
return JsonDocument
|
||||
.Parse(file)
|
||||
.RootElement
|
||||
.GetProperty("words")
|
||||
.EnumerateArray()
|
||||
.Select(w => w.GetString() ?? throw new Exception("Cannot parse null words"))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public static TagFileInfo FromPath(string path) => new()
|
||||
{
|
||||
Path = path,
|
||||
ImageName = System.IO.Path.GetFileNameWithoutExtension(path),
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => ImageName;
|
||||
}
|
||||
Reference in New Issue
Block a user