Skip to content

Commit 1d8080a

Browse files
authored
fix: handle Unicode punctuation post-processing (#3238)
1 parent 0a5a003 commit 1d8080a

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

fun_text_processing/text_normalization/data_loader_utils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def post_process_punct(input: str, normalized_text: str, add_unicode_punct: bool
284284
Args:
285285
input: input text (original input to the NN, before normalization or tokenization)
286286
normalized_text: output text (output of the TN NN model)
287-
add_unicode_punct: set to True to handle unicode punctuation marks as well as default string.punctuation (increases post processing time)
287+
add_unicode_punct: set to True to handle unicode punctuation marks as well as default string.punctuation
288288
"""
289289
# in the post-processing WFST graph "``" are repalced with '"" quotes (otherwise single quotes "`" won't be handled correctly)
290290
# this function fixes spaces around them based on input sequence, so here we're making the same double quote replacement
@@ -296,12 +296,11 @@ def post_process_punct(input: str, normalized_text: str, add_unicode_punct: bool
296296
punct_marks = [x for x in string.punctuation if x in input]
297297

298298
if add_unicode_punct:
299-
punct_unicode = [
300-
chr(i)
301-
for i in range(sys.maxunicode)
302-
if category(chr(i)).startswith("P") and chr(i) not in punct_default and chr(i) in input
303-
]
304-
punct_marks = punct_marks.extend(punct_unicode)
299+
punct_marks.extend(
300+
char
301+
for char in dict.fromkeys(input)
302+
if char not in string.punctuation and category(char).startswith("P")
303+
)
305304

306305
for punct in punct_marks:
307306
try:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from fun_text_processing.text_normalization.data_loader_utils import post_process_punct
2+
3+
4+
def test_post_process_punct_preserves_ascii_punctuation_spacing():
5+
assert post_process_punct("test' example", "test 'example") == "test' example"
6+
7+
8+
def test_post_process_punct_handles_unicode_punctuation():
9+
assert (
10+
post_process_punct(
11+
"\u4f60\u597d\uff0c\u4e16\u754c\uff01",
12+
"\u4f60\u597d \uff0c \u4e16\u754c \uff01",
13+
add_unicode_punct=True,
14+
)
15+
== "\u4f60\u597d\uff0c\u4e16\u754c\uff01"
16+
)

0 commit comments

Comments
 (0)