From c869c362aaa0ab3d236433ac3de4a31e32268c9a Mon Sep 17 00:00:00 2001 From: Enkidu93 Date: Wed, 1 Jul 2026 13:54:59 -0400 Subject: [PATCH] Properly handle average confidence for translation jos with no pretranslations (i.e. primarily SMT) --- .../Services/PlatformService.cs | 10 +++++++--- .../Services/PlatformServiceTests.cs | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Serval/src/Serval.Translation/Services/PlatformService.cs b/src/Serval/src/Serval.Translation/Services/PlatformService.cs index e0a24184..1616bd6b 100644 --- a/src/Serval/src/Serval.Translation/Services/PlatformService.cs +++ b/src/Serval/src/Serval.Translation/Services/PlatformService.cs @@ -376,7 +376,7 @@ public async Task InsertPretranslationsAsync( int nextModelRevision = engine.ModelRevision + 1; var batch = new List(); - double confidenceTotal = 0.0; + double totalConfidence = 0.0; int numPretranslations = 0; await foreach (PretranslationContract item in pretranslations.WithCancellation(cancellationToken)) { @@ -404,7 +404,7 @@ public async Task InsertPretranslationsAsync( Confidence = item.Confidence, } ); - confidenceTotal += item.Confidence ?? 0.0; + totalConfidence += item.Confidence ?? 0.0; numPretranslations += 1; if (batch.Count == PretranslationInsertBatchSize) { @@ -417,7 +417,11 @@ public async Task InsertPretranslationsAsync( await _builds.UpdateAsync( b => b.Id == buildId, - u => u.Set(b => b.ExecutionData.AveragePretranslationConfidence, confidenceTotal / numPretranslations), + u => + u.Set( + b => b.ExecutionData.AveragePretranslationConfidence, + numPretranslations > 0 ? totalConfidence / numPretranslations : 0.0 + ), cancellationToken: cancellationToken ); } diff --git a/src/Serval/test/Serval.Translation.Tests/Services/PlatformServiceTests.cs b/src/Serval/test/Serval.Translation.Tests/Services/PlatformServiceTests.cs index 0289ed71..9e0d0901 100644 --- a/src/Serval/test/Serval.Translation.Tests/Services/PlatformServiceTests.cs +++ b/src/Serval/test/Serval.Translation.Tests/Services/PlatformServiceTests.cs @@ -52,13 +52,27 @@ await env.Builds.InsertAsync( Assert.That(env.Pretranslations.Count, Is.EqualTo(1)); await env.PlatformService.BuildCompletedAsync("b0", 0, 0.0); Assert.That(env.Pretranslations.Count, Is.EqualTo(1)); + await env.PlatformService.BuildStartedAsync("b0"); await env.PlatformService.InsertPretranslationsAsync("e0", "b0", GetTestPretranslations()); await env.PlatformService.BuildCompletedAsync("b0", 0, 0.0); Assert.That(env.Pretranslations.Count, Is.EqualTo(1)); Assert.That( (await env.Builds.GetAsync(b => b.Id == "b0"))?.ExecutionData.AveragePretranslationConfidence, - Is.Zero + Is.Zero.Within(0.001) + ); + + await env.PlatformService.BuildStartedAsync("b0"); + await env.PlatformService.InsertPretranslationsAsync( + "e0", + "b0", + AsyncEnumerable.Empty() + ); + await env.PlatformService.BuildCompletedAsync("b0", 0, 0.0); + Assert.That(env.Pretranslations.Count, Is.EqualTo(0)); + Assert.That( + (await env.Builds.GetAsync(b => b.Id == "b0"))?.ExecutionData.AveragePretranslationConfidence, + Is.Zero.Within(0.001) ); }