rename text processing functions

pull/3047/head
jlqian 3 years ago
parent 242834d3ee
commit 335a50ce56

@ -2,7 +2,7 @@
namespace ppspeech { namespace ppspeech {
std::string RemoveBlk(const std::string& str) { std::string DelBlank(const std::string& str) {
std::string out = ""; std::string out = "";
int ptr_in = 0; // the pointer of input string (for traversal) int ptr_in = 0; // the pointer of input string (for traversal)
int end = str.size(); int end = str.size();
@ -23,7 +23,7 @@ std::string RemoveBlk(const std::string& str) {
return out; return out;
} }
std::string AddBlk(const std::string& str) { std::string AddBlank(const std::string& str) {
std::string out = ""; std::string out = "";
int ptr = 0; // the pointer of the input string int ptr = 0; // the pointer of the input string
int end = str.size(); int end = str.size();
@ -44,32 +44,29 @@ std::string AddBlk(const std::string& str) {
return out; return out;
} }
std::string ReverseFrac(const std::string& str, std::string ReverseFraction(const std::string& str) {
const std::string& left_tag,
const std::string& right_tag) {
std::string out = ""; std::string out = "";
int ptr = 0; // the pointer of the input string int ptr = 0; // the pointer of the input string
int end = str.size(); int end = str.size();
int left, right, frac; // the start index of the left tag, right tag and '/'. int left, right, frac; // the start index of the left tag, right tag and '/'.
left = right = frac = 0; left = right = frac = 0;
int len_left_tag = left_tag.size(); int len_tag = 5; // length of "<tag>"
int len_right_tag = right_tag.size();
while (ptr != end) { while (ptr != end) {
// find the position of left tag, right tag and '/'. (xxx<tag>num1/num2</tag>) // find the position of left tag, right tag and '/'. (xxx<tag>num1/num2</tag>)
left = str.find(left_tag, ptr); left = str.find("<tag>", ptr);
if (left == -1) if (left == -1)
break; break;
out += str.substr(ptr, left - ptr); // content before left tag (xxx) out += str.substr(ptr, left - ptr); // content before left tag (xxx)
frac = str.find("/", left); frac = str.find("/", left);
right = str.find(right_tag, frac); right = str.find("<tag>", frac);
out += str.substr(frac + 1, right - frac - 1) + '/' + out += str.substr(frac + 1, right - frac - 1) + '/' +
str.substr(left + len_left_tag, frac - left - len_left_tag); // num2/num1 str.substr(left + len_tag, frac - left - len_tag); // num2/num1
ptr = right + len_right_tag; ptr = right + len_tag;
} }
if (ptr != end) { if (ptr != end) {
out += str.substr(ptr, end - right - len_right_tag); out += str.substr(ptr, end - ptr);
} }
return out; return out;
} }

@ -4,12 +4,10 @@
namespace ppspeech { namespace ppspeech {
std::string RemoveBlk(const std::string& str); std::string DelBlank(const std::string& str);
std::string AddBlk(const std::string& str); std::string AddBlank(const std::string& str);
std::string ReverseFrac(const std::string& str, std::string ReverseFraction(const std::string& str);
const std::string& left_tag = "<tag>",
const std::string& right_tag = "</tag>");
} // namespace ppspeech } // namespace ppspeech

@ -3,43 +3,45 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <gmock/gmock.h> #include <gmock/gmock.h>
TEST(TextProcess, RemoveBlkTest) { TEST(TextProcess, DelBlankTest) {
std::string test_str = "我 今天 去 了 超市 花了 120 元。"; std::string test_str = "我 今天 去 了 超市 花了 120 元。";
std::string out_str = ppspeech::RemoveBlk(test_str); std::string out_str = ppspeech::DelBlank(test_str);
int ret = out_str.compare("我今天去了超市花了120元。"); int ret = out_str.compare("我今天去了超市花了120元。");
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
test_str = "how are you today"; test_str = "how are you today";
out_str = ppspeech::RemoveBlk(test_str); out_str = ppspeech::DelBlank(test_str);
ret = out_str.compare("how are you today"); ret = out_str.compare("how are you today");
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
test_str = "我 的 paper 在 哪里?"; test_str = "我 的 paper 在 哪里?";
out_str = ppspeech::RemoveBlk(test_str); out_str = ppspeech::DelBlank(test_str);
ret = out_str.compare("我的paper在哪里"); ret = out_str.compare("我的paper在哪里");
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
} }
TEST(TextProcess, AddBlkTest) { TEST(TextProcess, AddBlankTest) {
std::string test_str = "how are you"; std::string test_str = "how are you";
std::string out_str = ppspeech::AddBlk(test_str); std::string out_str = ppspeech::AddBlank(test_str);
int ret = out_str.compare(" how are you "); int ret = out_str.compare(" how are you ");
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
test_str = "欢迎来到China。"; test_str = "欢迎来到China。";
out_str = ppspeech::AddBlk(test_str); out_str = ppspeech::AddBlank(test_str);
ret = out_str.compare("欢迎来到 China 。"); ret = out_str.compare("欢迎来到 China 。");
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
} }
TEST(TextProcess, ReverseFracTest) { TEST(TextProcess, ReverseFractionTest) {
std::string test_str = "<tag>3/1</tag>"; std::string test_str = "<tag>3/1<tag>";
std::string out_str = ppspeech::ReverseFrac(test_str); std::string out_str = ppspeech::ReverseFraction(test_str);
int ret = out_str.compare("1/3"); int ret = out_str.compare("1/3");
std::cout<<out_str<<std::endl;
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
test_str = "<tag>3/1</tag> <tag>100/10000</tag>"; test_str = "<tag>3/1<tag> <tag>100/10000<tag>";
out_str = ppspeech::ReverseFrac(test_str); out_str = ppspeech::ReverseFraction(test_str);
ret = out_str.compare("1/3 10000/100"); ret = out_str.compare("1/3 10000/100");
std::cout<<out_str<<std::endl;
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
} }
Loading…
Cancel
Save