only cache translation on current thread

This commit is contained in:
Akash Mozumdar 2019-07-03 12:23:10 +05:30
parent 011a5418e9
commit c074bbf506
3 changed files with 7 additions and 9 deletions

View File

@ -4,6 +4,8 @@
extern const wchar_t* TRANSLATION_ERROR; extern const wchar_t* TRANSLATION_ERROR;
extern Synchronized<std::wstring> translateTo;
const char* TRANSLATION_PROVIDER = "Bing"; const char* TRANSLATION_PROVIDER = "Bing";
QStringList languages QStringList languages
{ {
@ -53,7 +55,6 @@ QStringList languages
"Vietnamese: vi", "Vietnamese: vi",
"Welsh: cy" "Welsh: cy"
}; };
Synchronized<std::wstring> translateTo = L"en";
std::pair<bool, std::wstring> Translate(const std::wstring& text) std::pair<bool, std::wstring> Translate(const std::wstring& text)
{ {

View File

@ -6,6 +6,8 @@
extern const wchar_t* TRANSLATION_ERROR; extern const wchar_t* TRANSLATION_ERROR;
extern Synchronized<std::wstring> translateTo;
const char* TRANSLATION_PROVIDER = "Google"; const char* TRANSLATION_PROVIDER = "Google";
QStringList languages QStringList languages
{ {
@ -70,7 +72,6 @@ QStringList languages
"Yiddish: yi", "Yiddish: yi",
"Zulu: zu" "Zulu: zu"
}; };
Synchronized<std::wstring> translateTo = L"en";
std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK) std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK)
{ {

View File

@ -9,9 +9,9 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS;
extern const char* TRANSLATION_PROVIDER; extern const char* TRANSLATION_PROVIDER;
extern QStringList languages; extern QStringList languages;
extern Synchronized<std::wstring> translateTo;
std::pair<bool, std::wstring> Translate(const std::wstring& text); std::pair<bool, std::wstring> Translate(const std::wstring& text);
Synchronized<std::wstring> translateTo = L"en";
int savedSize; int savedSize;
Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache; Synchronized<std::unordered_map<std::wstring, std::wstring>> translationCache;
@ -86,7 +86,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
if (translationCache->count(sentence) != 0) translation = translationCache->at(sentence); if (translationCache->count(sentence) != 0) translation = translationCache->at(sentence);
else if (!(rateLimiter.Request() || sentenceInfo["current select"])) translation = TOO_MANY_TRANS_REQUESTS; else if (!(rateLimiter.Request() || sentenceInfo["current select"])) translation = TOO_MANY_TRANS_REQUESTS;
else std::tie(cache, translation) = Translate(sentence); else std::tie(cache, translation) = Translate(sentence);
if (cache) if (cache && sentenceInfo["current select"])
{ {
translationCache->insert({ sentence, translation }); translationCache->insert({ sentence, translation });
if (translationCache->size() > savedSize + 50) SaveCache(); if (translationCache->size() > savedSize + 50) SaveCache();
@ -98,9 +98,5 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
} }
TEST( TEST(
{ assert(Translate(L"こんにちは").second.find(L"ello") != std::wstring::npos)
std::wstring test = L"こんにちは";
ProcessSentence(test, { SentenceInfo::DUMMY });
assert(test.find(L"Hello") != std::wstring::npos);
}
); );