From dce3c798a2f2d7e1cc436e78347fe23fff99074b Mon Sep 17 00:00:00 2001 From: Gres Date: Sat, 11 Apr 2020 20:28:37 +0300 Subject: [PATCH] Add ability to set substitution for any language --- share/translations/screentranslator_ru.ts | 11 +++++-- src/correct/corrector.cpp | 39 ++++++++++++++++------- src/languagecodes.cpp | 6 ++++ src/languagecodes.h | 1 + src/substitutionstable.cpp | 4 +++ 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/share/translations/screentranslator_ru.ts b/share/translations/screentranslator_ru.ts index c4ddc9f..17c2987 100644 --- a/share/translations/screentranslator_ru.ts +++ b/share/translations/screentranslator_ru.ts @@ -590,6 +590,11 @@ Ctrl - продолжить выделять Greek, Ancient (to 1453) Дневнегреческий + + + Any + Любой язык + app @@ -1055,17 +1060,17 @@ in %1 SubstitutionsTable - + Language Язык - + Source Исходный текст - + Changed Замена diff --git a/src/correct/corrector.cpp b/src/correct/corrector.cpp index 393071d..3623331 100644 --- a/src/correct/corrector.cpp +++ b/src/correct/corrector.cpp @@ -1,6 +1,7 @@ #include "corrector.h" #include "correctorworker.h" #include "debug.h" +#include "languagecodes.h" #include "manager.h" #include "settings.h" #include "task.h" @@ -72,22 +73,38 @@ QString Corrector::substituteUser(const QString &source, { auto result = source; - const auto range = settings_.userSubstitutions.equal_range(language); - if (range.first == settings_.userSubstitutions.cend()) + using It = Substitutions::const_iterator; + std::vector> ranges; + + { + const auto range = settings_.userSubstitutions.equal_range(language); + if (range.first != settings_.userSubstitutions.cend()) + ranges.push_back(range); + } + { + const auto anyId = LanguageCodes::anyLanguageId(); + const auto range = settings_.userSubstitutions.equal_range(anyId); + if (range.first != settings_.userSubstitutions.cend()) + ranges.push_back(range); + } + + if (ranges.empty()) return result; while (true) { - auto bestMatch = range.first; + auto bestMatch = ranges.front().first; auto bestMatchLen = 0; - for (auto it = range.first; it != range.second; ++it) { - const auto &sub = it->second; - if (!result.contains(sub.source)) - continue; - const auto len = sub.source.length(); - if (len > bestMatchLen) { - bestMatchLen = len; - bestMatch = it; + for (const auto &range : ranges) { + for (auto it = range.first; it != range.second; ++it) { + const auto &sub = it->second; + if (!result.contains(sub.source)) + continue; + const auto len = sub.source.length(); + if (len > bestMatchLen) { + bestMatchLen = len; + bestMatch = it; + } } } diff --git a/src/languagecodes.cpp b/src/languagecodes.cpp index 1c311e3..5aa4f64 100644 --- a/src/languagecodes.cpp +++ b/src/languagecodes.cpp @@ -201,6 +201,7 @@ const std::unordered_map {I("enm"), {I("enm"), S(""), S("enm"), QT_TRANSLATE_NOOP("QObject", "English, Middle (1100-1500)")}}, {I("frm"), {I("frm"), S(""), S("frm"), QT_TRANSLATE_NOOP("QObject", "French, Middle (ca.1400-1600)")}}, {I("grc"), {I("grc"), S(""), S("grc"), QT_TRANSLATE_NOOP("QObject", "Greek, Ancient (to 1453)")}}, + {I("any"), {I("any"), S(""), S(""), QT_TRANSLATE_NOOP("QObject", "Any")}}, // clang-format on }; #undef I @@ -253,3 +254,8 @@ std::vector LanguageCodes::allIds() for (const auto &code : codes_) result.push_back(code.first); return result; } + +LanguageId LanguageCodes::anyLanguageId() +{ + return "any"; +} diff --git a/src/languagecodes.h b/src/languagecodes.h index f1b5e46..405144c 100644 --- a/src/languagecodes.h +++ b/src/languagecodes.h @@ -16,6 +16,7 @@ public: static QString tesseract(const LanguageId& id); static QString name(const LanguageId& id); static std::vector allIds(); + static LanguageId anyLanguageId(); private: struct Bundle { diff --git a/src/substitutionstable.cpp b/src/substitutionstable.cpp index d854132..0f3662e 100644 --- a/src/substitutionstable.cpp +++ b/src/substitutionstable.cpp @@ -119,6 +119,10 @@ void SubstitutionsTable::updateModel(const Substitutions &substitutions) } } + const auto any = LanguageCodes::name(LanguageCodes::anyLanguageId()); + if (!strings.contains(any)) + strings.append(any); + std::sort(strings.begin(), strings.end()); substitutionLanguages_->setStringList(strings); }