Fangjun Kuang
Committed by GitHub

Add homonphone replacer example for Python API. (#2161)

@@ -30,7 +30,7 @@ $EXE \ @@ -30,7 +30,7 @@ $EXE \
30 --model-type=nemo_transducer \ 30 --model-type=nemo_transducer \
31 ./sherpa-onnx-nemo-transducer-giga-am-v2-russian-2025-04-19/test_wavs/example.wav 31 ./sherpa-onnx-nemo-transducer-giga-am-v2-russian-2025-04-19/test_wavs/example.wav
32 32
33 -rm sherpa-onnx-nemo-transducer-giga-am-v2-russian-2025-04-19 33 +rm -rf sherpa-onnx-nemo-transducer-giga-am-v2-russian-2025-04-19
34 34
35 35
36 log "------------------------------------------------------------------------" 36 log "------------------------------------------------------------------------"
@@ -28,7 +28,7 @@ on: @@ -28,7 +28,7 @@ on:
28 workflow_dispatch: 28 workflow_dispatch:
29 29
30 concurrency: 30 concurrency:
31 - group: aarch64-linux-gnu-shared-${{ github.ref }} 31 + group: rknn-linux-aarch64-${{ github.ref }}
32 cancel-in-progress: true 32 cancel-in-progress: true
33 33
34 jobs: 34 jobs:
@@ -107,6 +107,27 @@ def get_args(): @@ -107,6 +107,27 @@ def get_args():
107 ) 107 )
108 108
109 parser.add_argument( 109 parser.add_argument(
  110 + "--hr-dict-dir",
  111 + type=str,
  112 + default="",
  113 + help="If not empty, it is the jieba dict directory for homophone replacer",
  114 + )
  115 +
  116 + parser.add_argument(
  117 + "--hr-lexicon",
  118 + type=str,
  119 + default="",
  120 + help="If not empty, it is the lexicon.txt for homophone replacer",
  121 + )
  122 +
  123 + parser.add_argument(
  124 + "--hr-rule-fsts",
  125 + type=str,
  126 + default="",
  127 + help="If not empty, it is the replace.fst for homophone replacer",
  128 + )
  129 +
  130 + parser.add_argument(
110 "--device-name", 131 "--device-name",
111 type=str, 132 type=str,
112 required=True, 133 required=True,
@@ -159,6 +180,9 @@ def create_recognizer(args): @@ -159,6 +180,9 @@ def create_recognizer(args):
159 hotwords_file=args.hotwords_file, 180 hotwords_file=args.hotwords_file,
160 hotwords_score=args.hotwords_score, 181 hotwords_score=args.hotwords_score,
161 blank_penalty=args.blank_penalty, 182 blank_penalty=args.blank_penalty,
  183 + hr_dict_dir=args.hr_dict_dir,
  184 + hr_rule_fsts=args.hr_rule_fsts,
  185 + hr_lexicon=args.hr_lexicon,
162 ) 186 )
163 return recognizer 187 return recognizer
164 188
@@ -114,6 +114,27 @@ def get_args(): @@ -114,6 +114,27 @@ def get_args():
114 """, 114 """,
115 ) 115 )
116 116
  117 + parser.add_argument(
  118 + "--hr-dict-dir",
  119 + type=str,
  120 + default="",
  121 + help="If not empty, it is the jieba dict directory for homophone replacer",
  122 + )
  123 +
  124 + parser.add_argument(
  125 + "--hr-lexicon",
  126 + type=str,
  127 + default="",
  128 + help="If not empty, it is the lexicon.txt for homophone replacer",
  129 + )
  130 +
  131 + parser.add_argument(
  132 + "--hr-rule-fsts",
  133 + type=str,
  134 + default="",
  135 + help="If not empty, it is the replace.fst for homophone replacer",
  136 + )
  137 +
117 return parser.parse_args() 138 return parser.parse_args()
118 139
119 140
@@ -142,6 +163,9 @@ def create_recognizer(args): @@ -142,6 +163,9 @@ def create_recognizer(args):
142 hotwords_file=args.hotwords_file, 163 hotwords_file=args.hotwords_file,
143 hotwords_score=args.hotwords_score, 164 hotwords_score=args.hotwords_score,
144 blank_penalty=args.blank_penalty, 165 blank_penalty=args.blank_penalty,
  166 + hr_dict_dir=args.hr_dict_dir,
  167 + hr_rule_fsts=args.hr_rule_fsts,
  168 + hr_lexicon=args.hr_lexicon,
145 ) 169 )
146 return recognizer 170 return recognizer
147 171
@@ -123,6 +123,27 @@ def get_args(): @@ -123,6 +123,27 @@ def get_args():
123 """, 123 """,
124 ) 124 )
125 125
  126 + parser.add_argument(
  127 + "--hr-dict-dir",
  128 + type=str,
  129 + default="",
  130 + help="If not empty, it is the jieba dict directory for homophone replacer",
  131 + )
  132 +
  133 + parser.add_argument(
  134 + "--hr-lexicon",
  135 + type=str,
  136 + default="",
  137 + help="If not empty, it is the lexicon.txt for homophone replacer",
  138 + )
  139 +
  140 + parser.add_argument(
  141 + "--hr-rule-fsts",
  142 + type=str,
  143 + default="",
  144 + help="If not empty, it is the replace.fst for homophone replacer",
  145 + )
  146 +
126 return parser.parse_args() 147 return parser.parse_args()
127 148
128 149
@@ -148,6 +169,9 @@ def create_recognizer(args): @@ -148,6 +169,9 @@ def create_recognizer(args):
148 hotwords_file=args.hotwords_file, 169 hotwords_file=args.hotwords_file,
149 hotwords_score=args.hotwords_score, 170 hotwords_score=args.hotwords_score,
150 blank_penalty=args.blank_penalty, 171 blank_penalty=args.blank_penalty,
  172 + hr_dict_dir=args.hr_dict_dir,
  173 + hr_rule_fsts=args.hr_rule_fsts,
  174 + hr_lexicon=args.hr_lexicon,
151 ) 175 )
152 return recognizer 176 return recognizer
153 177
@@ -110,6 +110,26 @@ def get_args(): @@ -110,6 +110,26 @@ def get_args():
110 """, 110 """,
111 ) 111 )
112 112
  113 + parser.add_argument(
  114 + "--hr-dict-dir",
  115 + type=str,
  116 + default="",
  117 + help="If not empty, it is the jieba dict directory for homophone replacer",
  118 + )
  119 +
  120 + parser.add_argument(
  121 + "--hr-lexicon",
  122 + type=str,
  123 + default="",
  124 + help="If not empty, it is the lexicon.txt for homophone replacer",
  125 + )
  126 +
  127 + parser.add_argument(
  128 + "--hr-rule-fsts",
  129 + type=str,
  130 + default="",
  131 + help="If not empty, it is the replace.fst for homophone replacer",
  132 + )
113 133
114 return parser.parse_args() 134 return parser.parse_args()
115 135
@@ -133,6 +153,9 @@ def create_recognizer(args): @@ -133,6 +153,9 @@ def create_recognizer(args):
133 rule3_min_utterance_length=300, # it essentially disables this rule 153 rule3_min_utterance_length=300, # it essentially disables this rule
134 hotwords_file=args.hotwords_file, 154 hotwords_file=args.hotwords_file,
135 hotwords_score=args.hotwords_score, 155 hotwords_score=args.hotwords_score,
  156 + hr_dict_dir=args.hr_dict_dir,
  157 + hr_rule_fsts=args.hr_rule_fsts,
  158 + hr_lexicon=args.hr_lexicon,
136 ) 159 )
137 return recognizer 160 return recognizer
138 161
@@ -273,6 +273,27 @@ def get_args(): @@ -273,6 +273,27 @@ def get_args():
273 help="Feature dimension. Must match the one expected by the model", 273 help="Feature dimension. Must match the one expected by the model",
274 ) 274 )
275 275
  276 + parser.add_argument(
  277 + "--hr-dict-dir",
  278 + type=str,
  279 + default="",
  280 + help="If not empty, it is the jieba dict directory for homophone replacer",
  281 + )
  282 +
  283 + parser.add_argument(
  284 + "--hr-lexicon",
  285 + type=str,
  286 + default="",
  287 + help="If not empty, it is the lexicon.txt for homophone replacer",
  288 + )
  289 +
  290 + parser.add_argument(
  291 + "--hr-rule-fsts",
  292 + type=str,
  293 + default="",
  294 + help="If not empty, it is the replace.fst for homophone replacer",
  295 + )
  296 +
276 return parser.parse_args() 297 return parser.parse_args()
277 298
278 299
@@ -312,6 +333,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer: @@ -312,6 +333,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer:
312 decoding_method=args.decoding_method, 333 decoding_method=args.decoding_method,
313 blank_penalty=args.blank_penalty, 334 blank_penalty=args.blank_penalty,
314 debug=args.debug, 335 debug=args.debug,
  336 + hr_dict_dir=args.hr_dict_dir,
  337 + hr_rule_fsts=args.hr_rule_fsts,
  338 + hr_lexicon=args.hr_lexicon,
315 ) 339 )
316 elif args.paraformer: 340 elif args.paraformer:
317 assert len(args.sense_voice) == 0, args.sense_voice 341 assert len(args.sense_voice) == 0, args.sense_voice
@@ -334,6 +358,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer: @@ -334,6 +358,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer:
334 feature_dim=args.feature_dim, 358 feature_dim=args.feature_dim,
335 decoding_method=args.decoding_method, 359 decoding_method=args.decoding_method,
336 debug=args.debug, 360 debug=args.debug,
  361 + hr_dict_dir=args.hr_dict_dir,
  362 + hr_rule_fsts=args.hr_rule_fsts,
  363 + hr_lexicon=args.hr_lexicon,
337 ) 364 )
338 elif args.sense_voice: 365 elif args.sense_voice:
339 assert len(args.whisper_encoder) == 0, args.whisper_encoder 366 assert len(args.whisper_encoder) == 0, args.whisper_encoder
@@ -352,6 +379,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer: @@ -352,6 +379,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer:
352 num_threads=args.num_threads, 379 num_threads=args.num_threads,
353 use_itn=True, 380 use_itn=True,
354 debug=args.debug, 381 debug=args.debug,
  382 + hr_dict_dir=args.hr_dict_dir,
  383 + hr_rule_fsts=args.hr_rule_fsts,
  384 + hr_lexicon=args.hr_lexicon,
355 ) 385 )
356 elif args.whisper_encoder: 386 elif args.whisper_encoder:
357 assert_file_exists(args.whisper_encoder) 387 assert_file_exists(args.whisper_encoder)
@@ -373,6 +403,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer: @@ -373,6 +403,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer:
373 language=args.whisper_language, 403 language=args.whisper_language,
374 task=args.whisper_task, 404 task=args.whisper_task,
375 tail_paddings=args.whisper_tail_paddings, 405 tail_paddings=args.whisper_tail_paddings,
  406 + hr_dict_dir=args.hr_dict_dir,
  407 + hr_rule_fsts=args.hr_rule_fsts,
  408 + hr_lexicon=args.hr_lexicon,
376 ) 409 )
377 elif args.moonshine_preprocessor: 410 elif args.moonshine_preprocessor:
378 assert_file_exists(args.moonshine_preprocessor) 411 assert_file_exists(args.moonshine_preprocessor)
@@ -389,6 +422,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer: @@ -389,6 +422,9 @@ def create_recognizer(args) -> sherpa_onnx.OfflineRecognizer:
389 num_threads=args.num_threads, 422 num_threads=args.num_threads,
390 decoding_method=args.decoding_method, 423 decoding_method=args.decoding_method,
391 debug=args.debug, 424 debug=args.debug,
  425 + hr_dict_dir=args.hr_dict_dir,
  426 + hr_rule_fsts=args.hr_rule_fsts,
  427 + hr_lexicon=args.hr_lexicon,
392 ) 428 )
393 else: 429 else:
394 raise ValueError("Please specify at least one model") 430 raise ValueError("Please specify at least one model")