@@ -50,6 +50,7 @@ final class PostProcessingPipeline {
5050 func process( rawText: String , engine: ( any ASREngine ) ? , completion: @escaping ( String ? ) -> Void ) {
5151 processingQueue. async { [ weak self] in
5252 guard let self = self else { return }
53+ let pipelineStart = ContinuousClock . now
5354
5455 guard !rawText. isEmpty else {
5556 logger. info ( " 最终识别结果: (无) " )
@@ -58,26 +59,53 @@ final class PostProcessingPipeline {
5859 }
5960
6061 // Stage 1: Term normalization (domain-specific)
62+ let termNormStart = ContinuousClock . now
6163 let normalizedText = self . termNormalizer? . normalize ( rawText) ?? rawText
62- if normalizedText != rawText {
64+ let termNormMs = AnalyticsService . elapsedMs ( since: termNormStart)
65+ let termNormChanged = normalizedText != rawText
66+ if termNormChanged {
6367 logger. info ( " TermNormalizer: \( rawText, privacy: . public) → \( normalizedText, privacy: . public) " )
6468 }
6569
6670 // Stage 2: ITN (inverse text normalization) for engines that need it
6771 var processedText = normalizedText
72+ let itnApplied : Bool
73+ let itnMs : Int
6874 if let engine = engine, engine. needsITN, let itn = self . itn {
75+ let itnStart = ContinuousClock . now
76+ let before = processedText
6977 processedText = itn. normalize ( text: processedText)
70- if processedText != normalizedText {
78+ itnMs = AnalyticsService . elapsedMs ( since: itnStart)
79+ itnApplied = processedText != before
80+ if itnApplied {
7181 logger. info ( " ITN: \( normalizedText, privacy: . public) → \( processedText, privacy: . public) " )
7282 }
83+ } else {
84+ itnApplied = false
85+ itnMs = 0
7386 }
7487
7588 // Branch: engines that don't need punctuation skip CSC + punctuation
7689 guard let engine = engine, engine. needsPunctuation else {
7790 Task { [ weak self] in
7891 guard let self else { return }
92+ let cloudRewriteStart = ContinuousClock . now
7993 let rewrittenText = await self . cloudRewriteService. rewriteOrPassthrough ( processedText)
94+ let cloudRewriteMs = AnalyticsService . elapsedMs ( since: cloudRewriteStart)
8095 self . processingQueue. async {
96+ let totalMs = AnalyticsService . elapsedMs ( since: pipelineStart)
97+ AnalyticsService . track ( " PostProcessing.Completed " , parameters: [
98+ " itnApplied " : " \( itnApplied) " ,
99+ " cscApplied " : " false " ,
100+ " punctuationApplied " : " false " ,
101+ " totalLatencyMs " : " \( totalMs) " ,
102+ " termNormLatencyMs " : " \( termNormMs) " ,
103+ " itnLatencyMs " : " \( itnMs) " ,
104+ " cscLatencyMs " : " 0 " ,
105+ " punctLatencyMs " : " 0 " ,
106+ " cloudRewriteLatencyMs " : " \( cloudRewriteMs) " ,
107+ " termNormChanged " : " \( termNormChanged) " ,
108+ ] )
81109 logger. info ( " 最终结果: \( rewrittenText, privacy: . public) " )
82110 completion ( rewrittenText)
83111 }
@@ -86,23 +114,44 @@ final class PostProcessingPipeline {
86114 }
87115
88116 // Stage 3: CSC (Chinese spelling correction)
117+ let cscStart = ContinuousClock . now
89118 let correctedText = self . corrector? . correctSpelling ( processedText) ?? processedText
119+ let cscMs = AnalyticsService . elapsedMs ( since: cscStart)
120+ let cscApplied = correctedText != processedText
90121 logger. info ( " 原始文本: \( processedText, privacy: . public) " )
91- if correctedText != processedText {
122+ if cscApplied {
92123 logger. info ( " CSC 纠正: \( correctedText, privacy: . public) " )
93124 } else {
94125 logger. info ( " CSC 未修改文本 " )
95126 }
96127
97128 // Stage 4: Punctuation
129+ let punctStart = ContinuousClock . now
98130 let punctuatedText = self . punctuator? . addPunctuation ( text: correctedText) ?? correctedText
131+ let punctMs = AnalyticsService . elapsedMs ( since: punctStart)
132+ let punctuationApplied = punctuatedText != correctedText
99133 logger. info ( " 标点处理后: \( punctuatedText, privacy: . public) " )
100134
101135 // Stage 5: Cloud rewrite
102136 Task { [ weak self] in
103137 guard let self else { return }
138+ let cloudRewriteStart = ContinuousClock . now
104139 let rewrittenText = await self . cloudRewriteService. rewriteOrPassthrough ( punctuatedText)
140+ let cloudRewriteMs = AnalyticsService . elapsedMs ( since: cloudRewriteStart)
105141 self . processingQueue. async {
142+ let totalMs = AnalyticsService . elapsedMs ( since: pipelineStart)
143+ AnalyticsService . track ( " PostProcessing.Completed " , parameters: [
144+ " itnApplied " : " \( itnApplied) " ,
145+ " cscApplied " : " \( cscApplied) " ,
146+ " punctuationApplied " : " \( punctuationApplied) " ,
147+ " totalLatencyMs " : " \( totalMs) " ,
148+ " termNormLatencyMs " : " \( termNormMs) " ,
149+ " itnLatencyMs " : " \( itnMs) " ,
150+ " cscLatencyMs " : " \( cscMs) " ,
151+ " punctLatencyMs " : " \( punctMs) " ,
152+ " cloudRewriteLatencyMs " : " \( cloudRewriteMs) " ,
153+ " termNormChanged " : " \( termNormChanged) " ,
154+ ] )
106155 completion ( rewrittenText)
107156 }
108157 }
0 commit comments