2019-01-24 03:48:09 +08:00
|
|
|
|
#include "extension.h"
|
2018-08-19 12:13:19 +08:00
|
|
|
|
|
2019-06-05 03:30:48 +08:00
|
|
|
|
void RemoveRepeatedSentences(std::wstring& sentence, uint64_t textNumber)
|
|
|
|
|
{
|
2019-06-05 11:12:45 +08:00
|
|
|
|
static std::deque<Synchronized<std::vector<std::wstring>>> cache;
|
2019-06-05 03:30:48 +08:00
|
|
|
|
static std::mutex m;
|
|
|
|
|
m.lock();
|
|
|
|
|
if (textNumber + 1 > cache.size()) cache.resize(textNumber + 1);
|
2019-06-05 11:12:45 +08:00
|
|
|
|
auto prevSentences = cache.at(textNumber).Acquire();
|
2019-06-05 03:30:48 +08:00
|
|
|
|
m.unlock();
|
|
|
|
|
auto& inserted = prevSentences->emplace_back(sentence);
|
|
|
|
|
auto firstLocation = std::find(prevSentences->begin(), prevSentences->end(), sentence);
|
|
|
|
|
if (&*firstLocation != &inserted)
|
|
|
|
|
{
|
|
|
|
|
prevSentences->erase(firstLocation);
|
|
|
|
|
sentence.clear();
|
|
|
|
|
}
|
|
|
|
|
if (prevSentences->size() > 50) prevSentences->erase(prevSentences->begin());
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 23:43:54 +08:00
|
|
|
|
void RemoveRepeatedChars(std::wstring& sentence)
|
2018-08-19 12:13:19 +08:00
|
|
|
|
{
|
2018-12-29 00:57:06 +08:00
|
|
|
|
std::vector<int> repeatNumbers(sentence.size() + 1, 0);
|
|
|
|
|
int repeatNumber = 1;
|
|
|
|
|
wchar_t prevChar = L'\0';
|
|
|
|
|
for (auto nextChar : sentence)
|
2019-02-25 13:47:18 +08:00
|
|
|
|
{
|
|
|
|
|
if (nextChar == prevChar)
|
|
|
|
|
{
|
|
|
|
|
repeatNumber += 1;
|
|
|
|
|
}
|
2018-12-29 00:57:06 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
prevChar = nextChar;
|
2019-02-21 11:12:26 +08:00
|
|
|
|
repeatNumbers.at(repeatNumber) += 1;
|
2018-12-29 00:57:06 +08:00
|
|
|
|
repeatNumber = 1;
|
|
|
|
|
}
|
2019-02-25 13:47:18 +08:00
|
|
|
|
}
|
2018-12-29 00:57:06 +08:00
|
|
|
|
if ((repeatNumber = std::distance(repeatNumbers.begin(), std::max_element(repeatNumbers.begin(), repeatNumbers.end()))) == 1) return;
|
2018-09-01 16:24:22 +08:00
|
|
|
|
|
2018-12-29 00:57:06 +08:00
|
|
|
|
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;
|
2018-09-01 16:24:22 +08:00
|
|
|
|
}
|
2018-08-19 12:13:19 +08:00
|
|
|
|
|
2018-11-07 23:43:54 +08:00
|
|
|
|
void RemoveCyclicRepeats(std::wstring& sentence)
|
2018-09-01 16:24:22 +08:00
|
|
|
|
{
|
2019-02-25 13:47:18 +08:00
|
|
|
|
if (sentence.size() > 15000) return; // this algorithm is O(N^3) so if N > 15000 it's extremely slow
|
2019-02-21 11:12:26 +08:00
|
|
|
|
auto data = std::make_unique<wchar_t[]>(sentence.size() + 1);
|
|
|
|
|
wcscpy_s(data.get(), sentence.size() + 1, sentence.c_str());
|
|
|
|
|
wchar_t* dataEnd = data.get() + sentence.size();
|
|
|
|
|
int skip = 0, count = 0;
|
|
|
|
|
for (wchar_t* end = dataEnd; end - data.get() > skip; --end)
|
|
|
|
|
{
|
|
|
|
|
std::swap(*end, *dataEnd);
|
|
|
|
|
int junkLength = end - data.get() - skip;
|
|
|
|
|
auto junkFound = wcsstr(sentence.c_str() + skip + junkLength, data.get() + skip);
|
|
|
|
|
std::swap(*end, *dataEnd);
|
|
|
|
|
if (junkFound)
|
2018-11-15 13:06:49 +08:00
|
|
|
|
{
|
2019-02-25 13:47:18 +08:00
|
|
|
|
if (count && junkLength < min(skip / count, 4)) break;
|
2019-02-21 11:12:26 +08:00
|
|
|
|
skip += junkLength;
|
|
|
|
|
count += 1;
|
|
|
|
|
end = dataEnd;
|
2018-11-15 13:06:49 +08:00
|
|
|
|
}
|
2019-02-21 11:12:26 +08:00
|
|
|
|
}
|
|
|
|
|
if (count && skip / count >= 3) sentence = data.get() + skip;
|
2018-09-01 16:24:22 +08:00
|
|
|
|
}
|
2018-08-19 12:13:19 +08:00
|
|
|
|
|
2018-09-23 03:08:31 +08:00
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
2018-09-01 16:24:22 +08:00
|
|
|
|
{
|
2019-02-18 08:14:49 +08:00
|
|
|
|
if (sentenceInfo["text number"] == 0) return false;
|
2019-06-05 03:30:48 +08:00
|
|
|
|
RemoveRepeatedSentences(sentence, sentenceInfo["text number"]);
|
2018-11-07 23:43:54 +08:00
|
|
|
|
RemoveRepeatedChars(sentence);
|
|
|
|
|
RemoveCyclicRepeats(sentence);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-12-29 00:57:06 +08:00
|
|
|
|
|
|
|
|
|
TEST(
|
|
|
|
|
{
|
|
|
|
|
std::wstring repeatedChars = L"aaaaaaaaaaaabbbbbbcccdddaabbbcccddd";
|
|
|
|
|
RemoveRepeatedChars(repeatedChars);
|
|
|
|
|
assert(repeatedChars.find(L"aaaabbcd") == 0);
|
|
|
|
|
|
2019-02-25 13:47:18 +08:00
|
|
|
|
std::wstring cyclicRepeats = L"_abcde_abcdef_abcdefg_abcdefg_abcdefg_abcdefg_abcdefg";
|
|
|
|
|
std::wstring buildupRepeats = L"__a_ab_abc_abcd_abcde_abcdef_abcdefg";
|
2018-12-29 00:57:06 +08:00
|
|
|
|
RemoveCyclicRepeats(cyclicRepeats);
|
2019-02-21 11:12:26 +08:00
|
|
|
|
RemoveCyclicRepeats(buildupRepeats);
|
2019-02-25 13:47:18 +08:00
|
|
|
|
assert(cyclicRepeats == L"_abcdefg");
|
|
|
|
|
assert(buildupRepeats == L"_abcdefg");
|
2018-12-29 00:57:06 +08:00
|
|
|
|
|
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. はい");
|
2018-12-29 00:57:06 +08:00
|
|
|
|
}
|
|
|
|
|
);
|