mirror of
https://github.com/modelscope/FunASR.git
synced 2026-10-12 02:19:18 +00:00
fix: guard empty English timestamp segments
Avoid indexing empty sentence buffers when punctuation and timestamp entries outnumber split words, and add regression coverage for whitespace-only input. Co-authored-by: Amir Fathi <[email protected]> Signed-off-by: LauraGPT <[email protected]>
This commit is contained in:
@@ -281,16 +281,16 @@ def timestamp_sentence_en(
|
||||
|
||||
punc_id = int(punc_id) if punc_id is not None else 1
|
||||
sentence_end = timestamp[1] if timestamp is not None else sentence_end
|
||||
sentence_text = sentence_text[1:] if sentence_text[0] == ' ' else sentence_text
|
||||
if sentence_text.startswith(" "):
|
||||
sentence_text = sentence_text[1:]
|
||||
if is_sentence_start:
|
||||
sentence_start = timestamp[0] if timestamp is not None else sentence_start
|
||||
is_sentence_start = False
|
||||
if punc_id > 1:
|
||||
is_sentence_start = True
|
||||
sentence_text += punc_list[punc_id - 2]
|
||||
sentence_text_seg = (
|
||||
sentence_text_seg[:-1] if sentence_text_seg[-1] == " " else sentence_text_seg
|
||||
)
|
||||
if sentence_text_seg.endswith(" "):
|
||||
sentence_text_seg = sentence_text_seg[:-1]
|
||||
if return_raw_text:
|
||||
res.append(
|
||||
{
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
from funasr.utils.timestamp_tools import timestamp_sentence_en
|
||||
|
||||
|
||||
def test_timestamp_sentence_en_handles_whitespace_only_segment():
|
||||
result = timestamp_sentence_en(
|
||||
punc_id_list=[3],
|
||||
timestamp_postprocessed=[[0, 100]],
|
||||
text_postprocessed=" ",
|
||||
return_raw_text=True,
|
||||
)
|
||||
|
||||
assert result == [
|
||||
{
|
||||
"text": ".",
|
||||
"start": 0,
|
||||
"end": 100,
|
||||
"timestamp": [[0, 100]],
|
||||
"raw_text": "",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_timestamp_sentence_en_preserves_normal_sentence_output():
|
||||
result = timestamp_sentence_en(
|
||||
punc_id_list=[1, 3],
|
||||
timestamp_postprocessed=[[0, 100], [100, 220]],
|
||||
text_postprocessed="hello world",
|
||||
return_raw_text=True,
|
||||
)
|
||||
|
||||
assert result == [
|
||||
{
|
||||
"text": "hello world.",
|
||||
"start": 0,
|
||||
"end": 220,
|
||||
"timestamp": [[0, 100], [100, 220]],
|
||||
"raw_text": "hello world",
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user