Skip to content

Commit

Permalink
Header anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
russcam committed May 3, 2024
1 parent 5cda050 commit 67b0243
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions tests/Lingua.AccuracyReport.Tests/LanguageDetectionStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@ namespace Lingua.AccuracyReport.Tests;
public class LanguageDetectionStatistics<TDetectorFactory> : IDisposable
where TDetectorFactory : ILanguageDetectorFactory, new()
{
private readonly Dictionary<Language, int[]> _singleWordsStatistics = new();
private readonly Dictionary<Language, int[]> _wordPairsStatistics = new();
private readonly Dictionary<Language, int[]> _sentencesStatistics = new();
private int _wordCount;
private int _wordPairCount;
private int _sentenceCount;
private int _wordLengthCount;
private int _wordPairLengthCount;
private int _sentenceLengthCount;
private static readonly TDetectorFactory Factory;

// intentional to have different detectors stored per closed generic type of TDetectorFactory
// ReSharper disable StaticMemberInGenericType
private static readonly ILanguageDetector LowAccuracyDetector;
private static readonly ILanguageDetector HighAccuracyDetector;
// ReSharper restore StaticMemberInGenericType

public Language Language { get; set; }
private static string Implementation => Factory.Implementation.ToString();
private static string ImplementationLowercase => Implementation.ToLowerInvariant();

static LanguageDetectionStatistics()
{
Factory = new TDetectorFactory();
(LowAccuracyDetector, HighAccuracyDetector) = Factory.Create();
}

private readonly Dictionary<Language, int[]> _singleWordsStatistics = new();
private readonly Dictionary<Language, int[]> _wordPairsStatistics = new();
private readonly Dictionary<Language, int[]> _sentencesStatistics = new();
private int _wordCount;
private int _wordPairCount;
private int _sentenceCount;
private int _wordLengthCount;
private int _wordPairLengthCount;
private int _sentenceLengthCount;

public Language Language { get; set; }

public void Dispose()
{
var accuracyReportsDirectoryPath = Path.Combine(
Expand All @@ -53,10 +56,13 @@ public void Dispose()
File.WriteAllText(accuracyReportFilePath, statisticsReport);
}

private static string Anchor(string text, string id) => $"<a id=\"{id}\">{text}</a>";

private string StatisticsReport()
{
var newlines = new string('\n', 2);
var report = new StringBuilder($"## {Language}");
var language = Language.ToString();
var report = new StringBuilder($"## {Anchor(language, $"{ImplementationLowercase}-{language.ToLowerInvariant()}")}");

var singleWordsAccuracyvalues = MapCountsToAccuracies(_singleWordsStatistics);
var wordPairsAccuracyvalues = MapCountsToAccuracies(_wordPairsStatistics);
Expand Down Expand Up @@ -199,7 +205,7 @@ private static void ComputeStatistics(Dictionary<Language, int[]> statistics, st
string description)
{
var accuracies = statistics.GetValueOrDefault(Language, (0d, 0d));
var report = new StringBuilder($"###{Factory.Implementation}: {Language} {description}");
var report = new StringBuilder($"### {Factory.Implementation}: {Language} {description}");
report.AppendLine();
report.AppendLine();
report.AppendLine($"Detection of {count} {description} (average length: {(int)((double)length / count)} chars)");
Expand Down

0 comments on commit 67b0243

Please sign in to comment.