Textractor_test/extensions/removerepeatchar.cpp

55 lines
1.6 KiB
C++
Raw Normal View History

2019-06-13 13:06:34 -04:00
#include "extension.h"
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
if (sentenceInfo["text number"] == 0) return false;
std::vector<int> repeatNumbers(sentence.size() + 1, 0);
for (int i = 0; i < sentence.size(); ++i)
2019-06-13 13:06:34 -04:00
{
if (sentence[i] != sentence[i + 1])
2019-06-13 13:06:34 -04:00
{
int j = i;
while (sentence[j] == sentence[i] && --j >= 0);
repeatNumbers[i - j] += 1;
2019-06-13 13:06:34 -04:00
}
}
int repeatNumber = std::distance(repeatNumbers.begin(), std::max_element(repeatNumbers.rbegin(), repeatNumbers.rend()).base() - 1);
if (repeatNumber < 2) return false;
2019-06-13 13:06:34 -04:00
std::wstring newSentence;
for (int i = 0; i < sentence.size();)
{
newSentence.push_back(sentence[i]);
2019-06-13 13:06:34 -04:00
for (int j = i; j <= sentence.size(); ++j)
{
if (j == sentence.size() || sentence[i] != sentence[j])
2019-06-13 13:06:34 -04:00
{
i += (j - i) % repeatNumber == 0 ? repeatNumber : 1;
break;
}
}
}
sentence = newSentence;
return true;
}
TEST(
{
2019-10-02 05:30:14 -04:00
InfoForExtension nonConsole[] = { { "text number", 1 }, {} };
2019-06-13 13:06:34 -04:00
std::wstring repeatedChars = L"aaaaaaaaaaaabbbbbbcccdddaabbbcccddd";
std::wstring someRepeatedChars = L"abcdefaabbccddeeff";
2019-10-02 05:30:14 -04:00
ProcessSentence(repeatedChars, { nonConsole });
ProcessSentence(someRepeatedChars, { nonConsole });
2019-06-13 13:06:34 -04:00
assert(repeatedChars.find(L"aaaabbcd") == 0);
assert(someRepeatedChars == L"abcdefabcdef");
2019-06-13 13:06:34 -04:00
std::wstring empty = L"", one = L" ", normal = L"This is a normal sentence. はい";
2019-10-02 05:30:14 -04:00
ProcessSentence(empty, { nonConsole });
ProcessSentence(one, { nonConsole });
ProcessSentence(normal, { nonConsole });
2019-06-13 13:06:34 -04:00
assert(empty == L"" && one == L" " && normal == L"This is a normal sentence. はい");
}
);