• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • sonnet
globals.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Zack Rusin <zack@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19#include "globals.h"
20
21#include "speller.h"
22#include "filter_p.h"
23#include "loader_p.h"
24#include "settings_p.h"
25
26#include <kconfig.h>
27
28#include <QMap>
29#include <QVector>
30#include <QSet>
31#include <QDebug>
32
33#include <memory>
34
35namespace Sonnet {
36/* a very silly implementation: uses all available dictionaries
37 * to check the text, the one with the least amount of misspelling
38 * is likely the language we're looking for. (unless of course
39 * someone is a real terrible speller).
40 */
41QString detectLanguage(const QString &sentence)
42{
43 Speller speller;
44 QMap<QString, QString> dicts = speller.availableDictionaries();
45 QMap<QString, int> correctHits;
46 QSet<QString> seenLangs;
47 {
48 QMap<QString, QString>::const_iterator itr = dicts.constBegin();
49 for (int i = 0; i < dicts.count(); ++i) {
50 seenLangs.insert(itr.value());
51 ++itr;
52 }
53 }
54
55 QVector<Speller> spellers(seenLangs.count());
56 {
57 QSet<QString>::const_iterator itr = seenLangs.constBegin();
58 for (int i = 0; i < spellers.count(); ++i) {
59 spellers[i].setLanguage(*itr);
60 ++itr;
61 }
62 }
63
64 Filter f;
65 f.setBuffer(sentence);
66 while (!f.atEnd()) {
67 Word word = f.nextWord();
68
69 if (!word.word.isEmpty()) {
70 for (int i = 0; i < spellers.count(); ++i) {
71 if (spellers[i].isCorrect(word.word))
72 correctHits[spellers[i].language()] += 1;
73 }
74 }
75 }
76
77 QMap<QString, int>::const_iterator max = correctHits.constBegin();
78 for (QMap<QString, int>::const_iterator itr = correctHits.constBegin();
79 itr != correctHits.constEnd(); ++itr) {
80 if (itr.value() > max.value())
81 max = itr;
82 }
83 return max.key();
84}
85
86// SLOW!!!
87// TODO: cache this value! And then use some dbus signal to notify all apps when
88// changing the default language changes.
89QString defaultLanguageName()
90{
91 Loader *loader = Loader::openLoader();
92 KConfig config(QString::fromLatin1("sonnetrc"));
93 loader->settings()->restore(&config);
94
95 return loader->languageNameForCode(loader->settings()->defaultLanguage());
96}
97
98}
KConfig
The central class of the KDE configuration data system.
Definition: kconfig.h:71
QMap
QSet
Definition: k3resolver.h:41
QString
Sonnet::Filter
Filter is used to split text into words which will be spell checked.
Definition: filter_p.h:66
Sonnet::Filter::setBuffer
void setBuffer(const QString &buffer)
Definition: filter.cpp:81
Sonnet::Filter::atEnd
bool atEnd() const
Definition: filter.cpp:92
Sonnet::Filter::nextWord
virtual Word nextWord() const
Definition: filter.cpp:168
Sonnet::Loader
Class used to deal with dictionaries.
Definition: loader_p.h:46
Sonnet::Loader::languageNameForCode
QString languageNameForCode(const QString &langCode) const
Definition: loader.cpp:128
Sonnet::Loader::settings
Settings * settings() const
Returns the Settings object used by the loader.
Definition: loader.cpp:248
Sonnet::Loader::openLoader
static Loader * openLoader()
Constructs the loader.
Definition: loader.cpp:55
Sonnet::Settings::defaultLanguage
QString defaultLanguage() const
Definition: settings.cpp:83
Sonnet::Settings::restore
void restore(KConfig *config)
Definition: settings.cpp:226
Sonnet::Speller
Spell checker object.
Definition: speller.h:39
Sonnet::Speller::availableDictionaries
QMap< QString, QString > availableDictionaries() const
Returns a map of all available language descriptions and their codes.
Definition: speller.cpp:270
filter_p.h
globals.h
kconfig.h
loader_p.h
Sonnet
The sonnet namespace.
Definition: backgroundchecker.h:34
Sonnet::detectLanguage
QString detectLanguage(const QString &sentence)
The function will return the language code for the language in which it thinks the sentence which has...
Definition: globals.cpp:41
Sonnet::defaultLanguageName
QString defaultLanguageName()
Definition: globals.cpp:89
settings_p.h
speller.h
Sonnet::Word
Structure abstracts the word and its position in the parent text.
Definition: filter_p.h:41
Sonnet::Word::word
QString word
Definition: filter_p.h:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal