Add Dart API for homophone replacer (#2167)
This commit is contained in:
@@ -4,6 +4,7 @@ import 'dart:ffi';
|
||||
|
||||
export 'src/audio_tagging.dart';
|
||||
export 'src/feature_config.dart';
|
||||
export 'src/homophone_replacer_config.dart';
|
||||
export 'src/keyword_spotter.dart';
|
||||
export 'src/offline_punctuation.dart';
|
||||
export 'src/offline_recognizer.dart';
|
||||
|
||||
29
flutter/sherpa_onnx/lib/src/homophone_replacer_config.dart
Normal file
29
flutter/sherpa_onnx/lib/src/homophone_replacer_config.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2025 Xiaomi Corporation
|
||||
|
||||
class HomophoneReplacerConfig {
|
||||
const HomophoneReplacerConfig(
|
||||
{this.dictDir = '', this.lexicon = '', this.ruleFsts = ''});
|
||||
|
||||
factory HomophoneReplacerConfig.fromJson(Map<String, dynamic> json) {
|
||||
return HomophoneReplacerConfig(
|
||||
dictDir: json['dictDir'] as String? ?? '',
|
||||
lexicon: json['lexicon'] as String? ?? '',
|
||||
ruleFsts: json['ruleFsts'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'HomophoneReplacerConfig(dictDir: $dictDir, lexicon: $lexicon, ruleFsts: $ruleFsts)';
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'dictDir': dictDir,
|
||||
'lexicon': lexicon,
|
||||
'ruleFsts': ruleFsts,
|
||||
};
|
||||
|
||||
final String dictDir;
|
||||
final String lexicon;
|
||||
final String ruleFsts;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import './feature_config.dart';
|
||||
import './homophone_replacer_config.dart';
|
||||
import './offline_stream.dart';
|
||||
import './sherpa_onnx_bindings.dart';
|
||||
import './utils.dart';
|
||||
@@ -403,6 +404,7 @@ class OfflineRecognizerConfig {
|
||||
this.ruleFsts = '',
|
||||
this.ruleFars = '',
|
||||
this.blankPenalty = 0.0,
|
||||
this.hr = const HomophoneReplacerConfig(),
|
||||
});
|
||||
|
||||
factory OfflineRecognizerConfig.fromJson(Map<String, dynamic> json) {
|
||||
@@ -421,12 +423,13 @@ class OfflineRecognizerConfig {
|
||||
ruleFsts: json['ruleFsts'] as String? ?? '',
|
||||
ruleFars: json['ruleFars'] as String? ?? '',
|
||||
blankPenalty: (json['blankPenalty'] as num?)?.toDouble() ?? 0.0,
|
||||
hr: HomophoneReplacerConfig.fromJson(json['hr'] as Map<String, dynamic>),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'OfflineRecognizerConfig(feat: $feat, model: $model, lm: $lm, decodingMethod: $decodingMethod, maxActivePaths: $maxActivePaths, hotwordsFile: $hotwordsFile, hotwordsScore: $hotwordsScore, ruleFsts: $ruleFsts, ruleFars: $ruleFars, blankPenalty: $blankPenalty)';
|
||||
return 'OfflineRecognizerConfig(feat: $feat, model: $model, lm: $lm, decodingMethod: $decodingMethod, maxActivePaths: $maxActivePaths, hotwordsFile: $hotwordsFile, hotwordsScore: $hotwordsScore, ruleFsts: $ruleFsts, ruleFars: $ruleFars, blankPenalty: $blankPenalty, hr: $hr)';
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
@@ -440,6 +443,7 @@ class OfflineRecognizerConfig {
|
||||
'ruleFsts': ruleFsts,
|
||||
'ruleFars': ruleFars,
|
||||
'blankPenalty': blankPenalty,
|
||||
'hr': hr.toJson(),
|
||||
};
|
||||
|
||||
final FeatureConfig feat;
|
||||
@@ -457,6 +461,7 @@ class OfflineRecognizerConfig {
|
||||
final String ruleFars;
|
||||
|
||||
final double blankPenalty;
|
||||
final HomophoneReplacerConfig hr;
|
||||
}
|
||||
|
||||
class OfflineRecognizerResult {
|
||||
@@ -598,8 +603,15 @@ class OfflineRecognizer {
|
||||
|
||||
c.ref.blankPenalty = config.blankPenalty;
|
||||
|
||||
c.ref.hr.dictDir = config.hr.dictDir.toNativeUtf8();
|
||||
c.ref.hr.lexicon = config.hr.lexicon.toNativeUtf8();
|
||||
c.ref.hr.ruleFsts = config.hr.ruleFsts.toNativeUtf8();
|
||||
|
||||
final ptr = SherpaOnnxBindings.createOfflineRecognizer?.call(c) ?? nullptr;
|
||||
|
||||
calloc.free(c.ref.hr.dictDir);
|
||||
calloc.free(c.ref.hr.lexicon);
|
||||
calloc.free(c.ref.hr.ruleFsts);
|
||||
calloc.free(c.ref.ruleFars);
|
||||
calloc.free(c.ref.ruleFsts);
|
||||
calloc.free(c.ref.hotwordsFile);
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import './feature_config.dart';
|
||||
import './homophone_replacer_config.dart';
|
||||
import './online_stream.dart';
|
||||
import './sherpa_onnx_bindings.dart';
|
||||
import './utils.dart';
|
||||
@@ -194,6 +195,7 @@ class OnlineRecognizerConfig {
|
||||
this.ruleFsts = '',
|
||||
this.ruleFars = '',
|
||||
this.blankPenalty = 0.0,
|
||||
this.hr = const HomophoneReplacerConfig(),
|
||||
});
|
||||
|
||||
factory OnlineRecognizerConfig.fromJson(Map<String, dynamic> json) {
|
||||
@@ -217,12 +219,14 @@ class OnlineRecognizerConfig {
|
||||
ruleFsts: json['ruleFsts'] as String? ?? '',
|
||||
ruleFars: json['ruleFars'] as String? ?? '',
|
||||
blankPenalty: (json['blankPenalty'] as num?)?.toDouble() ?? 0.0,
|
||||
hr: HomophoneReplacerConfig.fromJson(
|
||||
json['hr'] as Map<String, dynamic>? ?? const {}),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'OnlineRecognizerConfig(feat: $feat, model: $model, decodingMethod: $decodingMethod, maxActivePaths: $maxActivePaths, enableEndpoint: $enableEndpoint, rule1MinTrailingSilence: $rule1MinTrailingSilence, rule2MinTrailingSilence: $rule2MinTrailingSilence, rule3MinUtteranceLength: $rule3MinUtteranceLength, hotwordsFile: $hotwordsFile, hotwordsScore: $hotwordsScore, ctcFstDecoderConfig: $ctcFstDecoderConfig, ruleFsts: $ruleFsts, ruleFars: $ruleFars, blankPenalty: $blankPenalty)';
|
||||
return 'OnlineRecognizerConfig(feat: $feat, model: $model, decodingMethod: $decodingMethod, maxActivePaths: $maxActivePaths, enableEndpoint: $enableEndpoint, rule1MinTrailingSilence: $rule1MinTrailingSilence, rule2MinTrailingSilence: $rule2MinTrailingSilence, rule3MinUtteranceLength: $rule3MinUtteranceLength, hotwordsFile: $hotwordsFile, hotwordsScore: $hotwordsScore, ctcFstDecoderConfig: $ctcFstDecoderConfig, ruleFsts: $ruleFsts, ruleFars: $ruleFars, blankPenalty: $blankPenalty, hr: $hr)';
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
@@ -240,6 +244,7 @@ class OnlineRecognizerConfig {
|
||||
'ruleFsts': ruleFsts,
|
||||
'ruleFars': ruleFars,
|
||||
'blankPenalty': blankPenalty,
|
||||
'hr': hr.toJson(),
|
||||
};
|
||||
|
||||
final FeatureConfig feat;
|
||||
@@ -265,6 +270,7 @@ class OnlineRecognizerConfig {
|
||||
final String ruleFars;
|
||||
|
||||
final double blankPenalty;
|
||||
final HomophoneReplacerConfig hr;
|
||||
}
|
||||
|
||||
class OnlineRecognizerResult {
|
||||
@@ -352,8 +358,15 @@ class OnlineRecognizer {
|
||||
|
||||
c.ref.blankPenalty = config.blankPenalty;
|
||||
|
||||
c.ref.hr.dictDir = config.hr.dictDir.toNativeUtf8();
|
||||
c.ref.hr.lexicon = config.hr.lexicon.toNativeUtf8();
|
||||
c.ref.hr.ruleFsts = config.hr.ruleFsts.toNativeUtf8();
|
||||
|
||||
final ptr = SherpaOnnxBindings.createOnlineRecognizer?.call(c) ?? nullptr;
|
||||
|
||||
calloc.free(c.ref.hr.dictDir);
|
||||
calloc.free(c.ref.hr.lexicon);
|
||||
calloc.free(c.ref.hr.ruleFsts);
|
||||
calloc.free(c.ref.ruleFars);
|
||||
calloc.free(c.ref.ruleFsts);
|
||||
calloc.free(c.ref.ctcFstDecoderConfig.graph);
|
||||
|
||||
@@ -353,6 +353,7 @@ final class SherpaOnnxOfflineRecognizerConfig extends Struct {
|
||||
|
||||
@Float()
|
||||
external double blankPenalty;
|
||||
external SherpaOnnxHomophoneReplacerConfig hr;
|
||||
}
|
||||
|
||||
final class SherpaOnnxOnlineTransducerModelConfig extends Struct {
|
||||
@@ -404,6 +405,12 @@ final class SherpaOnnxOnlineCtcFstDecoderConfig extends Struct {
|
||||
external int maxActive;
|
||||
}
|
||||
|
||||
final class SherpaOnnxHomophoneReplacerConfig extends Struct {
|
||||
external Pointer<Utf8> dictDir;
|
||||
external Pointer<Utf8> lexicon;
|
||||
external Pointer<Utf8> ruleFsts;
|
||||
}
|
||||
|
||||
final class SherpaOnnxOnlineRecognizerConfig extends Struct {
|
||||
external SherpaOnnxFeatureConfig feat;
|
||||
external SherpaOnnxOnlineModelConfig model;
|
||||
@@ -441,6 +448,7 @@ final class SherpaOnnxOnlineRecognizerConfig extends Struct {
|
||||
|
||||
@Int32()
|
||||
external int hotwordsBufSize;
|
||||
external SherpaOnnxHomophoneReplacerConfig hr;
|
||||
}
|
||||
|
||||
final class SherpaOnnxSileroVadModelConfig extends Struct {
|
||||
|
||||
Reference in New Issue
Block a user