Skip to content
Merged
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
11 changes: 10 additions & 1 deletion funasr/utils/postprocess_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ def format_str_v2(s):
return s.strip()


def _is_spaced_char(ch):
"""Latin and Hangul text separates words with spaces; CJK text does not."""
return (ch.isascii() and not ch.isspace()) or "\uac00" <= ch <= "\ud7a3"


def rich_transcription_postprocess(s):
"""Rich transcription postprocess.

Expand Down Expand Up @@ -481,6 +486,10 @@ def get_event(s):
cur_ent_event = get_event(s_list[i])
if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s):
new_s = new_s[:-1]
new_s += s_list[i].strip().lstrip()
segment = s_list[i].strip()
# Each VAD segment is its own <|lang|> block; keep the word boundary between them.
if segment and _is_spaced_char(new_s[-1]) and _is_spaced_char(segment[0]):
new_s += " "
new_s += segment
new_s = new_s.replace("The.", " ")
return new_s.strip()
11 changes: 10 additions & 1 deletion runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ def format_str_v2(s):
return s.strip()


def _is_spaced_char(ch):
"""Latin and Hangul text separates words with spaces; CJK text does not."""
return (ch.isascii() and not ch.isspace()) or "\uac00" <= ch <= "\ud7a3"


def rich_transcription_postprocess(s):
def get_emo(s):
return s[-1] if s[-1] in emo_set else None
Expand All @@ -370,6 +375,10 @@ def get_event(s):
cur_ent_event = get_event(s_list[i])
if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s):
new_s = new_s[:-1]
new_s += s_list[i].strip().lstrip()
segment = s_list[i].strip()
# Each VAD segment is its own <|lang|> block; keep the word boundary between them.
if segment and _is_spaced_char(new_s[-1]) and _is_spaced_char(segment[0]):
new_s += " "
new_s += segment
new_s = new_s.replace("The.", " ")
return new_s.strip()
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ def format_str_v2(s):
return s.strip()


def _is_spaced_char(ch):
"""Latin and Hangul text separates words with spaces; CJK text does not."""
return (ch.isascii() and not ch.isspace()) or "\uac00" <= ch <= "\ud7a3"


def rich_transcription_postprocess(s):
def get_emo(s):
return s[-1] if s[-1] in emo_set else None
Expand All @@ -424,6 +429,10 @@ def get_event(s):
cur_ent_event = get_event(s_list[i])
if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s):
new_s = new_s[:-1]
new_s += s_list[i].strip().lstrip()
segment = s_list[i].strip()
# Each VAD segment is its own <|lang|> block; keep the word boundary between them.
if segment and _is_spaced_char(new_s[-1]) and _is_spaced_char(segment[0]):
new_s += " "
new_s += segment
new_s = new_s.replace("The.", " ")
return new_s.strip()
4 changes: 3 additions & 1 deletion tests/test_rich_transcription_event_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_repeated_event_only_segments_keep_following_text(postprocess, event, fo

result = postprocess.rich_transcription_postprocess(text)

assert result == postprocess.event_dict[event] + "hello" + following_text
assert result == postprocess.event_dict[event] + " ".join(
filter(None, ["hello", following_text])
)


@pytest.mark.parametrize("event", ["<|Cough|>", "<|Sneeze|>", "<|Applause|>"])
Expand Down
21 changes: 21 additions & 0 deletions tests/test_rich_transcription_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ def test_cough_and_sneeze_events_render_distinct_emojis(path, event, emoji):
text = module.rich_transcription_postprocess(f"<|en|><|NEUTRAL|>{event}<|woitn|>hello")

assert text == f"{emoji}hello"


@pytest.mark.parametrize("path", POSTPROCESS_UTILS, ids=lambda path: path.parts[-3])
@pytest.mark.parametrize(
"first, second, expected",
[
("Hello world.", "How are you?", "Hello world. How are you?"),
("so I went to the", "store", "so I went to the store"),
("안녕하세요.", "감사합니다.", "안녕하세요. 감사합니다."),
("你好。", "再见。", "你好。再见。"),
("你好。", "Hello.", "你好。Hello."),
],
)
def test_vad_segments_keep_a_space_between_spaced_scripts(path, first, second, expected):
module = _load(path)
tags = "<|NEUTRAL|><|Speech|><|withitn|>"

# AutoModel joins the tagged text of each VAD segment with a single space.
text = module.rich_transcription_postprocess(f"<|en|>{tags}{first} <|en|>{tags}{second}")

assert text == expected
Loading