This repository has been archived on 2024-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2024-01-08 16:23:24 +01:00

49 lines
1.2 KiB
C#

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;
}