@@ -68,6 +68,42 @@ def _join_vad_texts(texts):
6868 return joined
6969
7070
71+ def _vad_segment_sentences (restored_data , vadsegments ):
72+ """Build readable sentence records directly from VAD-aligned ASR chunks."""
73+ sentences = []
74+ for result , vadsegment in zip (restored_data , vadsegments ):
75+ text = re .sub (r"<\|[^|]*\|>" , "" , str (result .get ("text" , "" ))).strip ()
76+ if not text :
77+ continue
78+
79+ timestamps = []
80+ raw_timestamps = result .get ("timestamp" )
81+ if raw_timestamps is None :
82+ raw_timestamps = result .get ("timestamps" , [])
83+ for item in raw_timestamps or []:
84+ if isinstance (item , dict ):
85+ start = item .get ("start_time" )
86+ end = item .get ("end_time" )
87+ if start is None or end is None :
88+ continue
89+ timestamps .append ([int (float (start ) * 1000 ), int (float (end ) * 1000 )])
90+ elif isinstance (item , (list , tuple )) and len (item ) >= 2 :
91+ timestamps .append ([int (item [0 ]), int (item [1 ])])
92+
93+ start = timestamps [0 ][0 ] if timestamps else vadsegment [0 ]
94+ end = timestamps [- 1 ][1 ] if timestamps else vadsegment [1 ]
95+ sentences .append (
96+ {
97+ "start" : start ,
98+ "end" : end ,
99+ "text" : text ,
100+ "sentence" : text ,
101+ "timestamp" : timestamps ,
102+ }
103+ )
104+ return sentences
105+
106+
71107def _get_punc_tokens (text , punc_array , punc_model ):
72108 """Return the surface tokens represented by a CT-Transformer punctuation array."""
73109 try :
@@ -1018,6 +1054,7 @@ def inference_with_vad(self, input, input_len=None, **cfg):
10181054
10191055 timestamp_text = punc_input_text
10201056 sentence_timestamps = result .get ("timestamp" , [])
1057+ punc_alignment_failed = False
10211058 if punc_res is not None :
10221059 try :
10231060 punc_length = len (punc_array )
@@ -1038,6 +1075,7 @@ def inference_with_vad(self, input, input_len=None, **cfg):
10381075 timestamp_text , sentence_timestamps = merged_units
10391076 if punc_array is not None and punc_length != len (sentence_timestamps ):
10401077 punc_array = None
1078+ punc_alignment_failed = True
10411079 surface_sentence_list = None
10421080 if aligned_word_text is not None and punc_array is not None :
10431081 surface_sentence_list = _timestamp_sentences_from_surface (
@@ -1131,25 +1169,17 @@ def inference_with_vad(self, input, input_len=None, **cfg):
11311169 if not len (result ["text" ].strip ()):
11321170 sentence_list = []
11331171 elif self .punc_model is None and punc_res is None and not sentence_timestamps :
1134- sentence_list = []
1135- for rest , vadsegment in zip (restored_data , vadsegments ):
1136- text = str (rest .get ("text" , "" )).strip ()
1137- if not text :
1138- continue
1139- sentence_list .append (
1140- {
1141- "start" : vadsegment [0 ],
1142- "end" : vadsegment [1 ],
1143- "text" : text ,
1144- "sentence" : text ,
1145- "timestamp" : [],
1146- }
1147- )
1172+ sentence_list = _vad_segment_sentences (restored_data , vadsegments )
11481173 elif punc_res is None :
11491174 logging .warning (
11501175 "punc_model is required for sentence_timestamp, skipping sentence segmentation."
11511176 )
11521177 sentence_list = []
1178+ elif punc_alignment_failed :
1179+ logging .warning (
1180+ "punctuation timestamps could not be aligned, falling back to VAD segments."
1181+ )
1182+ sentence_list = _vad_segment_sentences (restored_data , vadsegments )
11531183 elif surface_sentence_list is not None :
11541184 sentence_list = surface_sentence_list
11551185 else :
0 commit comments