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>
<translation>Дневнегреческий</translation>
</message>
<message>
<location filename="../../src/languagecodes.cpp" line="204"/>
<source>Any</source>
<translation>Любой язык</translation>
</message>
<message>
<location filename="../../src/manager.cpp" line="42"/>
<source>app</source>
@ -1055,17 +1060,17 @@ in %1</source>
<context>
<name>SubstitutionsTable</name>
<message>
<location filename="../../src/substitutionstable.cpp" line="85"/>
<location filename="../../src/substitutionstable.cpp" line="81"/>
<source>Language</source>
<translation>Язык</translation>
</message>
<message>
<location filename="../../src/substitutionstable.cpp" line="85"/>
<location filename="../../src/substitutionstable.cpp" line="81"/>
<source>Source</source>
<translation>Исходный текст</translation>
</message>
<message>
<location filename="../../src/substitutionstable.cpp" line="85"/>
<location filename="../../src/substitutionstable.cpp" line="81"/>
<source>Changed</source>
<translation>Замена</translation>
</message>

View File

@ -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<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;
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;
}
}
}

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("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<LanguageId> LanguageCodes::allIds()
for (const auto &code : codes_) result.push_back(code.first);
return result;
}
LanguageId LanguageCodes::anyLanguageId()
{
return "any";
}

View File

@ -16,6 +16,7 @@ public:
static QString tesseract(const LanguageId& id);
static QString name(const LanguageId& id);
static std::vector<LanguageId> allIds();
static LanguageId anyLanguageId();
private:
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());
substitutionLanguages_->setStringList(strings);
}