File size: 12,510 Bytes
a7c2243 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | # Copyright (c) 2025, NVIDIA CORPORATION. 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 os.path
import re
from functools import cached_property
from typing import Any
from nemo.collections.asr.models import ASRModel
from nemo.collections.asr.parts.utils.rnnt_utils import Hypothesis
from nemo.collections.asr.parts.utils.timestamp_utils import get_segment_offsets, get_words_offsets
class BaseTimestampsTest:
"""
Base class for testing timestamps in decoders (CTC and RNNT).
This class defines common test methods that can be inherited by both
test_ctc_decoding.py and test_rnnt_decoding.py.
"""
@cached_property
def bpe_tokenizer(self):
model_path = "/home/TestData/asr/stt_en_conformer_transducer_small.nemo"
if os.path.exists(model_path):
model = ASRModel.restore_from(model_path, map_location="cpu")
else:
model = ASRModel.from_pretrained("stt_en_conformer_transducer_small", map_location="cpu")
return model.tokenizer
@property
def char_offsets_chars(self):
char_offsets = [
{"char": "e", "start_offset": 0, "end_offset": 1},
{"char": " ", "start_offset": 2, "end_offset": 2},
{"char": "e", "start_offset": 3, "end_offset": 4},
{"char": " ", "start_offset": 5, "end_offset": 5},
{"char": ".", "start_offset": 6, "end_offset": 7},
{"char": " ", "start_offset": 8, "end_offset": 9},
{"char": "e", "start_offset": 10, "end_offset": 11},
{"char": " ", "start_offset": 12, "end_offset": 13},
{"char": "?", "start_offset": 14, "end_offset": 15},
{"char": " ", "start_offset": 16, "end_offset": 17},
]
return char_offsets
@property
def word_offsets_chars_expected_output(self):
return [
{'word': 'e', 'start_offset': 0, 'end_offset': 1},
{'word': 'e.', 'start_offset': 3, 'end_offset': 7},
{'word': 'e?', 'start_offset': 10, 'end_offset': 15},
]
@property
def word_offsets_chars_expected_output_other_delimiter(self):
return [
{'word': 'e e ', 'start_offset': 0, 'end_offset': 5},
{'word': ' e? ', 'start_offset': 8, 'end_offset': 17},
]
@property
def segment_offsets_expected_output(self):
return [
{'segment': 'e e.', 'start_offset': 0, 'end_offset': 7},
{'segment': 'e?', 'start_offset': 10, 'end_offset': 15},
]
@property
def segment_offsets_expected_output_gap(self):
return [
{'segment': 'e e. e?', 'start_offset': 0, 'end_offset': 15},
]
@property
def char_offsets_wpe(self):
char_offsets = [
{"char": "nineteen", "start_offset": 0, "end_offset": 1},
{"char": "##th", "start_offset": 2, "end_offset": 2},
{"char": "re", "start_offset": 3, "end_offset": 4},
{"char": "seven", "start_offset": 5, "end_offset": 6},
{"char": "##ty", "start_offset": 6, "end_offset": 7},
{"char": "eighty", "start_offset": 8, "end_offset": 9},
]
return char_offsets
@property
def word_offsets_wpe_expected_output(self):
return [
{'word': 'nineteenth', 'start_offset': 0, 'end_offset': 2},
{'word': 're', 'start_offset': 3, 'end_offset': 4},
{'word': 'seventy', 'start_offset': 5, 'end_offset': 7},
{'word': 'eighty', 'start_offset': 8, 'end_offset': 9},
]
@property
def word_offsets_wpe_expected_output_other_delimiter(self):
return [
{'word': 'nineteenth', 'start_offset': 0, 'end_offset': 2},
{'word': 'seventy eighty', 'start_offset': 5, 'end_offset': 9},
]
@property
def char_offsets_bpe(self):
char_offsets = [
{"char": "discuss", "start_offset": 0, "end_offset": 2},
{"char": "absolute", "start_offset": 2, "end_offset": 4},
{"char": "'", "start_offset": 5, "end_offset": 5},
{"char": "really", "start_offset": 5, "end_offset": 6},
{"char": "friend", "start_offset": 6, "end_offset": 7},
{"char": "ship", "start_offset": 8, "end_offset": 9},
]
return char_offsets
@property
def encoded_char_offsets_bpe(self):
char_offsets = [
{"char": "▁discuss", "start_offset": 0, "end_offset": 2},
{"char": "▁absolute", "start_offset": 2, "end_offset": 4},
{"char": "'", "start_offset": 5, "end_offset": 5},
{"char": "▁really", "start_offset": 5, "end_offset": 6},
{"char": "▁friend", "start_offset": 6, "end_offset": 7},
{"char": "ship", "start_offset": 8, "end_offset": 9},
]
return char_offsets
@property
def word_offsets_bpe_expected_output(self):
return [
{'word': "discuss", 'start_offset': 0, 'end_offset': 2},
{'word': "absolute'", 'start_offset': 2, 'end_offset': 5},
{'word': "really", 'start_offset': 5, 'end_offset': 6},
{'word': "friendship", 'start_offset': 6, 'end_offset': 9},
]
@property
def word_offsets_bpe_expected_output_other_delimiter(self):
return [
{'word': "discuss absolute'", 'start_offset': 0, 'end_offset': 5},
{'word': "friendship", 'start_offset': 6, 'end_offset': 9},
]
@staticmethod
def check_char_timestamps(hyp: Hypothesis, decoding: Any):
"""Test character-level timestamps for both CTC and RNNT"""
assert hyp.timestamp is not None
assert isinstance(hyp.timestamp, dict)
assert 'timestep' in hyp.timestamp
assert 'char' in hyp.timestamp
assert 'word' in hyp.timestamp
assert 'segment' in hyp.timestamp
hypothesis_text = re.sub(r'\s+', ' ', hyp.text.strip())
words = hyp.text.split(decoding.word_seperator)
words = list(filter(lambda x: x != '', words))
assert len(hyp.timestamp['word']) == len(words)
words_from_timestamps = [ts['word'] for ts in hyp.timestamp['word']]
assert hypothesis_text == decoding.word_seperator.join(words_from_timestamps)
segments = []
segment = []
for word in words:
segment.append(word)
if word[-1] in decoding.segment_seperators:
segments.append(' '.join(segment))
segment = []
if segment:
segments.append(' '.join(segment))
assert len(hyp.timestamp['segment']) == len(segments)
segments_from_timestamps = [ts['segment'] for ts in hyp.timestamp['segment']]
assert hypothesis_text == decoding.word_seperator.join(segments_from_timestamps)
@staticmethod
def check_subword_timestamps(hyp: Hypothesis, decoding: Any):
"""Test subword-level timestamps for both CTC and RNNT"""
assert hyp.timestamp is not None
assert isinstance(hyp.timestamp, dict)
assert 'timestep' in hyp.timestamp
assert 'char' in hyp.timestamp
assert 'word' in hyp.timestamp
assert 'segment' in hyp.timestamp
chars = list(hyp.text)
chars = list(filter(lambda x: x not in ['', ' ', '#'], chars))
all_chars = [list(decoding.tokenizer.tokens_to_text(data['char'])) for data in hyp.timestamp['char']]
all_chars = [char for subword in all_chars for char in subword]
all_chars = list(filter(lambda x: x not in ['', ' ', '#'], all_chars))
assert len(chars) == len(all_chars)
hypothesis_text = re.sub(r'\s+', ' ', hyp.text.strip())
words_from_timestamps = [ts['word'] for ts in hyp.timestamp['word']]
assert hypothesis_text == decoding.word_seperator.join(words_from_timestamps)
segments_count = sum([hyp.text.count(seperator) for seperator in decoding.segment_seperators])
if hyp.text[-1] not in decoding.segment_seperators:
segments_count += 1
if hyp.text in decoding.segment_seperators:
segments_count = 0
assert len(hyp.timestamp['segment']) == segments_count
segments_from_timestamps = [ts['segment'] for ts in hyp.timestamp['segment']]
assert hypothesis_text == decoding.word_seperator.join(segments_from_timestamps)
def test_word_offsets_chars(self):
word_offsets = get_words_offsets(
char_offsets=self.char_offsets_chars,
encoded_char_offsets=None,
word_delimiter_char=" ",
tokenizer_type="char",
supported_punctuation={'.', '!', '?'},
decode_tokens_to_str=self.decoding_char.decode_tokens_to_str,
)
assert word_offsets == self.word_offsets_chars_expected_output
def test_word_offsets_char_other_delimiter(self):
word_offsets = get_words_offsets(
char_offsets=self.char_offsets_chars,
encoded_char_offsets=None,
tokenizer_type="char",
word_delimiter_char=".",
supported_punctuation={'.', '!', '?'},
decode_tokens_to_str=self.decoding_char.decode_tokens_to_str,
)
assert word_offsets == self.word_offsets_chars_expected_output_other_delimiter
def test_word_offsets_subword_wpe(self):
word_offsets = get_words_offsets(
char_offsets=self.char_offsets_wpe,
encoded_char_offsets=None,
word_delimiter_char=" ",
tokenizer_type="wpe",
supported_punctuation={'.', '!', '?'},
decode_tokens_to_str=self.decoding_subword_wpe.decode_tokens_to_str,
)
assert word_offsets == self.word_offsets_wpe_expected_output
def test_word_offsets_subword_wpe_other_delimiter(self):
word_offsets = get_words_offsets(
char_offsets=self.char_offsets_wpe,
encoded_char_offsets=None,
word_delimiter_char="re",
tokenizer_type="wpe",
supported_punctuation={'.', '!', '?'},
decode_tokens_to_str=self.decoding_subword_wpe.decode_tokens_to_str,
)
assert word_offsets == self.word_offsets_wpe_expected_output_other_delimiter
def test_word_offsets_subword_bpe(self):
word_offsets = get_words_offsets(
char_offsets=self.char_offsets_bpe,
encoded_char_offsets=self.encoded_char_offsets_bpe,
word_delimiter_char=" ",
tokenizer_type="bpe",
supported_punctuation={'.', '!', '?'},
decode_tokens_to_str=self.decoding_subword_bpe.decode_tokens_to_str,
)
assert word_offsets == self.word_offsets_bpe_expected_output
def test_word_offsets_subword_bpe_other_delimiter(self):
word_offsets = get_words_offsets(
char_offsets=self.char_offsets_bpe,
encoded_char_offsets=self.encoded_char_offsets_bpe,
word_delimiter_char="really",
tokenizer_type="bpe",
supported_punctuation={'.', '!', '?'},
decode_tokens_to_str=self.decoding_subword_bpe.decode_tokens_to_str,
)
assert word_offsets == self.word_offsets_bpe_expected_output_other_delimiter
def test_segment_offsets_delimiter(self):
segment_offsets = get_segment_offsets(
word_offsets=self.word_offsets_chars_expected_output,
segment_delimiter_tokens=['.', '!', '?'],
supported_punctuation={'.', '!', '?'},
)
assert segment_offsets == self.segment_offsets_expected_output
def test_segment_offsets_gap(self):
segment_offsets = get_segment_offsets(
word_offsets=self.word_offsets_chars_expected_output,
segment_delimiter_tokens=[],
supported_punctuation={},
segment_gap_threshold=10,
)
assert segment_offsets == self.segment_offsets_expected_output_gap
|