Skip to content

Commit 8d3db4c

Browse files
authored
fix(postprocess): keep symbol token timestamps aligned (#3929)
Keep word timestamps aligned when sentence postprocessing encounters symbol tokens across core, ONNX, and Torch implementations. Independent current-main validation: base 13 tests produced 4 failures and 10 errors; head 13/13 and related 50/50 passed. Exact-head hosted postprocessing, ONNX, Torch, and runtime-eval jobs passed. No model inference or production service was used. Signed-off-by: LauraGPT <zhifu.gzf@alibaba-inc.com>
2 parents dce4ec4 + 920f663 commit 8d3db4c

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎funasr/utils/postprocess_utils.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ def sentence_postprocess(words: List[Any], time_stamp: List[List] = None):
267267
ts_lists.append([begin, end])
268268
begin = end
269269
else:
270+
if alpha_blank is True:
271+
word_lists.pop()
270272
word_lists.append(ch)
273+
alpha_blank = False
274+
if time_stamp is not None:
275+
ts_lists.append([time_stamp[i][0], time_stamp[i][1]])
271276

272277
if time_stamp is not None:
273278
word_lists, ts_lists = abbr_dispose(word_lists, ts_lists)

‎runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,12 @@ def sentence_postprocess(words: List[Any], time_stamp: List[List] = None):
234234
ts_lists.append([begin, end])
235235
begin = end
236236
else:
237-
raise ValueError("invalid character: {}".format(ch))
237+
if alpha_blank is True:
238+
word_lists.pop()
239+
word_lists.append(ch)
240+
alpha_blank = False
241+
if time_stamp is not None:
242+
ts_lists.append([time_stamp[i][0], time_stamp[i][1]])
238243

239244
if time_stamp is not None:
240245
word_lists, ts_lists = abbr_dispose(word_lists, ts_lists)

‎runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,12 @@ def sentence_postprocess(words: List[Any], time_stamp: List[List] = None):
236236
ts_lists.append([begin, end])
237237
begin = end
238238
else:
239-
raise ValueError("invalid character: {}".format(ch))
239+
if alpha_blank is True:
240+
word_lists.pop()
241+
word_lists.append(ch)
242+
alpha_blank = False
243+
if time_stamp is not None:
244+
ts_lists.append([time_stamp[i][0], time_stamp[i][1]])
240245

241246
if time_stamp is not None:
242247
word_lists, ts_lists = abbr_dispose(word_lists, ts_lists)

‎tests/test_sentence_postprocess_timestamps.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,38 @@ def test_spelled_out_letters_are_one_word_without_timestamps(self):
116116
("看CCTV新闻", ["看", "CCTV", "新", "闻"]),
117117
)
118118

119+
def test_symbol_tokens_keep_their_own_timestamp(self):
120+
self.assert_timestamps(
121+
["价", "格", "是", "3", ".", "5", "元", "好"],
122+
["价", "格", "是", "3", ".", "5", "元", "好"],
123+
[[i * 100, (i + 1) * 100] for i in range(8)],
124+
)
125+
self.assert_timestamps(
126+
["ok", ".", "好"],
127+
["ok", ".", "好"],
128+
[[0, 100], [100, 200], [200, 300]],
129+
)
130+
self.assert_timestamps(
131+
["x", "y", ".", "好"],
132+
["XY", ".", "好"],
133+
[[0, 200], [200, 300], [300, 400]],
134+
)
135+
136+
def test_spelled_out_letters_after_a_symbol_token_keep_their_timestamp(self):
137+
self.assert_timestamps(
138+
[".", "x", "y"],
139+
[".", "XY"],
140+
[[0, 100], [100, 300]],
141+
)
142+
143+
def test_symbol_tokens_are_accepted_without_timestamps(self):
144+
for path in POSTPROCESS_UTILS:
145+
with self.subTest(path=path):
146+
self.assertEqual(
147+
_load_sentence_postprocess(path)(["价", "格", "3", ".", "5", "元"]),
148+
("价格3.5元", ["价", "格", "3", ".", "5", "元"]),
149+
)
150+
119151

120152
if __name__ == "__main__":
121153
unittest.main()

0 commit comments

Comments
 (0)