kiten/lib
dictFileDeinflect.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00039
00040
00041
00042
00043
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
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
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
00091
00092
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
00105
00106 for(QString text = t.readLine(); !text.isEmpty(); text = t.readLine())
00107 {
00108 if(text.at(0) != '#')
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