Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions funasr/models/contextual_paraformer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ def load_seg_dict(seg_dict_file):
lines = f.readlines()
for line in lines:
s = line.strip().split()
if not s:
continue
key = s[0]
value = s[1:]
seg_dict[key] = " ".join(value)
Expand Down
2 changes: 2 additions & 0 deletions funasr/models/seaco_paraformer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ def load_seg_dict(seg_dict_file):
lines = f.readlines()
for line in lines:
s = line.strip().split()
if not s:
continue
key = s[0]
value = s[1:]
seg_dict[key] = " ".join(value)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_paraformer_hotword_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,20 @@ def cp1252_default_open(file, mode="r", *args, **kwargs):
hotword_list = _generate(model_cls, tmp_path, "阿里巴巴\n阿里云\n")

assert hotword_list == [[2, 3, 4, 4], [2, 3, 5], [SOS]]


@pytest.mark.parametrize("model_cls", [SeacoParaformer, ContextualParaformer])
def test_blank_lines_in_seg_dict_are_skipped(model_cls, tmp_path):
# A seg_dict with blank or whitespace-only lines (e.g. a trailing newline
# added while editing it) used to raise IndexError while loading.
(tmp_path / "seg_dict").write_text("阿 阿\n\n里 里\n \n巴 巴\n云 云\n\n", encoding="utf-8")
frontend = types.SimpleNamespace(cmvn_file=os.path.join(str(tmp_path), "am.mvn"))

hotword_list = model_cls.generate_hotwords_list(
types.SimpleNamespace(sos=SOS),
"阿里巴巴 阿里云",
tokenizer=_Tokenizer(),
frontend=frontend,
)

assert hotword_list == [[2, 3, 4, 4], [2, 3, 5], [SOS]]
Loading