From 5fdc343baf0d7745ff434ce993ae9aa4ad7d5d82 Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Thu, 1 Oct 2026 01:32:59 +0700 Subject: [PATCH] Keep a leading insertion in the first alignment span. indexed_map_to_output stopped the left walk at index 1, so a leading epsilon that writes the first output character was left out of the span. Signed-off-by: Sasha Mitchell --- .../fst_alignment/alignment.py | 2 +- .../test_fst_alignment.py | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/nemo_text_processing/test_fst_alignment.py diff --git a/nemo_text_processing/fst_alignment/alignment.py b/nemo_text_processing/fst_alignment/alignment.py index c16aec079..8a61b9f2b 100644 --- a/nemo_text_processing/fst_alignment/alignment.py +++ b/nemo_text_processing/fst_alignment/alignment.py @@ -217,7 +217,7 @@ def indexed_map_to_output(alignment: List[tuple], start: int, end: int, mode: st # extend aligned_start to left while ( - aligned_start - 1 > 0 + aligned_start - 1 >= 0 and alignment[aligned_start - 1][0] == EPS and (alignment[aligned_start - 1][1] in tn_itn_symbols or alignment[aligned_start - 1][1] == EPS) ): diff --git a/tests/nemo_text_processing/test_fst_alignment.py b/tests/nemo_text_processing/test_fst_alignment.py new file mode 100644 index 000000000..c2d8a4d43 --- /dev/null +++ b/tests/nemo_text_processing/test_fst_alignment.py @@ -0,0 +1,38 @@ +# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +pytest.importorskip('pynini') + +from nemo_text_processing.fst_alignment.alignment import EPS, indexed_map_to_output # noqa: E402 + + +def test_leading_insertion_stays_in_the_first_span(): + alignment = [ + (EPS, 't'), + ('2', 'w'), + ] + start, end = indexed_map_to_output(alignment, start=0, end=1, mode='itn') + assert (start, end) == (0, 2) + + +def test_a_later_span_does_not_take_the_leading_insertion(): + alignment = [ + (EPS, 'X'), + ('a', 'a'), + ('b', 'b'), + ] + start, end = indexed_map_to_output(alignment, start=1, end=2, mode='itn') + assert (start, end) == (2, 3)