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:
Max 2025-09-09 17:03:43 +08:00
parent 75080d0c0d
commit 508244fac3
2 changed files with 3 additions and 27 deletions

View File

@ -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;
}
}

View File

@ -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);
}