@@ -15,6 +15,8 @@ class RecordingManager {
1515 var onPartialResult : ( ( String ) -> Void ) ?
1616 /// 累积的识别文字
1717 private var accumulatedText : String = " "
18+ /// 上一个语音段的结束时间(用于计算停顿时长)
19+ private var lastSegmentEndTime : Float = 0
1820 /// 用于识别的队列
1921 private let recognitionQueue = DispatchQueue ( label: " com.typeless.recognition " , qos: . userInitiated)
2022
@@ -78,6 +80,7 @@ class RecordingManager {
7880
7981 // 重置状态
8082 accumulatedText = " "
83+ lastSegmentEndTime = 0
8184 vad? . reset ( )
8285
8386 // 创建音频引擎
@@ -145,25 +148,30 @@ class RecordingManager {
145148
146149 // 检查是否有完整的语音段
147150 while vad. hasSegment ( ) {
148- if let segment = vad. popSegment ( ) {
151+ if let segment = vad. popSegmentWithTime ( ) {
149152 recognitionQueue. async { [ weak self] in
150153 self ? . transcribeSegment ( segment)
151154 }
152155 }
153156 }
154157 }
155158
156- private func transcribeSegment( _ samples : [ Float ] ) {
159+ private func transcribeSegment( _ segment : SpeechSegment ) {
157160 guard let recognizer = recognizer else { return }
158161
159- if let text = recognizer. transcribe ( samples: samples) {
162+ if let text = recognizer. transcribe ( samples: segment . samples) {
160163 DispatchQueue . main. async { [ weak self] in
161164 guard let self = self else { return }
162165
163- // 智能拼接文字(去除重叠)
164- self . accumulatedText = self . mergeTexts ( self . accumulatedText, text)
166+ // 计算与上一段的停顿时长
167+ let pauseDuration = self . lastSegmentEndTime > 0 ? segment. startTime - self . lastSegmentEndTime : 0
168+ self . lastSegmentEndTime = segment. endTime
169+
170+ // 智能拼接文字(带标点)
171+ self . accumulatedText = self . mergeTexts ( self . accumulatedText, text, pauseDuration: pauseDuration)
165172
166173 print ( " >>> 分段识别结果: \( text) " )
174+ print ( " >>> 停顿时长: \( pauseDuration) 秒 " )
167175 print ( " >>> 累积文字: \( self . accumulatedText) " )
168176
169177 // 通知 UI 更新
@@ -172,9 +180,53 @@ class RecordingManager {
172180 }
173181 }
174182
175- /// 拼接文字
176- private func mergeTexts( _ existing: String , _ new: String ) -> String {
177- return existing + new
183+ /// 根据文本内容和停顿时长决定标点
184+ private func determinePunctuation( text: String , pauseDuration: Float ) -> String {
185+ let trimmed = text. trimmingCharacters ( in: . whitespaces)
186+ guard let lastChar = trimmed. last else { return " " }
187+
188+ // 疑问词检测
189+ let questionWords : Set < Character > = [ " 吗 " , " 呢 " , " 吧 " , " 么 " , " 嘛 " ]
190+ if questionWords. contains ( lastChar) {
191+ return " ? "
192+ }
193+
194+ // 感叹词检测
195+ let exclamationWords : Set < Character > = [ " 哇 " , " 耶 " , " 啦 " ]
196+ if exclamationWords. contains ( lastChar) {
197+ return " ! "
198+ }
199+
200+ // 根据停顿时长决定
201+ return pauseDuration >= 1.0 ? " 。 " : " , "
202+ }
203+
204+ /// 拼接文字(带智能标点)
205+ private func mergeTexts( _ existing: String , _ new: String , pauseDuration: Float ) -> String {
206+ if existing. isEmpty {
207+ return new
208+ }
209+ let punctuation = determinePunctuation ( text: existing, pauseDuration: pauseDuration)
210+ return existing + punctuation + new
211+ }
212+
213+ /// 在文本末尾添加最终标点
214+ private func addFinalPunctuation( _ text: String ) -> String {
215+ let punctuationSet : Set < Character > = [ " , " , " 。 " , " ! " , " ? " , " 、 " , " ; " ]
216+ if let last = text. last, punctuationSet. contains ( last) {
217+ return text
218+ }
219+
220+ // 检查是否应该用问号
221+ let trimmed = text. trimmingCharacters ( in: . whitespaces)
222+ if let lastChar = trimmed. last {
223+ let questionWords : Set < Character > = [ " 吗 " , " 呢 " , " 吧 " , " 么 " , " 嘛 " ]
224+ if questionWords. contains ( lastChar) {
225+ return text + " ? "
226+ }
227+ }
228+
229+ return text + " 。 "
178230 }
179231
180232 func stopRecording( completion: @escaping ( String ? ) -> Void ) {
@@ -201,18 +253,24 @@ class RecordingManager {
201253 }
202254
203255 while self . vad? . hasSegment ( ) == true {
204- if let segment = self . vad? . popSegment ( ) {
205- if let text = self . recognizer? . transcribe ( samples: segment) {
256+ if let segment = self . vad? . popSegmentWithTime ( ) {
257+ if let text = self . recognizer? . transcribe ( samples: segment. samples ) {
206258 DispatchQueue . main. sync {
207- // 智能拼接文字(去除重叠)
208- self . accumulatedText = self . mergeTexts ( self . accumulatedText, text)
259+ // 计算停顿时长
260+ let pauseDuration = self . lastSegmentEndTime > 0 ? segment. startTime - self . lastSegmentEndTime : 0
261+ self . lastSegmentEndTime = segment. endTime
262+ // 智能拼接文字(带标点)
263+ self . accumulatedText = self . mergeTexts ( self . accumulatedText, text, pauseDuration: pauseDuration)
209264 }
210265 }
211266 }
212267 }
213268
214- // 返回最终结果
215- let finalText = self . accumulatedText. isEmpty ? nil : self . accumulatedText
269+ // 返回最终结果(添加末尾标点)
270+ var finalText : String ? = nil
271+ if !self . accumulatedText. isEmpty {
272+ finalText = self . addFinalPunctuation ( self . accumulatedText)
273+ }
216274 DispatchQueue . main. async {
217275 print ( " >>> 最终识别结果: \( finalText ?? " (无) " ) " )
218276 completion ( finalText)
0 commit comments