Skip to content

Commit 5e7e252

Browse files
authored
Refactor text-utils (k2-fsa#2855)
This pull request refactors text utility functions by introducing a generic Join function in text-utils and removing a specialized JoinTokensNoSpace function.
1 parent 2493d1f commit 5e7e252

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

sherpa-onnx/csrc/matcha-tts-lexicon.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,6 @@ std::vector<std::string> ConvertPhonemesToUTF8(
9292
return out;
9393
}
9494

95-
std::string JoinTokensNoSpace(const std::vector<std::string> &tokens) {
96-
std::string out;
97-
for (const auto &t : tokens) {
98-
out += t;
99-
}
100-
return out;
101-
}
102-
10395
std::string ApplyReplacements(std::string s) {
10496
for (const auto &p : kReplacements) {
10597
const std::string &from = p.first;
@@ -131,7 +123,7 @@ std::vector<std::string> SplitTokensUTF8(const std::string &s) {
131123
std::vector<std::string> ProcessPhonemes(
132124
const std::vector<std::vector<char32_t>> &phonemes) {
133125
auto tokens = ConvertPhonemesToUTF8(phonemes);
134-
std::string joined = JoinTokensNoSpace(tokens);
126+
std::string joined = Join(tokens);
135127
std::string replaced = ApplyReplacements(joined);
136128
return SplitTokensUTF8(replaced);
137129
}

sherpa-onnx/csrc/text-utils.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,17 @@ std::vector<std::string> SplitString(const std::string &s, int32_t chunk_size) {
733733
return ans;
734734
}
735735

736+
std::string Join(const std::vector<std::string> &ss, const std::string &delim) {
737+
std::ostringstream oss;
738+
if (!ss.empty()) {
739+
oss << ss[0];
740+
for (size_t i = 1; i < ss.size(); ++i) {
741+
oss << delim << ss[i];
742+
}
743+
}
744+
return oss.str();
745+
}
746+
736747
std::u32string Utf8ToUtf32(const std::string &str) {
737748
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
738749
return conv.from_bytes(str);

sherpa-onnx/csrc/text-utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ bool Contains(const std::string &haystack, const std::string &needle);
151151

152152
std::vector<std::string> SplitString(const std::string &s, int32_t chunk_size);
153153

154+
std::string Join(const std::vector<std::string> &ss,
155+
const std::string &delim = "");
156+
154157
// Converts a UTF-8 std::string to a UTF-32 std::u32string
155158
std::u32string Utf8ToUtf32(const std::string &str);
156159

0 commit comments

Comments
 (0)