moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kiten / deinf.cpp
blobbacc403cfdfe86205f51a9477fbc89a3cdc36afc
1 /**
2 This file is part of Kiten, a KDE Japanese Reference Tool...
3 Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA
19 **/
21 #include <kdebug.h>
22 #include <kstandarddirs.h>
23 #include <kmessagebox.h>
24 #include <klocale.h>
25 #include <qfile.h>
26 #include <qregexp.h>
27 #include <qtextcodec.h>
28 #include "deinf.h"
30 Deinf::Index::Index()
32 loaded = false;
35 void Deinf::Index::load()
37 if (loaded)
38 return;
40 KStandardDirs *dirs = KGlobal::dirs();
41 QString vconj = dirs->findResource("data", "kiten/vconj");
42 if (vconj.isNull())
44 KMessageBox::error(0, i18n("Verb deinflection information not found, so verb deinflection cannot be used."));
45 return;
48 QFile f(vconj);
50 if (!f.open(IO_ReadOnly))
52 KMessageBox::error(0, i18n("Verb deinflection information could not be loaded, so verb deinflection cannot be used."));
53 return;
56 QTextStream t(&f);
57 t.setCodec(QTextCodec::codecForName("eucJP"));
59 for(QString text = t.readLine(); !t.eof() && text.at(0) != '$'; text = t.readLine())
61 if(text.at(0) != '#')
63 unsigned int number = text.left(2).stripWhiteSpace().toUInt();
64 QString name = text.right(text.length() - 2).stripWhiteSpace();
66 names[number] = name;
69 for(QString text = t.readLine(); !text.isEmpty(); text = t.readLine())
71 if(text.at(0) != '#')
73 QStringList things(QStringList::split(QChar('\t'), text));
75 Conjugation conj;
76 conj.ending = things.first();
77 conj.replace = (*things.at(1));
78 conj.num = things.last().toUInt();
80 list.append(conj);
84 f.close();
85 loaded = true;
88 namespace
90 QStringList possibleConjugations(const QString &text)
92 QStringList endings;
93 for (unsigned i = 0; i < text.length(); ++i)
94 endings.append(text.right(i));
95 return endings;
99 QStringList Deinf::Index::deinflect(const QString &text, QStringList &name)
101 load();
102 QStringList endings = possibleConjugations(text);
103 QStringList ret;
105 for (QValueListIterator <Conjugation> it = list.begin(); it != list.end(); ++it)
107 QStringList matches(endings.grep(QRegExp(QString("^") + (*it).ending)));
109 if (matches.size() > 0) // a match
111 name.append(names[(*it).num]);
113 //kdDebug() << "match ending: " << (*it).ending << "; replace: " << (*it).replace << "; name: " << names[(*it).num] << endl;
115 QString tmp(text);
116 tmp.replace(QRegExp((*it).ending + "*", false, true), (*it).replace);
117 ret.append(tmp);
121 return ret;