Add ability to set substitution for any language

This commit is contained in:
Gres 2020-04-11 20:28:37 +03:00
parent da51f79708
commit dce3c798a2
5 changed files with 47 additions and 14 deletions

View File

@ -590,6 +590,11 @@ Ctrl - продолжить выделять</translation>
<source>Greek, Ancient (to 1453)</source> <source>Greek, Ancient (to 1453)</source>
<translation>Дневнегреческий</translation> <translation>Дневнегреческий</translation>
</message> </message>
<message>
<location filename="../../src/languagecodes.cpp" line="204"/>
<source>Any</source>
<translation>Любой язык</translation>
</message>
<message> <message>
<location filename="../../src/manager.cpp" line="42"/> <location filename="../../src/manager.cpp" line="42"/>
<source>app</source> <source>app</source>
@ -1055,17 +1060,17 @@ in %1</source>
<context> <context>
<name>SubstitutionsTable</name> <name>SubstitutionsTable</name>
<message> <message>
<location filename="../../src/substitutionstable.cpp" line="85"/> <location filename="../../src/substitutionstable.cpp" line="81"/>
<source>Language</source> <source>Language</source>
<translation>Язык</translation> <translation>Язык</translation>
</message> </message>
<message> <message>
<location filename="../../src/substitutionstable.cpp" line="85"/> <location filename="../../src/substitutionstable.cpp" line="81"/>
<source>Source</source> <source>Source</source>
<translation>Исходный текст</translation> <translation>Исходный текст</translation>
</message> </message>
<message> <message>
<location filename="../../src/substitutionstable.cpp" line="85"/> <location filename="../../src/substitutionstable.cpp" line="81"/>
<source>Changed</source> <source>Changed</source>
<translation>Замена</translation> <translation>Замена</translation>
</message> </message>

View File

@ -1,6 +1,7 @@
#include "corrector.h" #include "corrector.h"
#include "correctorworker.h" #include "correctorworker.h"
#include "debug.h" #include "debug.h"
#include "languagecodes.h"
#include "manager.h" #include "manager.h"
#include "settings.h" #include "settings.h"
#include "task.h" #include "task.h"
@ -72,22 +73,38 @@ QString Corrector::substituteUser(const QString &source,
{ {
auto result = source; auto result = source;
const auto range = settings_.userSubstitutions.equal_range(language); using It = Substitutions::const_iterator;
if (range.first == settings_.userSubstitutions.cend()) std::vector<std::pair<It, It>> 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; return result;
while (true) { while (true) {
auto bestMatch = range.first; auto bestMatch = ranges.front().first;
auto bestMatchLen = 0; auto bestMatchLen = 0;
for (auto it = range.first; it != range.second; ++it) { for (const auto &range : ranges) {
const auto &sub = it->second; for (auto it = range.first; it != range.second; ++it) {
if (!result.contains(sub.source)) const auto &sub = it->second;
continue; if (!result.contains(sub.source))
const auto len = sub.source.length(); continue;
if (len > bestMatchLen) { const auto len = sub.source.length();
bestMatchLen = len; if (len > bestMatchLen) {
bestMatch = it; bestMatchLen = len;
bestMatch = it;
}
} }
} }

View File

@ -201,6 +201,7 @@ const std::unordered_map<LanguageId, LanguageCodes::Bundle>
{I("enm"), {I("enm"), S(""), S("enm"), QT_TRANSLATE_NOOP("QObject", "English, Middle (1100-1500)")}}, {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("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("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 // clang-format on
}; };
#undef I #undef I
@ -253,3 +254,8 @@ std::vector<LanguageId> LanguageCodes::allIds()
for (const auto &code : codes_) result.push_back(code.first); for (const auto &code : codes_) result.push_back(code.first);
return result; return result;
} }
LanguageId LanguageCodes::anyLanguageId()
{
return "any";
}

View File

@ -16,6 +16,7 @@ public:
static QString tesseract(const LanguageId& id); static QString tesseract(const LanguageId& id);
static QString name(const LanguageId& id); static QString name(const LanguageId& id);
static std::vector<LanguageId> allIds(); static std::vector<LanguageId> allIds();
static LanguageId anyLanguageId();
private: private:
struct Bundle { struct Bundle {

View File

@ -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()); std::sort(strings.begin(), strings.end());
substitutionLanguages_->setStringList(strings); substitutionLanguages_->setStringList(strings);
} }