• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kiten/lib

dictFileDeinflect.cpp

Go to the documentation of this file.
00001 /* This file is part of Kiten, a KDE Japanese Reference Tool...
00002  Copyright (C) 2001  Jason Katz-Brown <jason@katzbrown.com>
00003            (C) 2006  Joseph Kerian <jkerian@gmail.com>
00004               (C) 2006  Eric Kjeldergaard <kjelderg@gmail.com>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 #include "dictFileDeinflect.h"
00022 #include "entryDeinflect.h"
00023 #include "EntryList.h"
00024 
00025 #include <kdebug.h>
00026 #include <kstandarddirs.h>
00027 #include <kmessagebox.h>
00028 #include <klocale.h>
00029 
00030 #include <QtCore/QString>
00031 #include <QtCore/QHash>
00032 #include <QtCore/QList>
00033 #include <QtCore/QFile>
00034 #include <QtCore/QRegExp>
00035 #include <QtCore/QTextCodec>
00036 #include <QtCore/QTextStream>
00037 
00038 //This is a very primative form of information hiding
00039 //But C++ can get stupid with static QT objects...
00040 //So this turns out to be much, much easier
00041 //TODO: Fix this for thread safety/functionality (I'm presuming it's broken atm)
00042 
00043 //Declare our constants
00044 QList<dictFileDeinflect::Conjugation> *dictFileDeinflect::conjugationList = NULL;
00045 
00046 dictFileDeinflect::dictFileDeinflect() : dictFile("Deinflect") {}
00047 
00048 QStringList dictFileDeinflect::listDictDisplayOptions(QStringList orig) const {
00049     return QStringList("Deinflection");
00050 }
00051 bool dictFileDeinflect::validDictionaryFile(const QString &filename) {
00052     return false;
00053 }
00054 bool dictFileDeinflect::validQuery(const DictQuery &query) {
00055     return true;
00056 }
00057 
00058 bool dictFileDeinflect::loadDictionary(const QString &file, const QString &name)
00059 {
00060     if (conjugationList != NULL)
00061         return true;
00062 
00063     conjugationList = new QList<Conjugation>;
00064 
00065     QString vconj;
00066     if(file.isEmpty()) {
00067         KStandardDirs *dirs = KGlobal::dirs();
00068         vconj = dirs->findResource("data", "kiten/vconj");
00069         //Find the file
00070         if (vconj.isEmpty())
00071         {
00072             KMessageBox::error(0, i18n("Verb deinflection information not found, so verb deinflection cannot be used."));
00073             return false;
00074         }
00075     } else
00076         vconj = file;
00077 
00078     QHash<unsigned long,QString> names;
00079     //Open the file
00080     QFile f(vconj);
00081     if (!f.open(QIODevice::ReadOnly))
00082     {
00083         KMessageBox::error(0, i18n("Verb deinflection information could not be loaded, so verb deinflection cannot be used."));
00084         return false;
00085     }
00086 
00087     QTextStream t(&f);
00088     t.setCodec(QTextCodec::codecForName("eucJP"));
00089 
00090     //The file starts out with a number -> name list of the conjugation types
00091     //In the format "#[#].\tNAME\n"
00092     //The next section beginning is flagged with a $ at the beginning of the line
00093     for(QString text = t.readLine(); !t.atEnd() && text.at(0) != '$';
00094             text = t.readLine())
00095     {
00096         if(text.at(0) != '#')
00097         {
00098             unsigned long number = text.left(2).trimmed().toULong();
00099             QString name = text.right(text.length() - 2).trimmed();
00100             names[number] = name;
00101         }
00102     }
00103 
00104     //Now for the actual conjugation data
00105     //Format is "ENDING_TO_REPLACE\tREPLACEMENT\tNUMBER_FROM_LIST_ABOVE\n"
00106     for(QString text = t.readLine(); !text.isEmpty(); text = t.readLine())
00107     {
00108         if(text.at(0) != '#') //We still have these comments everywhere
00109         {
00110             QStringList things( text.split(QChar('\t') ));
00111 
00112             Conjugation conj;
00113             conj.ending = things.first();
00114             conj.replace = (things.at(1));
00115             conj.label = names.find(things.last().toULong()).value();
00116 
00117             conjugationList->append(conj);
00118         }
00119     }
00120 
00121     f.close();
00122 
00123     m_dictionaryName = name;
00124 
00125     return true;
00126 }
00127 
00128 
00129 EntryList *dictFileDeinflect::doSearch(const DictQuery &query)
00130 {
00131     if (conjugationList == NULL)
00132         return NULL;
00133 
00134     QString text = query.getWord();
00135     if(text.isEmpty())
00136         text = query.getPronunciation();
00137     if(text.isEmpty())
00138         return NULL;
00139 
00140     EntryList *ret = new EntryList;
00141     int index = 0;
00142     foreach(const Conjugation &it, *conjugationList)
00143     {
00144         if(text.endsWith(it.ending)) {
00145             QString replacement = text;
00146             replacement.truncate(text.length()-it.ending.length());
00147             replacement += it.replace;
00148             entryDeinflect *foo = new entryDeinflect(replacement,it.label,index++,it.ending);
00149             ret->append(foo);
00150 
00151             if(ret->count() >= 3)
00152                 return ret;
00153         }
00154     }
00155     return ret;
00156 }
00157 

kiten/lib

Skip menu "kiten/lib"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal