Skip to content

Commit c26848f

Browse files
committed
fix: repair KWS symbol cleanup regex
1 parent 3b608cf commit c26848f

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

funasr/utils/kws_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List, Optional, Tuple
88

99

10-
symbol_str = '[’!"#$%&\'()*+,-./:;<>=?@,。?★、…【】《》?“”‘’![\\]^_`{|}~\s]+'
10+
symbol_str = r'''[’!"#$%&'()*+,\-./:;<>=?@,。?★、…【】《》?“”‘’!\[\]\\^_`{|}~\s]+'''
1111

1212

1313
def split_mixed_label(input_str):

tests/run_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
from fnmatch import fnmatch
88

99

10+
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
11+
if PROJECT_ROOT not in sys.path:
12+
sys.path.insert(0, PROJECT_ROOT)
13+
14+
1015
def gather_test_cases(test_dir, pattern, list_tests):
1116
case_list = []
1217
for dirpath, dirnames, filenames in os.walk(test_dir):

tests/test_kws_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import re
2+
import unittest
3+
4+
from funasr.utils.kws_utils import query_token_set, split_mixed_label, symbol_str
5+
6+
7+
class KwsUtilsTest(unittest.TestCase):
8+
def test_symbol_regex_strips_punctuation_and_whitespace(self):
9+
self.assertEqual(re.sub(symbol_str, "", "abc!@# 中文\t★"), "abc中文")
10+
11+
def test_split_mixed_label_lowercases_ascii_words(self):
12+
self.assertEqual(split_mixed_label("Hello中文"), ["hello", "中", "文"])
13+
14+
def test_query_token_set_strips_symbols_before_character_lookup(self):
15+
symbol_table = {"a": 1, "中": 2, "<unk>": 0}
16+
tokens, token_ids = query_token_set("A!中", symbol_table, {})
17+
18+
self.assertEqual(tokens, ("a", "中"))
19+
self.assertEqual(token_ids, (1, 2))
20+
21+
22+
if __name__ == "__main__":
23+
unittest.main()

0 commit comments

Comments
 (0)