From bb88e512a0e044ab4d7ab2f8f75c8796396fbcad Mon Sep 17 00:00:00 2001 From: Peter Chapman Date: Thu, 6 Aug 2026 10:03:00 +1200 Subject: [PATCH 1/2] Allow passing a null pointer to swAlignModel_getTrainingAlignment --- .gitignore | 1 + src/shared_library/thot.cc | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d60b6215..c0a2807d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ Release/ *.nupkg .vs/ .vscode/ +.idea/ build*/ artifacts/ diff --git a/src/shared_library/thot.cc b/src/shared_library/thot.cc index d8029f21..23c9f52c 100644 --- a/src/shared_library/thot.cc +++ b/src/shared_library/thot.cc @@ -1094,11 +1094,14 @@ extern "C" WordAlignmentMatrix waMatrix; LgProb prob = alignmentModel->getTrainingAlignment(n, waMatrix); - // A filtered/out-of-range pair yields an empty matrix, so clamp to its actual - // dimensions as well as the caller-provided capacity. - for (unsigned int i = 0; i < *iLen && i < waMatrix.get_I(); i++) - for (unsigned int j = 0; j < *jLen && j < waMatrix.get_J(); j++) - matrix[i][j] = waMatrix.getValue(i, j); + if (matrix != nullptr) + { + // A filtered/out-of-range pair yields an empty matrix, so clamp to its actual + // dimensions as well as the caller-provided capacity. + for (unsigned int i = 0; i < *iLen && i < waMatrix.get_I(); i++) + for (unsigned int j = 0; j < *jLen && j < waMatrix.get_J(); j++) + matrix[i][j] = waMatrix.getValue(i, j); + } *iLen = waMatrix.get_I(); *jLen = waMatrix.get_J(); return prob; From 8cc1cdfee00ae674cb39c879f9b316f5b5cf1284 Mon Sep 17 00:00:00 2001 From: Peter Chapman Date: Mon, 10 Aug 2026 12:34:43 +1200 Subject: [PATCH 2/2] Use getSentencePair to calculate getTrainingAlignment dimensions --- src/shared_library/thot.cc | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/shared_library/thot.cc b/src/shared_library/thot.cc index 23c9f52c..b4da90f3 100644 --- a/src/shared_library/thot.cc +++ b/src/shared_library/thot.cc @@ -1092,16 +1092,24 @@ extern "C" { auto alignmentModel = static_cast(swAlignModelHandle); - WordAlignmentMatrix waMatrix; - LgProb prob = alignmentModel->getTrainingAlignment(n, waMatrix); - if (matrix != nullptr) + if (matrix == nullptr) { - // A filtered/out-of-range pair yields an empty matrix, so clamp to its actual - // dimensions as well as the caller-provided capacity. - for (unsigned int i = 0; i < *iLen && i < waMatrix.get_I(); i++) - for (unsigned int j = 0; j < *jLen && j < waMatrix.get_J(); j++) - matrix[i][j] = waMatrix.getValue(i, j); + // Retrieve the dimensions of the training alignment matrix for sentence pair n + Count c; + std::vector srcSentence, trgSentence; + alignmentModel->getSentencePair(n, srcSentence, trgSentence, c); + *iLen = (unsigned int)srcSentence.size(); + *jLen = (unsigned int)trgSentence.size(); + return SMALL_LG_NUM; } + + WordAlignmentMatrix waMatrix; + LgProb prob = alignmentModel->getTrainingAlignment(n, waMatrix); + // A filtered/out-of-range pair yields an empty matrix, so clamp to its actual + // dimensions as well as the caller-provided capacity. + for (unsigned int i = 0; i < *iLen && i < waMatrix.get_I(); i++) + for (unsigned int j = 0; j < *jLen && j < waMatrix.get_J(); j++) + matrix[i][j] = waMatrix.getValue(i, j); *iLen = waMatrix.get_I(); *jLen = waMatrix.get_J(); return prob;