Textractor_test/extensions/removerepeat.cpp

71 lines
1.9 KiB
C++
Raw Normal View History

2019-01-24 03:48:09 +08:00
#include "extension.h"
#include "defs.h"
void RemoveRepeatedChars(std::wstring& sentence)
{
std::vector<int> repeatNumbers(sentence.size() + 1, 0);
int repeatNumber = 1;
wchar_t prevChar = L'\0';
for (auto nextChar : sentence)
if (nextChar == prevChar) repeatNumber++;
else
{
prevChar = nextChar;
++repeatNumbers.at(repeatNumber);
repeatNumber = 1;
}
if ((repeatNumber = std::distance(repeatNumbers.begin(), std::max_element(repeatNumbers.begin(), repeatNumbers.end()))) == 1) return;
std::wstring newSentence;
for (int i = 0; i < sentence.size();)
{
newSentence.push_back(sentence.at(i));
for (int j = i; j <= sentence.size(); ++j)
{
if (j == sentence.size() || sentence.at(i) != sentence.at(j))
{
i += (j - i) % repeatNumber == 0 ? repeatNumber : 1;
break;
}
}
}
2018-09-13 14:50:03 +08:00
sentence = newSentence;
}
void RemoveCyclicRepeats(std::wstring& sentence)
{
2018-11-15 13:06:49 +08:00
remove:
2018-11-28 05:54:19 +08:00
for (std::wstring junk = sentence; junk.size() > 4; junk.pop_back())
2018-11-15 13:06:49 +08:00
if (sentence.rfind(junk) > 0)
{
sentence.erase(0, junk.size());
goto remove;
}
}
2018-09-23 03:08:31 +08:00
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
2018-09-23 03:08:31 +08:00
if (sentenceInfo["hook address"] == -1) return false;
RemoveRepeatedChars(sentence);
RemoveCyclicRepeats(sentence);
return true;
}
TEST(
{
std::wstring repeatedChars = L"aaaaaaaaaaaabbbbbbcccdddaabbbcccddd";
RemoveRepeatedChars(repeatedChars);
assert(repeatedChars.find(L"aaaabbcd") == 0);
std::wstring cyclicRepeats = L"abcdeabcdefabcdefgabcdefgabcdefgabcdefgabcdefg";
RemoveCyclicRepeats(cyclicRepeats);
assert(cyclicRepeats == L"abcdefg");
2019-01-24 03:48:09 +08:00
std::wstring empty = L"", one = L" ", normal = L"This is a normal sentence. はい";
2019-02-13 12:54:33 +08:00
ProcessSentence(empty, { SentenceInfo::DUMMY });
ProcessSentence(one, { SentenceInfo::DUMMY });
ProcessSentence(normal, { SentenceInfo::DUMMY });
2019-01-24 05:11:14 +08:00
assert(empty == L"" && one == L" " && normal == L"This is a normal sentence. はい");
}
);