Sonnet

hspelldict.cpp
1/*
2 * kspell_hspelldict.cpp
3 *
4 * SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org>
5 * SPDX-FileCopyrightText: 2005 Mashrab Kuvatov <kmashrab@uni-bremen.de>
6 * SPDX-FileCopyrightText: 2013 Martin Sandsmark <martin.sandsmark@kde.org>
7 *
8 * SPDX-License-Identifier: LGPL-2.1-or-later
9 */
10
11#include "hspelldict.h"
12
13#include "hspell_debug.h"
14
15#include <QSettings>
16
17using namespace Sonnet;
18
19HSpellDict::HSpellDict(const QString &lang)
20 : SpellerPlugin(lang)
21{
22 int int_error = hspell_init(&m_speller, HSPELL_OPT_DEFAULT);
23 if (int_error == -1) {
24 qCWarning(SONNET_LOG_HSPELL) << "HSpellDict::HSpellDict: Init failed";
25 initialized = false;
26 } else {
27 /* hspell understands only iso8859-8-i */
28 m_decoder = QStringDecoder("iso8859-8-i");
29 m_encoder = QStringEncoder("iso8859-8-i");
30 initialized = true;
31 }
32
33 QSettings settings(QStringLiteral("KDE"), QStringLiteral("SonnetHSpellPlugin"));
34 const QStringList personalWordsList = settings.value(QStringLiteral("PersonalWords"), QStringList()).toStringList();
35 m_personalWords = QSet<QString>(personalWordsList.begin(), personalWordsList.end());
36 QVariantHash replacementMap = settings.value(QStringLiteral("Replacements"), QVariant()).toHash();
37 for (const QString &key : replacementMap.keys()) {
38 m_replacements[key] = replacementMap[key].toString();
39 }
40}
41
42HSpellDict::~HSpellDict()
43{
44 /* It exists in =< hspell-0.8 */
45 if (initialized) {
46 hspell_uninit(m_speller);
47 }
48}
49
50bool HSpellDict::isCorrect(const QString &word) const
51{
52 if (m_sessionWords.contains(word)) {
53 return true;
54 }
55
56 if (m_personalWords.contains(word)) {
57 return true;
58 }
59
60 if (!initialized) {
61 // Not much we can do, so just return true (less annoying for the user)
62 return true;
63 }
64
65 int preflen;
66 QByteArray wordISO = m_encoder.encode(word);
67
68 // returns 1 if the word is correct, 0 otherwise
69 int correct = hspell_check_word(m_speller, wordISO.constData(),
70 &preflen); // this argument might be removed, it isn't useful
71
72 // gimatria is a representation of numbers with hebrew letters, we accept these
73 if (correct != 1) {
74 if (hspell_is_canonic_gimatria(wordISO.constData()) != 0) {
75 correct = 1;
76 }
77 }
78 return correct == 1;
79}
80
81QStringList HSpellDict::suggest(const QString &word) const
82{
83 QStringList suggestions;
84
85 if (m_replacements.contains(word)) {
86 suggestions.append(m_replacements[word]);
87 }
88
89 struct corlist correctionList;
90 int suggestionCount;
91 corlist_init(&correctionList);
92 const QByteArray encodedWord = m_encoder.encode(word);
93 hspell_trycorrect(m_speller, encodedWord.constData(), &correctionList);
94 for (suggestionCount = 0; suggestionCount < corlist_n(&correctionList); suggestionCount++) {
95 suggestions.append(m_decoder.decode(corlist_str(&correctionList, suggestionCount)));
96 }
97 corlist_free(&correctionList);
98 return suggestions;
99}
100
101bool HSpellDict::storeReplacement(const QString &bad, const QString &good)
102{
103 m_replacements[bad] = good;
104 storePersonalWords();
105 return true;
106}
107
108bool HSpellDict::addToPersonal(const QString &word)
109{
110 m_personalWords.insert(word);
111 storePersonalWords();
112 return true;
113}
114
115bool HSpellDict::addToSession(const QString &word)
116{
117 m_sessionWords.insert(word);
118 return true;
119}
120
121void HSpellDict::storePersonalWords()
122{
123 QSettings settings(QStringLiteral("KDE"), QStringLiteral("SonnetHSpellPlugin"));
124 const QStringList personalWordsList(m_personalWords.begin(), m_personalWords.end());
125 settings.setValue(QStringLiteral("PersonalWords"), QVariant(personalWordsList));
126 QVariantHash variantHash;
127 for (const QString &key : m_replacements.keys()) {
128 variantHash[key] = QVariant(m_replacements[key]);
129 }
130 settings.setValue(QStringLiteral("Replacements"), variantHash);
131}
The sonnet namespace.
const char * constData() const const
bool contains(const Key &key) const const
QList< Key > keys() const const
void append(QList< T > &&value)
iterator begin()
iterator end()
T value(qsizetype i) const const
iterator begin()
bool contains(const QSet< T > &other) const const
iterator end()
iterator insert(const T &value)
EncodedData< QByteArrayView > decode(QByteArrayView ba)
DecodedData< QStringView > encode(QStringView in)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.