Add JavaScript API (node-addon) for homophone replacer (#2158)

This commit is contained in:
Fangjun Kuang
2025-04-28 20:52:42 +08:00
committed by GitHub
parent a0aef1f6cd
commit 9d25c90a59
7 changed files with 214 additions and 0 deletions

View File

@@ -9,6 +9,7 @@
// defined in ./streaming-asr.cc
SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj);
SherpaOnnxHomophoneReplacerConfig GetHomophoneReplacerConfig(Napi::Object obj);
static SherpaOnnxOfflineTransducerModelConfig GetOfflineTransducerModelConfig(
Napi::Object obj) {
@@ -261,6 +262,7 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) {
c.feat_config = GetFeatureConfig(o);
c.model_config = GetOfflineModelConfig(o);
c.lm_config = GetOfflineLMConfig(o);
c.hr = GetHomophoneReplacerConfig(o);
SHERPA_ONNX_ASSIGN_ATTR_STR(decoding_method, decodingMethod);
SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths);
@@ -324,6 +326,9 @@ CreateOfflineRecognizerWrapper(const Napi::CallbackInfo &info) {
SHERPA_ONNX_DELETE_C_STR(c.hotwords_file);
SHERPA_ONNX_DELETE_C_STR(c.rule_fsts);
SHERPA_ONNX_DELETE_C_STR(c.rule_fars);
SHERPA_ONNX_DELETE_C_STR(c.hr.dict_dir);
SHERPA_ONNX_DELETE_C_STR(c.hr.lexicon);
SHERPA_ONNX_DELETE_C_STR(c.hr.rule_fsts);
if (!recognizer) {
Napi::TypeError::New(env, "Please check your config!")

View File

@@ -144,6 +144,24 @@ static SherpaOnnxOnlineCtcFstDecoderConfig GetCtcFstDecoderConfig(
return c;
}
// Also used in ./non-streaming-asr.cc
SherpaOnnxHomophoneReplacerConfig GetHomophoneReplacerConfig(Napi::Object obj) {
SherpaOnnxHomophoneReplacerConfig c;
memset(&c, 0, sizeof(c));
if (!obj.Has("hr") || !obj.Get("hr").IsObject()) {
return c;
}
Napi::Object o = obj.Get("hr").As<Napi::Object>();
SHERPA_ONNX_ASSIGN_ATTR_STR(dict_dir, dictDir);
SHERPA_ONNX_ASSIGN_ATTR_STR(lexicon, lexicon);
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fsts, ruleFsts);
return c;
}
static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
@@ -179,6 +197,7 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
memset(&c, 0, sizeof(c));
c.feat_config = GetFeatureConfig(o);
c.model_config = GetOnlineModelConfig(o);
c.hr = GetHomophoneReplacerConfig(o);
SHERPA_ONNX_ASSIGN_ATTR_STR(decoding_method, decodingMethod);
SHERPA_ONNX_ASSIGN_ATTR_INT32(max_active_paths, maxActivePaths);
@@ -243,6 +262,10 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
SHERPA_ONNX_DELETE_C_STR(c.hotwords_buf);
SHERPA_ONNX_DELETE_C_STR(c.ctc_fst_decoder_config.graph);
SHERPA_ONNX_DELETE_C_STR(c.hr.dict_dir);
SHERPA_ONNX_DELETE_C_STR(c.hr.lexicon);
SHERPA_ONNX_DELETE_C_STR(c.hr.rule_fsts);
if (!recognizer) {
Napi::TypeError::New(env, "Please check your config!")
.ThrowAsJavaScriptException();