9#include "hunspelldict.h"
11#include "config-hunspell.h"
12#include "hunspelldebug.h"
17#include <QStandardPaths>
22HunspellDict::HunspellDict(
const QString &lang,
const std::shared_ptr<Hunspell> &speller)
26 qCWarning(SONNET_HUNSPELL) <<
"Can't create a client without a speller";
30 if (!m_decoder.isValid()) {
31 qCWarning(SONNET_HUNSPELL) <<
"Failed to find a text codec for name" << speller->get_dic_encoding() <<
"defaulting to locale text codec";
33 Q_ASSERT(m_decoder.isValid());
36 if (!m_encoder.isValid()) {
37 qCWarning(SONNET_HUNSPELL) <<
"Failed to find a text codec for name" << speller->get_dic_encoding() <<
"defaulting to locale text codec";
39 Q_ASSERT(m_encoder.isValid());
44 QFile userDicFile(userDic);
46 qCDebug(SONNET_HUNSPELL) <<
"Load a user dictionary" << userDic;
48 while (!userDicIn.atEnd()) {
49 const QString word = userDicIn.readLine();
56 speller->add_with_affix(toDictEncoding(wordParts.
at(0)).constData(), toDictEncoding(wordParts.
at(1)).constData());
59 speller->remove(toDictEncoding(word.
mid(1)).constData());
61 speller->add(toDictEncoding(word).constData());
68std::shared_ptr<Hunspell> HunspellDict::createHunspell(
const QString &lang,
QString path)
70 qCDebug(SONNET_HUNSPELL) <<
"Loading dictionary for" << lang <<
"from" <<
path;
73 path += QLatin1Char(
'/');
76 QString dictionary =
path + QStringLiteral(
".dic");
77 QString aff =
path + QStringLiteral(
".aff");
80 qCWarning(SONNET_HUNSPELL) <<
"Unable to find dictionary for" << lang <<
"in path" <<
path;
85 qCDebug(SONNET_HUNSPELL) <<
"Created " << speller.get();
90HunspellDict::~HunspellDict()
96 if (m_encoder.isValid()) {
97 return m_encoder.encode(word);
102bool HunspellDict::isCorrect(
const QString &word)
const
104 qCDebug(SONNET_HUNSPELL) <<
" isCorrect :" << word;
109#if USE_OLD_HUNSPELL_API
110 int result = m_speller->spell(toDictEncoding(word).constData());
111 qCDebug(SONNET_HUNSPELL) <<
" result :" << result;
114 bool result = m_speller->spell(toDictEncoding(word).toStdString());
115 qCDebug(SONNET_HUNSPELL) <<
" result :" << result;
123 return QStringList();
127#if USE_OLD_HUNSPELL_API
129 int nbWord = m_speller->suggest(&selection, toDictEncoding(word).constData());
130 for (
int i = 0; i < nbWord; ++i) {
131 lst << m_decoder.decode(selection[i]);
133 m_speller->free_list(&selection, nbWord);
135 const auto suggestions = m_speller->suggest(toDictEncoding(word).toStdString());
136 for_each(suggestions.begin(), suggestions.end(), [
this, &lst](
const std::string &suggestion) {
137 lst << m_decoder.decode(suggestion.c_str());
144bool HunspellDict::storeReplacement(
const QString &bad,
const QString &good)
151 qCDebug(SONNET_HUNSPELL) <<
"HunspellDict::storeReplacement not implemented";
155bool HunspellDict::addToPersonal(
const QString &word)
160 m_speller->add(toDictEncoding(word).constData());
161 const QString userDic =
QDir::home().
filePath(QLatin1String(
".hunspell_") % language());
162 QFile userDicFile(userDic);
164 QTextStream out(&userDicFile);
173bool HunspellDict::addToSession(
const QString &word)
178 int r = m_speller->add(toDictEncoding(word).constData());
QString path(const QString &relativePath)
const char * constData() const const
QString filePath(const QString &fileName) const const
bool exists() const const
const_reference at(qsizetype i) const const
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QByteArray toLocal8Bit() const const