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
thesis-src/Examples/ReportGenerator/Models/ScannedResultInfo.cs
T
Simon Gruber 9ac01e6b12 checkpoint
2023-11-20 14:26:00 +01:00

46 lines
1.1 KiB
C#

using System.Text.Json;
using System.Text.RegularExpressions;
namespace ReportGenerator.Models;
internal struct ScannedResultInfo
{
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 ICollection<string> GetWords()
{
using var file = File.OpenRead(Path);
return JsonDocument
.Parse(file)
.RootElement
.EnumerateArray()
.Select(e =>
e.GetProperty("Text").GetString() ?? throw new Exception("Cannot parse null words"))
.ToArray();
}
public static ScannedResultInfo FromPath(string path)
{
var match = parseRegex.Match(System.IO.Path.GetFileName(path));
return new ScannedResultInfo
{
Path = path,
ProcessorName = match.Groups["processor"].Value,
ImageName = match.Groups["image"].Value
};
}
/// <inheritdoc />
public override string ToString() => ImageName;
}