diff --git a/runtime/engine/common/utils/blank_process.cc b/runtime/engine/common/utils/blank_process.cc new file mode 100644 index 000000000..869bb7813 --- /dev/null +++ b/runtime/engine/common/utils/blank_process.cc @@ -0,0 +1,26 @@ +#include "utils/blank_process.h" + +namespace ppspeech { + +std::string BlankProcess(const std::string& str) { + std::string out = ""; + int p = 0; + int end = str.size(); + int q = -1; // last char of the output string + while (p != end) { + while (p != end && str[p] == ' ') { + p += 1; + } + if (p == end) + return out; + if (q != -1 && isalpha(str[p]) && isalpha(str[q]) && str[p-1] == ' ') + // add a space when the last and current chars are in English and there have space(s) between them + out += ' '; + out += str[p]; + q = p; + p += 1; + } + return out; +} + +} // namespace ppspeech \ No newline at end of file diff --git a/runtime/engine/common/utils/blank_process.h b/runtime/engine/common/utils/blank_process.h new file mode 100644 index 000000000..84952651b --- /dev/null +++ b/runtime/engine/common/utils/blank_process.h @@ -0,0 +1,9 @@ +#include +#include +#include + +namespace ppspeech { + +std::string BlankProcess(const std::string& str); + +} // namespace ppspeech \ No newline at end of file diff --git a/runtime/engine/common/utils/blank_process_test.cc b/runtime/engine/common/utils/blank_process_test.cc new file mode 100644 index 000000000..75f762ae6 --- /dev/null +++ b/runtime/engine/common/utils/blank_process_test.cc @@ -0,0 +1,26 @@ +#include "utils/blank_process.h" + +#include +#include + +TEST(BlankProcess, BlankProcessTest) { + std::string test_str = "我 今天 去 了 超市 花了 120 元。"; + std::string out_str = ppspeech::BlankProcess(test_str); + int ret = out_str.compare("我今天去了超市花了120元。"); + EXPECT_EQ(ret, 0); + + test_str = "how are you today"; + out_str = ppspeech::BlankProcess(test_str); + ret = out_str.compare("how are you today"); + EXPECT_EQ(ret, 0); + + test_str = "我 的 paper 在 哪里?"; + out_str = ppspeech::BlankProcess(test_str); + ret = out_str.compare("我的paper在哪里?"); + EXPECT_EQ(ret, 0); + + test_str = "我 今天 去 了 超市 花了 120 元。"; + out_str = ppspeech::BlankProcess(test_str); + ret = out_str.compare("我今天去了超市花了120元。"); + EXPECT_EQ(ret, 0); +} \ No newline at end of file