Verified Commit 10b7007c authored by Fushan Wen's avatar Fushan Wen
Browse files

runners/spellchecker: Add automatic language detection of search term

If the default dictionary returns an empty result, try to iterate over
all available dictionaries (except the default one) to get the suggestion.

FEATURE: 439544
parent e567213e
......@@ -180,9 +180,10 @@ void SpellCheckRunner::match(RunnerContext &context)
// Pointer to speller object with our chosen language
QSharedPointer<Sonnet::Speller> speller = m_spellers[QString()];
QString lang;
if (speller->isValid()) {
QStringList terms = query.split(QLatin1Char(' '), Qt::SkipEmptyParts);
const QString lang = findLang(terms);
lang = findLang(terms);
// If we found a language, create a new speller object using it.
if (!lang.isEmpty()) {
// First term is the language
......@@ -206,26 +207,53 @@ void SpellCheckRunner::match(RunnerContext &context)
}
if (speller->isValid()) {
QStringList suggestions;
const bool correct = speller->checkAndSuggest(query, suggestions);
if (correct) {
QueryMatch match(this);
match.setType(QueryMatch::ExactMatch);
match.setIconName(QStringLiteral("checkbox"));
match.setText(query);
match.setSubtext(i18nc("Term is spelled correctly", "Correct"));
match.setData(query);
context.addMatch(match);
} else {
for (const auto &suggestion : std::as_const(suggestions)) {
const auto fillMatch = [this, &context, &query, &speller](const QString &langCode = QString()) {
if (!langCode.isEmpty()) {
speller->setLanguage(langCode);
}
QStringList suggestions;
const bool correct = speller->checkAndSuggest(query, suggestions);
if (correct) {
QueryMatch match(this);
match.setType(QueryMatch::ExactMatch);
match.setIconName(QStringLiteral("edit-rename"));
match.setText(suggestion);
match.setSubtext(i18n("Suggested term"));
match.setData(suggestion);
match.setIconName(QStringLiteral("checkbox"));
match.setText(query);
match.setSubtext(i18nc("Term is spelled correctly", "Correct"));
match.setData(query);
context.addMatch(match);
} else if (!suggestions.isEmpty()) {
for (const auto &suggestion : std::as_const(suggestions)) {
QueryMatch match(this);
match.setType(QueryMatch::ExactMatch);
match.setIconName(QStringLiteral("edit-rename"));
match.setText(suggestion);
match.setSubtext(i18n("Suggested term"));
match.setData(suggestion);
context.addMatch(match);
}
} else {
return false;
}
return true;
};
if (!fillMatch() && lang.isEmpty() && m_availableLangCodes.count() >= 2) {
// Perhaps the term is not in the default dictionary, try other dictionaries.
const QString defaultLangCode = speller->language();
for (const QString &langCode : std::as_const(m_availableLangCodes)) {
if (langCode == defaultLangCode) {
continue;
}
if (fillMatch(langCode)) {
// The dictionary returns valid results
break;
}
}
// No need to reset the default language as the speller will be reset in destroydata()
}
} else {
QueryMatch match(this);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment