Skip to content

Commit 09c8a1c

Browse files
fix(text): drop whitespace tokens from the Chinese word splitter (#2492)
jieba.cut keeps the spaces between words as tokens, so every zh heuristic filter counted them as words, and SymbolsToWordsFilter divided by the length of a stripped space and raised ZeroDivisionError on any document with a space. Closes #2491 Signed-off-by: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Co-authored-by: Sarah Yurick <53962159+sarahyurick@users.noreply.github.com>
1 parent 3cd0436 commit 09c8a1c

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎nemo_curator/stages/text/utils/text_utils.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def get_word_splitter(language: str) -> Callable[[str], list[str]]:
4444
import jieba
4545

4646
def jieba_splitter(text: str) -> list[str]:
47-
return list(jieba.cut(text))
47+
# Jieba returns the whitespace between words as tokens, so drop them like str.split() does
48+
return [word for word in jieba.cut(text) if word.strip()]
4849

4950
return jieba_splitter
5051

‎tests/stages/text/modules/test_filters.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@ def test_symbolswords(self) -> None:
505505
)
506506
assert all_equal(expected_data, filtered_data), f"Expected {expected_data} but got {filtered_data}"
507507

508+
def test_symbolswords_zh_with_spaces(self) -> None:
509+
dataset = list_to_dataset(["我用 Python 写代码。", "... ... 标题"])
510+
filters = ScoreFilter(SymbolsToWordsFilter(lang="zh"))
511+
512+
filtered_data = filters.process(dataset)
513+
514+
expected_data = DocumentBatch(
515+
data=pd.DataFrame({"text": ["我用 Python 写代码。"]}),
516+
dataset_name="test_1",
517+
)
518+
assert all_equal(expected_data, filtered_data), f"Expected {expected_data} but got {filtered_data}"
519+
508520
def test_numbers(self) -> None:
509521
dataset = list_to_dataset(["purely letters", "34134543", "$!@$@!$!@", "abcdefghi1"])
510522
filters = ScoreFilter(NumbersFilter(max_number_to_text_ratio=0.1))
@@ -728,6 +740,19 @@ def test_wordcount_zh(self) -> None:
728740
)
729741
assert all_equal(expected_data, filtered_data), f"Expected {expected_data} but got {filtered_data}"
730742

743+
def test_wordcount_zh_ignores_spaces(self) -> None:
744+
# 5 words: 我用 / Python / 写 / 代码 / 。, the two spaces are not words
745+
dataset = list_to_dataset(["我用 Python 写代码。"])
746+
filters = ScoreFilter(WordCountFilter(min_words=2, max_words=5, lang="zh"))
747+
748+
filtered_data = filters.process(dataset)
749+
750+
expected_data = DocumentBatch(
751+
data=pd.DataFrame({"text": ["我用 Python 写代码。"]}),
752+
dataset_name="test_1",
753+
)
754+
assert all_equal(expected_data, filtered_data), f"Expected {expected_data} but got {filtered_data}"
755+
731756
@pytest.mark.skip(reason="Skipping MeCab runtime error for now")
732757
def test_wordcount_ja(self) -> None:
733758
dataset = list_to_dataset(["", "猫が寝ます。", "私は日本語のテキストを分割します。"])

0 commit comments

Comments
 (0)