• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.12
  • kdesdk
  • lokalize
  • src
  • common
stemming.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2009-2011 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 #include "stemming.h"
25 
26 #include <QMap>
27 #include <QFileInfo>
28 #include <QMutex>
29 #include <QMutexLocker>
30 #include <QDebug>
31 
32 QString enhanceLangCode(const QString& langCode)
33 {
34  if (langCode.length()!=2)
35  return langCode;
36 
37  return QLocale(langCode).name();
38 }
39 
40 
41 #ifdef HAVE_HUNSPELL
42 #include <hunspell/hunspell.hxx>
43 #include <QTextCodec>
44 
45 struct SpellerAndCodec
46 {
47  Hunspell* speller;
48  QTextCodec* codec;
49  SpellerAndCodec():speller(0){}
50  SpellerAndCodec(const QString& langCode);
51 };
52 
53 SpellerAndCodec::SpellerAndCodec(const QString& langCode)
54 : speller(0)
55 {
56  QString dic=QString("/usr/share/myspell/dicts/%1.dic").arg(langCode);
57  if (!QFileInfo(dic).exists())
58  dic=QString("/usr/share/myspell/dicts/%1.dic").arg(enhanceLangCode(langCode));;
59  if (QFileInfo(dic).exists())
60  {
61  speller = new Hunspell(QString("/usr/share/myspell/dicts/%1.aff").arg(langCode).toUtf8().constData(),dic.toUtf8().constData());
62  codec=QTextCodec::codecForName(speller->get_dic_encoding());
63  if (!codec)
64  codec=QTextCodec::codecForLocale();
65  }
66 }
67 
68 static QMap<QString,SpellerAndCodec> hunspellers;
69 
70 #endif
71 
72 QString stem(const QString& langCode, const QString& word)
73 {
74  QString result=word;
75 
76 #ifdef HAVE_HUNSPELL
77  static QMutex mutex;
78  QMutexLocker locker(&mutex);
79 
80  if (!hunspellers.contains(langCode))
81  {
82  hunspellers.insert(langCode,SpellerAndCodec(langCode));
83  }
84 
85  SpellerAndCodec sc(hunspellers.value(langCode));
86  Hunspell* speller=sc.speller;
87  if (!speller)
88  return word;
89 
90  char** result1;
91  char** result2;
92  int n1 = speller->analyze(&result1, sc.codec->fromUnicode(word));
93  int n2 = speller->stem(&result2, result1, n1);
94 
95  if (n2)
96  result=sc.codec->toUnicode(result2[0]);
97 
98  speller->free_list(&result1, n1);
99  speller->free_list(&result2, n2);
100 #endif
101 
102  return result;
103 }
104 
105 void cleanupSpellers()
106 {
107 #ifdef HAVE_HUNSPELL
108  foreach(const SpellerAndCodec& sc, hunspellers)
109  delete sc.speller;
110 
111 #endif
112 }
113 
114 
stem
QString stem(const QString &langCode, const QString &word)
Definition: stemming.cpp:72
enhanceLangCode
QString enhanceLangCode(const QString &langCode)
Definition: stemming.cpp:32
stemming.h
cleanupSpellers
void cleanupSpellers()
Definition: stemming.cpp:105
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



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