MCPcopy Create free account
hub / github.com/Artikash/Textractor / ProcessSentence

Function ProcessSentence

extensions/removerepeatphrase.cpp:42–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40constexpr wchar_t ERASED = 0xf246; // inside Unicode private use area
41
42bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
43{
44 if (sentenceInfo["text number"] == 0) return false;
45
46 // This algorithm looks for repeating substrings (in other words, common prefixes among the set of suffixes) of the sentence with length > 6
47 // It then looks for any regions of characters at least twice as long as the substring made up only of characters in the substring, and erases them
48 // If this results in the substring being completely erased from the string, the substring is copied to the last location where it was located in the original string
49 auto timeout = GetTickCount64() + 30'000; // give up if taking over 30 seconds
50 std::vector<int> suffixArray = GenerateSuffixArray(sentence);
51 for (int i = 0; i + 1 < sentence.size() && GetTickCount64() < timeout; ++i)
52 {
53 int commonPrefixLength = 0;
54 for (int j = suffixArray[i], k = suffixArray[i + 1]; j < sentence.size() && k < sentence.size(); ++j, ++k)
55 if (sentence[j] != ERASED && sentence[j] == sentence[k]) commonPrefixLength += 1;
56 else break;
57
58 if (commonPrefixLength > 6)
59 {
60 std::wstring substring(sentence, suffixArray[i], commonPrefixLength);
61 bool substringCharMap[0x10000] = {};
62 for (auto ch : substring) substringCharMap[ch] = true;
63
64 for (int regionSize = 0, j = 0; j <= sentence.size(); ++j)
65 if (substringCharMap[sentence[j]]) regionSize += 1;
66 else if (regionSize >= commonPrefixLength * 2)
67 while (regionSize > 0) sentence[j - regionSize--] = ERASED;
68 else regionSize = 0;
69
70 if (!wcsstr(sentence.c_str(), substring.c_str())) std::copy(substring.begin(), substring.end(), sentence.begin() + max(suffixArray[i], suffixArray[i + 1]));
71 }
72 }
73 sentence.erase(std::remove(sentence.begin(), sentence.end(), ERASED), sentence.end());
74 return true;
75}
76
77TEST(
78 {

Callers 1

Calls 1

GenerateSuffixArrayFunction · 0.85

Tested by

no test coverage detected