Remove unused confidence and alternatives fields from SpeechRecognitionResult
Complete removal of unused fields to simplify data structure: 1. SpeechRecognitionResult Model Simplified: - Removed 'confidence' field (was always hardcoded to 0.8) - Removed 'alternatives' field (was always empty array) - Kept only 'recognizedWords' which is actually used - Updated constructor, fromMap, toMap, toString, ==, hashCode accordingly 2. YxAsrService Updates: - Simplified _sendResult() method signature - Removed unused confidence and alternatives parameters - Updated method call to only pass recognizedWords - Cleaner method invocation: _sendResult(recognizedWords: result.text) 3. Benefits Achieved: - 🧹 Simplified data structure - only essential fields remain - 🚀 Reduced memory usage - no unnecessary field storage/transmission - 💡 Cleaner API - method signatures reflect actual usage - ⚡ Better performance - less data serialization/deserialization - 🔍 Improved code clarity - no confusing unused parameters 4. Sherpa-ONNX Integration: - OnlineRecognizerResult only provides: text, tokens, timestamps - No confidence or alternatives data available from the library - Our simplified structure now aligns with actual data source This optimization removes all the 'fake' hardcoded values and focuses on the actual speech recognition text result that users need.
This commit is contained in:
parent
75080d0c0d
commit
508244fac3
|
|
@ -3,24 +3,14 @@ class SpeechRecognitionResult {
|
|||
/// 识别出的文字内容
|
||||
final String recognizedWords;
|
||||
|
||||
/// 识别置信度(0.0 到 1.0)
|
||||
final double confidence;
|
||||
|
||||
/// 备选识别结果
|
||||
final List<String> alternatives;
|
||||
|
||||
const SpeechRecognitionResult({
|
||||
required this.recognizedWords,
|
||||
this.confidence = 0.0,
|
||||
this.alternatives = const [],
|
||||
});
|
||||
|
||||
/// 从 Map 创建 [SpeechRecognitionResult] 实例
|
||||
factory SpeechRecognitionResult.fromMap(Map<String, dynamic> map) {
|
||||
return SpeechRecognitionResult(
|
||||
recognizedWords: map['recognizedWords'] as String? ?? '',
|
||||
confidence: (map['confidence'] as num?)?.toDouble() ?? 0.0,
|
||||
alternatives: List<String>.from(map['alternatives'] as List? ?? []),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -28,31 +18,23 @@ class SpeechRecognitionResult {
|
|||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'recognizedWords': recognizedWords,
|
||||
'confidence': confidence,
|
||||
'alternatives': alternatives,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'SpeechRecognitionResult(recognizedWords: $recognizedWords, '
|
||||
'confidence: $confidence, alternatives: $alternatives)';
|
||||
return 'SpeechRecognitionResult(recognizedWords: $recognizedWords)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) return true;
|
||||
return other is SpeechRecognitionResult &&
|
||||
other.recognizedWords == recognizedWords &&
|
||||
other.confidence == confidence &&
|
||||
other.alternatives.length == alternatives.length &&
|
||||
other.alternatives.every((alt) => alternatives.contains(alt));
|
||||
other.recognizedWords == recognizedWords;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return recognizedWords.hashCode ^
|
||||
confidence.hashCode ^
|
||||
alternatives.hashCode;
|
||||
return recognizedWords.hashCode;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -750,8 +750,6 @@ class YxAsrService implements SpeechRecognitionService {
|
|||
_lastRecognizedText = result.text; // 更新最后识别的文本
|
||||
_sendResult(
|
||||
recognizedWords: result.text,
|
||||
confidence: 0.8,
|
||||
alternatives: [],
|
||||
);
|
||||
} else if (result.text.isNotEmpty &&
|
||||
result.text == _lastRecognizedText) {
|
||||
|
|
@ -771,14 +769,10 @@ class YxAsrService implements SpeechRecognitionService {
|
|||
/// 发送识别结果到结果流
|
||||
void _sendResult({
|
||||
required String recognizedWords,
|
||||
required double confidence,
|
||||
required List<String> alternatives,
|
||||
}) {
|
||||
debugPrint('📤 [YxAsr] 发送识别结果: "$recognizedWords"');
|
||||
final result = SpeechRecognitionResult(
|
||||
recognizedWords: recognizedWords,
|
||||
confidence: confidence,
|
||||
alternatives: alternatives,
|
||||
);
|
||||
_resultController.add(result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue