@@ -57,7 +57,7 @@ func (s *Speech) Recognize(ctx context.Context, fileUri string) (sRet []*srt.Srt
5757 if err != nil {
5858 return
5959 }
60- logrus .Infof ("aliyun speech add task %s" ,fileUri )
60+ logrus .Infof ("aliyun speech add task %s" , fileUri )
6161 taskId , err = s .sendTask (client , fileUri )
6262 if err != nil {
6363 return
@@ -71,12 +71,12 @@ func (s *Speech) Recognize(ctx context.Context, fileUri string) (sRet []*srt.Srt
7171 err = errors .WithMessage (err , "recognize failed" )
7272 return
7373 }
74- logrus .Infof ("aliyun speech recognize result [%d]" ,len (rsp .Result .Sentences ))
74+ logrus .Infof ("aliyun speech recognize result [%d]" , len (rsp .Result .Sentences ))
7575 sRet , err = s .Sentence (0 , rsp )
76- wRet , err = s .BreakSentence (0 , rsp )
76+ wRet , err = s .NewBreakSentence (0 , rsp )
7777 if err != nil {
7878 data , _ := json .Marshal (rsp )
79- logrus .Errorf ("break sentence failed [%+v]" ,err )
79+ logrus .Errorf ("break sentence failed [%+v]" , err )
8080 _ = ioutil .WriteFile (fmt .Sprintf ("log/dump_%d.json" , time .Now ().Unix ()), data , os .ModePerm )
8181 err = nil
8282 }
@@ -165,48 +165,8 @@ func (s *Speech) sendTask(client *sdk.Client, URI string) (taskId string, err er
165165}
166166
167167func (s * Speech ) BreakSentence (channelId int , rsp * Response ) (ret []* srt.Srt , err error ) {
168- var (
169- newLine bool
170- idx int
171- )
172- //1. 重新断句
173- idx = 0
174- newLine = true
175- tmpSrt := & srt.Srt {}
176- for _ , sentence := range rsp .Result .Sentences {
177- //不是目标通道就过掉
178- if sentence .ChannelId != channelId {
179- continue
180- }
181- //1.1 按照空格切词
182- words := strings .Split (sentence .Text , " " )
183- //1.2 断句
184- for _ , word := range words {
185- word = strings .TrimSpace (word )
186- if word == "" {
187- continue
188- }
189- //句子结尾
190- if strings .ContainsAny (word , ",.?!,。?!" ) {
191- tmpSrt .Subtitle += " " + word
192- newLine = true
193- continue
194- }
195- //新句子开头
196- if newLine == true {
197- idx += 1
198- tmpSrt = & srt.Srt {
199- Sequence : idx ,
200- Subtitle : word ,
201- }
202- ret = append (ret , tmpSrt )
203- newLine = false
204- } else { //句子中间
205- tmpSrt .Subtitle += " " + word
206- }
207- }
208- }
209168
169+ ret = s .SplitSentence (channelId , rsp )
210170 re , _ := regexp .Compile (regexNumber )
211171 curIdx := 0
212172 for _ , itr := range ret {
@@ -218,9 +178,9 @@ func (s *Speech) BreakSentence(channelId int, rsp *Response) (ret []*srt.Srt, er
218178 }
219179
220180 sword = strings .ToLower (strings .TrimFunc (strings .TrimSpace (sword ), func (r rune ) bool {
221- if strings .ContainsRune (text .SentenceBreak ,r ) {
181+ if strings .ContainsRune (text .SentenceBreak , r ) {
222182 return true
223- }else {
183+ } else {
224184 return false
225185 }
226186 return false
@@ -310,3 +270,144 @@ func (s *Speech) Sentence(channelId int, rsp *Response) (ret []*srt.Srt, err err
310270
311271 return
312272}
273+
274+ func (s * Speech ) SplitSentence (channelId int , rsp * Response ) (ret []* srt.Srt ) {
275+ var (
276+ newLine bool
277+ idx int
278+ )
279+ //1. 重新断句
280+ idx = 0
281+ newLine = true
282+ tmpSrt := & srt.Srt {}
283+ for _ , sentence := range rsp .Result .Sentences {
284+ //不是目标通道就过掉
285+ if sentence .ChannelId != channelId {
286+ continue
287+ }
288+ //1.1 按照空格切词
289+ words := strings .Split (sentence .Text , " " )
290+ //1.2 断句
291+ for _ , word := range words {
292+ word = strings .TrimSpace (word )
293+ if word == "" {
294+ continue
295+ }
296+ //句子结尾
297+ if strings .ContainsAny (word , ",.?!,。?!" ) {
298+ tmpSrt .Subtitle += " " + word
299+ newLine = true
300+ continue
301+ }
302+ //新句子开头
303+ if newLine == true {
304+ idx += 1
305+ tmpSrt = & srt.Srt {
306+ Sequence : idx ,
307+ Subtitle : word ,
308+ }
309+ ret = append (ret , tmpSrt )
310+ newLine = false
311+ } else { //句子中间
312+ tmpSrt .Subtitle += " " + word
313+ }
314+ }
315+ }
316+
317+ return
318+ }
319+
320+ func (s * Speech ) NewBreakSentence (channelId int , rsp * Response ) (ret []* srt.Srt , err error ) {
321+ var (
322+ curIdx = 0
323+ )
324+
325+ ret = s .SplitSentence (channelId , rsp )
326+ re , _ := regexp .Compile (regexNumber )
327+ swStack := []* srt.Srt {}
328+ wStack := []* srt.Srt {}
329+
330+ for sIdx , itr := range ret {
331+ sentenceWords := strings .Split (itr .Subtitle , " " )
332+ for swIdx , sw := range sentenceWords { //句子中的词
333+
334+ //提取原始句子中的词
335+ sword := sw
336+ if strings .ContainsAny (sw , text .SentenceBreak ) {
337+ sword = strings .TrimRight (sword , text .SentenceBreak )
338+ }
339+
340+ sword = strings .ToLower (strings .TrimFunc (strings .TrimSpace (sword ), func (r rune ) bool {
341+ if strings .ContainsRune (text .SentenceBreak , r ) {
342+ return true
343+ } else {
344+ return false
345+ }
346+ return false
347+ }))
348+
349+ for wIdx := curIdx ; wIdx < len (rsp .Result .Words ); wIdx ++ {
350+ //更新curIdx
351+ if rsp .Result .Words [wIdx ].ChannelId != channelId {
352+ continue
353+ }
354+ word := strings .ToLower (strings .TrimSpace (rsp .Result .Words [wIdx ].Word ))
355+
356+ fmt .Printf ("sw:%s , w: %s info: %+v\n " , sword , word , rsp .Result .Words [wIdx ])
357+ if s .Equal (sword , word ) {
358+
359+ //句子首词匹配
360+ if swIdx == 0 {
361+ itr .Start = utils .MillisDurationConv (rsp .Result .Words [wIdx ].BeginTime )
362+ //句子尾
363+ } else if swIdx == len (sentenceWords )- 1 {
364+ itr .End = utils .MillisDurationConv (rsp .Result .Words [wIdx ].EndTime )
365+ }
366+
367+ //判断栈中是否为空
368+ if len (swStack ) > 0 {
369+ //前一个句子没有处理完
370+ if swIdx == 0 {
371+ ret [sIdx - 1 ].End = utils .MillisDurationConv (rsp .Result .Words [wIdx ].BeginTime )
372+ }
373+ //当前句子的首词被暂存
374+ if itr .Start == "" {
375+ //取暂存的中第一词
376+ itr .Start = wStack [len (wStack )- 1 ].Start
377+ }
378+ swStack = swStack [:0 ]
379+ wStack = wStack [:0 ]
380+ }
381+
382+ curIdx = wIdx + 1
383+ break //配对下一个词
384+ } else {
385+
386+ //词结果中需要暂存的情况
387+ if _ , ok := s .wellKnownNumber [word ]; ok {
388+ wStack = append (wStack , & srt.Srt {
389+ Sequence : 0 ,
390+ Start : utils .MillisDurationConv (rsp .Result .Words [wIdx ].BeginTime ),
391+ End : utils .MillisDurationConv (rsp .Result .Words [wIdx ].EndTime ),
392+ Subtitle : word ,
393+ })
394+ continue
395+ }
396+
397+ //句子中词需要暂存的情况
398+ if re .Match ([]byte (sword )) {
399+ swStack = append (swStack , & srt.Srt {
400+ Sequence : 0 ,
401+ Start : "" ,
402+ End : "" ,
403+ Subtitle : sword ,
404+ })
405+ break
406+ }
407+ }
408+ }
409+ }
410+ }
411+
412+ return
413+ }
0 commit comments