KDECore
globals.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2008 Zack Rusin <zack@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 * 02110-1301 USA 00018 */ 00019 #include "globals.h" 00020 00021 #include "speller.h" 00022 #include "filter_p.h" 00023 #include "loader_p.h" 00024 #include "settings_p.h" 00025 00026 #include <kconfig.h> 00027 00028 #include <QMap> 00029 #include <QVector> 00030 #include <QSet> 00031 #include <QDebug> 00032 00033 #include <memory> 00034 00035 namespace Sonnet { 00036 /* a very silly implementation: uses all available dictionaries 00037 * to check the text, the one with the least amount of misspelling 00038 * is likely the language we're looking for. (unless of course 00039 * someone is a real terrible speller). 00040 */ 00041 QString detectLanguage(const QString &sentence) 00042 { 00043 Speller speller; 00044 QMap<QString, QString> dicts = speller.availableDictionaries(); 00045 QMap<QString, int> correctHits; 00046 QSet<QString> seenLangs; 00047 { 00048 QMap<QString, QString>::const_iterator itr = dicts.constBegin(); 00049 for (int i = 0; i < dicts.count(); ++i) { 00050 seenLangs.insert(itr.value()); 00051 ++itr; 00052 } 00053 } 00054 00055 QVector<Speller> spellers(seenLangs.count()); 00056 { 00057 QSet<QString>::const_iterator itr = seenLangs.constBegin(); 00058 for (int i = 0; i < spellers.count(); ++i) { 00059 spellers[i].setLanguage(*itr); 00060 ++itr; 00061 } 00062 } 00063 00064 Filter f; 00065 f.setBuffer(sentence); 00066 while (!f.atEnd()) { 00067 Word word = f.nextWord(); 00068 00069 if (!word.word.isEmpty()) { 00070 for (int i = 0; i < spellers.count(); ++i) { 00071 if (spellers[i].isCorrect(word.word)) 00072 correctHits[spellers[i].language()] += 1; 00073 } 00074 } 00075 } 00076 00077 QMap<QString, int>::const_iterator max = correctHits.constBegin(); 00078 for (QMap<QString, int>::const_iterator itr = correctHits.constBegin(); 00079 itr != correctHits.constEnd(); ++itr) { 00080 if (itr.value() > max.value()) 00081 max = itr; 00082 } 00083 return max.key(); 00084 } 00085 00086 QString defaultLanguageName() 00087 { 00088 Loader *loader = Loader::openLoader(); 00089 KConfig config(QString::fromLatin1("sonnetrc")); 00090 loader->settings()->restore(&config); 00091 00092 return loader->languageNameForCode(loader->settings()->defaultLanguage()); 00093 } 00094 00095 }
KDE 4.6 API Reference