Skip to content

Commit 768fd7a

Browse files
authored
Remove spaces right before a punctuation in ASR result (k2-fsa#3709)
1 parent bbeb79e commit 768fd7a

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

sherpa-onnx/csrc/text-utils.cc

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,25 +1057,62 @@ bool ContainsCJK(const std::u32string &text) {
10571057
return false;
10581058
}
10591059

1060+
// Detect if a codepoint is a punctuation character.
1061+
// Covers ASCII punctuation, CJK Symbols and Punctuation, and Fullwidth forms.
1062+
static bool IsPunct(char32_t cp) {
1063+
// ASCII punctuation: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ `
1064+
// { | } ~
1065+
if (cp >= 0x21 && cp <= 0x2F) {
1066+
return true;
1067+
}
1068+
if (cp >= 0x3A && cp <= 0x40) {
1069+
return true;
1070+
}
1071+
if (cp >= 0x5B && cp <= 0x60) {
1072+
return true;
1073+
}
1074+
if (cp >= 0x7B && cp <= 0x7E) {
1075+
return true;
1076+
}
1077+
// CJK Symbols and Punctuation: 。,、;:!? etc.
1078+
if (cp >= 0x3000 && cp <= 0x303F) {
1079+
return true;
1080+
}
1081+
// Fullwidth punctuation variants
1082+
if (cp >= 0xFF01 && cp <= 0xFF0F) {
1083+
return true;
1084+
}
1085+
if (cp >= 0xFF1A && cp <= 0xFF20) {
1086+
return true;
1087+
}
1088+
if (cp >= 0xFF3B && cp <= 0xFF40) {
1089+
return true;
1090+
}
1091+
if (cp >= 0xFF5B && cp <= 0xFF65) {
1092+
return true;
1093+
}
1094+
return false;
1095+
}
1096+
10601097
std::string RemoveSpaceBetweenCjk(const std::string &text) {
10611098
std::u32string u32 = Utf8ToUtf32(text);
1062-
if (u32.size() < 3) {
1099+
if (u32.size() < 2) {
10631100
return text;
10641101
}
10651102

10661103
std::u32string ans;
10671104
ans.reserve(u32.size());
10681105
ans.push_back(u32[0]);
10691106

1070-
for (size_t i = 1; i + 1 < u32.size(); ++i) {
1071-
if (u32[i] == U' ' && IsCJK(u32[i - 1]) && IsCJK(u32[i + 1])) {
1107+
for (size_t i = 1; i < u32.size(); ++i) {
1108+
if (u32[i] == U' ' && i + 1 < u32.size() &&
1109+
((IsCJK(u32[i - 1]) && IsCJK(u32[i + 1])) || IsPunct(u32[i + 1]))) {
10721110
continue;
10731111
}
10741112

10751113
ans.push_back(u32[i]);
10761114
}
10771115

1078-
ans.push_back(u32.back());
10791116
return Utf32ToUtf8(ans);
10801117
}
10811118

@@ -1355,8 +1392,7 @@ std::vector<std::string> SplitLongSentence(const std::string &sentence,
13551392
split_pos = end;
13561393
}
13571394

1358-
std::string piece =
1359-
Trim(Utf32ToUtf8(u32.substr(start, split_pos - start)));
1395+
std::string piece = Trim(Utf32ToUtf8(u32.substr(start, split_pos - start)));
13601396
if (!piece.empty()) {
13611397
chunks.emplace_back(std::move(piece));
13621398
}

0 commit comments

Comments
 (0)