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/CharacterStats.cs
T
Simon Gruber 9ac01e6b12 checkpoint
2023-11-20 14:26:00 +01:00

61 lines
1.3 KiB
C#

namespace ReportGenerator.Models;
internal readonly struct CharacterStats
{
public string Reference { get; }
public string Value { get; }
public double CharacterError { get; }
public CharacterStats(string reference)
{
Reference = reference;
Value = string.Empty;
CharacterError = double.PositiveInfinity;
}
public CharacterStats(string reference, string value)
{
Value = value;
Reference = reference;
CharacterError = CalculateCer(reference, value);
}
private static double CalculateCer(string s1, string s2)
{
var distance = new int[s1.Length + 1, s2.Length + 1];
for (var i = 0; i <= s1.Length; i++)
{
distance[i, 0] = i;
}
for (var j = 0; j <= s2.Length; j++)
{
distance[0, j] = j;
}
for (var i = 1; i <= s1.Length; i++)
{
for (var j = 1; j <= s2.Length; j++)
{
var cost = s2[j - 1] == s1[i - 1] ? 0 : 1;
var c1 = Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1);
var c2 = distance[i - 1, j - 1] + cost;
distance[i, j] = Math.Min(c1, c2);
}
}
return distance[s1.Length, s2.Length];
}
/// <inheritdoc />
public override string ToString()
{
var value = string.IsNullOrEmpty(Value) ? "`null`" : Value;
return $"{value} ({CharacterError})";
}
}