10#include "guesslanguage.h"
11#include "languagefilter_p.h"
13#include "settingsimpl_p.h"
18#define MIN_RELIABILITY 0.1
21class LanguageFilterPrivate
24 LanguageFilterPrivate(AbstractTokenizer *s)
27 gl.setLimits(MAX_ITEMS, MIN_RELIABILITY);
30 ~LanguageFilterPrivate()
35 QString mainLanguage()
const;
37 AbstractTokenizer *source =
nullptr;
40 mutable QString lastLanguage;
41 mutable QString cachedMainLanguage;
48QString LanguageFilterPrivate::mainLanguage()
const
50 if (cachedMainLanguage.isNull()) {
51 cachedMainLanguage = gl.identify(source->buffer(), QStringList(Loader::openLoader()->settings()->defaultLanguage()));
53 return cachedMainLanguage;
58LanguageFilter::LanguageFilter(AbstractTokenizer *source)
59 : d(new LanguageFilterPrivate(source))
61 d->prevLanguage = Loader::openLoader()->settings()->defaultLanguage();
64LanguageFilter::LanguageFilter(
const LanguageFilter &other)
65 : d(new LanguageFilterPrivate(other.d->source))
67 d->lastToken = other.d->lastToken;
68 d->lastLanguage = other.d->lastLanguage;
69 d->cachedMainLanguage = other.d->cachedMainLanguage;
70 d->prevLanguage = other.d->prevLanguage;
73LanguageFilter::~LanguageFilter() =
default;
75bool LanguageFilter::hasNext()
const
77 return d->source->hasNext();
80void LanguageFilter::setBuffer(
const QString &buffer)
82 d->cachedMainLanguage =
QString();
83 d->source->setBuffer(buffer);
86Token LanguageFilter::next()
88 d->lastToken = d->source->next();
89 d->prevLanguage = d->lastLanguage;
94QString LanguageFilter::language()
const
96 if (d->lastLanguage.isNull()) {
97 d->lastLanguage = d->gl.identify(d->lastToken.toString(),
QStringList() << d->prevLanguage << Loader::openLoader()->settings()->defaultLanguage());
99 const QStringList available = d->sp.availableLanguages();
102 if (!available.
contains(d->lastLanguage)) {
103 for (
const QString &lang : available) {
104 if (lang.startsWith(d->lastLanguage)) {
105 d->lastLanguage = lang;
111 return d->lastLanguage;
114bool LanguageFilter::isSpellcheckable()
const
116 const QString &lastlang = language();
121 if (d->sp.availableLanguages().contains(lastlang)) {
128QString LanguageFilter::buffer()
const
130 return d->source->buffer();
133void LanguageFilter::replace(
int position,
int len,
const QString &newWord)
135 d->source->
replace(position, len, newWord);
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const