Add JavaScript API (WASM) for homophone replacer (#2157)
This commit is contained in:
@@ -63,6 +63,10 @@ function freeConfig(config, Module) {
|
||||
freeConfig(config.ctcFstDecoder, Module)
|
||||
}
|
||||
|
||||
if ('hr' in config) {
|
||||
freeConfig(config.hr, Module)
|
||||
}
|
||||
|
||||
Module._free(config.ptr);
|
||||
}
|
||||
|
||||
@@ -281,6 +285,34 @@ function initSherpaOnnxFeatureConfig(config, Module) {
|
||||
return {ptr: ptr, len: len};
|
||||
}
|
||||
|
||||
function initSherpaOnnxHomophoneReplacerConfig(config, Module) {
|
||||
const len = 3 * 4;
|
||||
const ptr = Module._malloc(len);
|
||||
|
||||
const dictDirLen = Module.lengthBytesUTF8(config.dictDir || '') + 1;
|
||||
const lexiconLen = Module.lengthBytesUTF8(config.lexicon || '') + 1;
|
||||
const ruleFstsLen = Module.lengthBytesUTF8(config.ruleFsts || '') + 1;
|
||||
|
||||
const bufferLen = dictDirLen + lexiconLen + ruleFstsLen;
|
||||
|
||||
const buffer = Module._malloc(bufferLen);
|
||||
let offset = 0
|
||||
Module.stringToUTF8(config.dictDir || '', buffer + offset, dictDirLen);
|
||||
offset += dictDirLen;
|
||||
|
||||
Module.stringToUTF8(config.lexicon || '', buffer + offset, lexiconLen);
|
||||
offset += lexiconLen;
|
||||
|
||||
Module.stringToUTF8(config.ruleFsts || '', buffer + offset, ruleFstsLen);
|
||||
offset += ruleFstsLen;
|
||||
|
||||
Module.setValue(ptr, buffer, 'i8*');
|
||||
Module.setValue(ptr + 4, buffer + dictDirLen, 'i8*');
|
||||
Module.setValue(ptr + 8, buffer + dictDirLen + lexiconLen, 'i8*');
|
||||
|
||||
return {ptr: ptr, len: len, buffer: buffer};
|
||||
}
|
||||
|
||||
function initSherpaOnnxOnlineCtcFstDecoderConfig(config, Module) {
|
||||
const len = 2 * 4;
|
||||
const ptr = Module._malloc(len);
|
||||
@@ -317,12 +349,21 @@ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
||||
config.hotwordsBufSize = 0;
|
||||
}
|
||||
|
||||
if (!('hr' in config)) {
|
||||
config.hr = {
|
||||
dictDir: '',
|
||||
lexicon: '',
|
||||
ruleFsts: '',
|
||||
};
|
||||
}
|
||||
|
||||
const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
|
||||
const model = initSherpaOnnxOnlineModelConfig(config.modelConfig, Module);
|
||||
const ctcFstDecoder = initSherpaOnnxOnlineCtcFstDecoderConfig(
|
||||
config.ctcFstDecoderConfig, Module)
|
||||
const hr = initSherpaOnnxHomophoneReplacerConfig(config.hr, Module);
|
||||
|
||||
const len = feat.len + model.len + 8 * 4 + ctcFstDecoder.len + 5 * 4;
|
||||
const len = feat.len + model.len + 8 * 4 + ctcFstDecoder.len + 5 * 4 + hr.len;
|
||||
const ptr = Module._malloc(len);
|
||||
|
||||
let offset = 0;
|
||||
@@ -411,9 +452,12 @@ function initSherpaOnnxOnlineRecognizerConfig(config, Module) {
|
||||
Module.setValue(ptr + offset, config.hotwordsBufSize || 0, 'i32');
|
||||
offset += 4;
|
||||
|
||||
Module._CopyHeap(hr.ptr, hr.len, ptr + offset);
|
||||
offset += hr.len;
|
||||
|
||||
return {
|
||||
buffer: buffer, ptr: ptr, len: len, feat: feat, model: model,
|
||||
ctcFstDecoder: ctcFstDecoder
|
||||
ctcFstDecoder: ctcFstDecoder, hr: hr,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -989,11 +1033,20 @@ function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
||||
};
|
||||
}
|
||||
|
||||
if (!('hr' in config)) {
|
||||
config.hr = {
|
||||
dictDir: '',
|
||||
lexicon: '',
|
||||
ruleFsts: '',
|
||||
};
|
||||
}
|
||||
|
||||
const feat = initSherpaOnnxFeatureConfig(config.featConfig, Module);
|
||||
const model = initSherpaOnnxOfflineModelConfig(config.modelConfig, Module);
|
||||
const lm = initSherpaOnnxOfflineLMConfig(config.lmConfig, Module);
|
||||
const hr = initSherpaOnnxHomophoneReplacerConfig(config.hr, Module);
|
||||
|
||||
const len = feat.len + model.len + lm.len + 7 * 4;
|
||||
const len = feat.len + model.len + lm.len + 7 * 4 + hr.len;
|
||||
const ptr = Module._malloc(len);
|
||||
|
||||
let offset = 0;
|
||||
@@ -1056,8 +1109,12 @@ function initSherpaOnnxOfflineRecognizerConfig(config, Module) {
|
||||
Module.setValue(ptr + offset, config.blankPenalty || 0, 'float');
|
||||
offset += 4;
|
||||
|
||||
Module._CopyHeap(hr.ptr, hr.len, ptr + offset);
|
||||
offset += hr.len;
|
||||
|
||||
return {
|
||||
buffer: buffer, ptr: ptr, len: len, feat: feat, model: model, lm: lm
|
||||
buffer: buffer, ptr: ptr, len: len, feat: feat, model: model, lm: lm,
|
||||
hr: hr,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,8 @@ static_assert(sizeof(SherpaOnnxOnlineCtcFstDecoderConfig) == 2 * 4, "");
|
||||
static_assert(sizeof(SherpaOnnxOnlineRecognizerConfig) ==
|
||||
sizeof(SherpaOnnxFeatureConfig) +
|
||||
sizeof(SherpaOnnxOnlineModelConfig) + 8 * 4 +
|
||||
sizeof(SherpaOnnxOnlineCtcFstDecoderConfig) + 5 * 4,
|
||||
sizeof(SherpaOnnxOnlineCtcFstDecoderConfig) + 5 * 4 +
|
||||
sizeof(SherpaOnnxHomophoneReplacerConfig),
|
||||
"");
|
||||
|
||||
void MyPrint(SherpaOnnxOnlineRecognizerConfig *config) {
|
||||
@@ -82,6 +83,11 @@ void MyPrint(SherpaOnnxOnlineRecognizerConfig *config) {
|
||||
fprintf(stdout, "graph: %s\n", config->ctc_fst_decoder_config.graph);
|
||||
fprintf(stdout, "max_active: %d\n",
|
||||
config->ctc_fst_decoder_config.max_active);
|
||||
|
||||
fprintf(stdout, "----------hr config----------\n");
|
||||
fprintf(stdout, "dict_dir: %s\n", config->hr.dict_dir);
|
||||
fprintf(stdout, "lexicon: %s\n", config->hr.lexicon);
|
||||
fprintf(stdout, "rule_fsts: %s\n", config->hr.rule_fsts);
|
||||
}
|
||||
|
||||
void CopyHeap(const char *src, int32_t num_bytes, char *dst) {
|
||||
|
||||
@@ -38,7 +38,8 @@ static_assert(sizeof(SherpaOnnxFeatureConfig) == 2 * 4, "");
|
||||
static_assert(sizeof(SherpaOnnxOfflineRecognizerConfig) ==
|
||||
sizeof(SherpaOnnxFeatureConfig) +
|
||||
sizeof(SherpaOnnxOfflineLMConfig) +
|
||||
sizeof(SherpaOnnxOfflineModelConfig) + 7 * 4,
|
||||
sizeof(SherpaOnnxOfflineModelConfig) + 7 * 4 +
|
||||
sizeof(SherpaOnnxHomophoneReplacerConfig),
|
||||
"");
|
||||
|
||||
void PrintOfflineTtsConfig(SherpaOnnxOfflineTtsConfig *tts_config) {
|
||||
@@ -137,6 +138,10 @@ void PrintOfflineRecognizerConfig(SherpaOnnxOfflineRecognizerConfig *config) {
|
||||
fprintf(stdout, "rule_fsts: %s\n", config->rule_fsts);
|
||||
fprintf(stdout, "rule_fars: %s\n", config->rule_fars);
|
||||
fprintf(stdout, "blank_penalty: %f\n", config->blank_penalty);
|
||||
fprintf(stdout, "----------hr config----------\n");
|
||||
fprintf(stdout, "dict_dir: %s\n", config->hr.dict_dir);
|
||||
fprintf(stdout, "lexicon: %s\n", config->hr.lexicon);
|
||||
fprintf(stdout, "rule_fsts: %s\n", config->hr.rule_fsts);
|
||||
}
|
||||
|
||||
void CopyHeap(const char *src, int32_t num_bytes, char *dst) {
|
||||
|
||||
Reference in New Issue
Block a user