Commit 9c51dadc for tesseract
commit 9c51dadc6715eae21438018e9118a97fafe1f35a
Author: David Lowry-Duda <david@lowryduda.com>
Date: Tue Aug 25 10:12:29 2026 -0400
Fix malformed ALTO WC for single-digit and 100 confidences (#4616)
This commit corrects word confidence translation from Tesseract to ALTO.
Tesseract uses integer confidence, ALTO uses floats from 0.0 to 1.0. The
current code converts Tesseract to ALTO by concatenating onto the string
"0.", but this is incorrect for confidences under 10 percent or exactly
100 percent.
For example, if Tesseract reports 9 percent confident, the current
implementation converts this to 0.9, or 90 percent confident in ALTO.
Co-authored-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/api/altorenderer.cpp b/src/api/altorenderer.cpp
index 3a69bb1f..f207c1d2 100644
--- a/src/api/altorenderer.cpp
+++ b/src/api/altorenderer.cpp
@@ -20,6 +20,8 @@
#include <tesseract/baseapi.h>
#include <tesseract/renderer.h>
+#include <iomanip> // for std::fixed, std::setprecision
+#include <locale> // for std::locale::classic
#include <memory>
#include <sstream> // for std::stringstream
@@ -45,7 +47,11 @@ static void AddBoxToAlto(const ResultIterator *it, PageIteratorLevel level,
if (level == RIL_WORD) {
int wc = it->Confidence(RIL_WORD);
- alto_str << " WC=\"0." << wc << "\"";
+ // Note: std::fixed and std::setprecision persist on alto_str, but this
+ // is the only floating-point value ever written to that stream, so the
+ // formatting state cannot affect any other output.
+ alto_str << " WC=\"" << std::fixed << std::setprecision(2)
+ << wc / 100.0f << "\"";
} else {
alto_str << ">";
}