From 9be8a443d74d68f179de88fff13b4e8424579d7b Mon Sep 17 00:00:00 2001
From: zhifu gao <zhifu.gzf@alibaba-inc.com>
Date: 星期五, 10 三月 2023 18:24:39 +0800
Subject: [PATCH] Merge pull request #207 from alibaba-damo-academy/dev_dzh

---
 funasr/utils/timestamp_tools.py |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/funasr/utils/timestamp_tools.py b/funasr/utils/timestamp_tools.py
index f966aee..4a367f8 100644
--- a/funasr/utils/timestamp_tools.py
+++ b/funasr/utils/timestamp_tools.py
@@ -4,7 +4,10 @@
 import numpy as np
 from typing import Any, List, Tuple, Union
 
+
 def time_stamp_lfr6_pl(us_alphas, us_cif_peak, char_list, begin_time=0.0, end_time=None):
+    if not len(char_list):
+        return []
     START_END_THRESHOLD = 5
     TIME_RATE = 10.0 * 6 / 1000 / 3  #  3 times upsampled
     if len(us_alphas.shape) == 3:
@@ -52,3 +55,56 @@
             res.append([int(timestamp[0] * 1000), int(timestamp[1] * 1000)])
     return res
 
+
+def time_stamp_sentence(punc_id_list, time_stamp_postprocessed, text_postprocessed):
+    res = []
+    if text_postprocessed is None:
+        return res
+    if time_stamp_postprocessed is None:
+        return res
+    if len(time_stamp_postprocessed) == 0:
+        return res
+    if len(text_postprocessed) == 0:
+        return res
+    if punc_id_list is None or len(punc_id_list) == 0:
+        res.append({
+            'text': text_postprocessed.split(),
+            "start": time_stamp_postprocessed[0][0],
+            "end": time_stamp_postprocessed[-1][1]
+        })
+        return res
+    if len(punc_id_list) != len(time_stamp_postprocessed):
+        res.append({
+            'text': text_postprocessed.split(),
+            "start": time_stamp_postprocessed[0][0],
+            "end": time_stamp_postprocessed[-1][1]
+        })
+        return res
+
+    sentence_text = ''
+    sentence_start = time_stamp_postprocessed[0][0]
+    texts = text_postprocessed.split()
+    for i in range(len(punc_id_list)):
+        sentence_text += texts[i]
+        if punc_id_list[i] == 2:
+            sentence_text += ','
+            res.append({
+                'text': sentence_text,
+                "start": sentence_start,
+                "end": time_stamp_postprocessed[i][1]
+            })
+            sentence_text = ''
+            sentence_start = time_stamp_postprocessed[i][1]
+        elif punc_id_list[i] == 3:
+            sentence_text += '.'
+            res.append({
+                'text': sentence_text,
+                "start": sentence_start,
+                "end": time_stamp_postprocessed[i][1]
+            })
+            sentence_text = ''
+            sentence_start = time_stamp_postprocessed[i][1]
+    return res
+
+
+

--
Gitblit v1.9.1