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

kiten/lib

  • sources
  • kde-4.14
  • kdeedu
  • kiten
  • lib
  • DictEdict
deinflection.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * This file is part of Kiten, a KDE Japanese Reference Tool *
3  * Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com> *
4  * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> *
5  * Copyright (C) 2006 Eric Kjeldergaard <kjelderg@gmail.com> *
6  * Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com> *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Library General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Library General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Library General Public License *
19  * along with this library; see the file COPYING.LIB. If not, write to *
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
21  * Boston, MA 02110-1301, USA. *
22  *****************************************************************************/
23 
24 #include "deinflection.h"
25 
26 #include "dictfileedict.h"
27 #include "dictquery.h"
28 #include "entryedict.h"
29 #include "entrylist.h"
30 
31 #include <KDebug>
32 #include <KLocale>
33 #include <KMessageBox>
34 #include <KStandardDirs>
35 
36 #include <QFile>
37 #include <QHash>
38 #include <QList>
39 #include <QRegExp>
40 #include <QString>
41 #include <QTextCodec>
42 #include <QTextStream>
43 #include <QVector>
44 
45 //This is a very primative form of information hiding
46 //But C++ can get stupid with static QT objects...
47 //So this turns out to be much, much easier
48 //TODO: Fix this for thread safety/functionality (I'm presuming it's broken atm)
49 
50 //Declare our constants
51 QList<Deinflection::Conjugation> *Deinflection::conjugationList = NULL;
52 
53 Deinflection::Deinflection( const QString name )
54 : m_deinflectionLabel( QString() )
55 , m_wordType( QString() )
56 , m_dictionaryName( name )
57 {
58 }
59 
60 QString* Deinflection::getDeinflectionLabel()
61 {
62  return &m_deinflectionLabel;
63 }
64 
65 QString* Deinflection::getWordType()
66 {
67  return &m_wordType;
68 }
69 
70 EntryList* Deinflection::search( const DictQuery &query, const QVector<QString> &preliminaryResults )
71 {
72  if( conjugationList == NULL )
73  {
74  return NULL;
75  }
76 
77  m_deinflectionLabel = QString();
78  m_wordType = QString();
79 
80  EntryList *entries = new EntryList();
81 
82  QStringList edictTypesList;
83  edictTypesList.append( EdictFormatting::Adjectives );
84  edictTypesList.append( EdictFormatting::Verbs );
85 
86  QString edictTypes = edictTypesList.join( "," );
87 
88  foreach( const QString &item, preliminaryResults )
89  {
90  EntryEdict *entry = makeEntry( item );
91  QStringListIterator it( entry->getTypesList() );
92  bool matched = false;
93  while( it.hasNext() && ! matched )
94  {
95  if( edictTypes.contains( it.next() ) )
96  {
97  entries->append( entry );
98  matched = true;
99  }
100  }
101  if (! matched) delete entry;
102  }
103 
104  EntryList *results = new EntryList();
105  EntryList::EntryIterator it( *entries );
106  while( it.hasNext() )
107  {
108  EntryEdict *entry = static_cast<EntryEdict*>( it.next() );
109 
110  QString text = query.getWord();
111  if( text.isEmpty() )
112  {
113  text = query.getPronunciation();
114 
115  if( text.isEmpty() )
116  {
117  entries->deleteAll();
118  delete entries;
119  delete results;
120  return NULL;
121  }
122  }
123 
124  QString word = entry->getWord();
125  foreach( const Deinflection::Conjugation &conj, *conjugationList )
126  {
127  if( text.endsWith( conj.ending )
128  && word.endsWith( conj.replace )
129  && text.startsWith( word.left( word.length() - conj.replace.length() ) ) )
130  {
131  QString replacement = text;
132  replacement.truncate( text.length() - conj.ending.length() );
133  replacement += conj.replace;
134 
135  if( word == replacement )
136  {
137  if( m_deinflectionLabel.isEmpty() )
138  {
139  m_deinflectionLabel = conj.label;
140  }
141 
142  if( m_wordType.isEmpty() )
143  {
144  if( entry->isVerb() )
145  {
146  m_wordType = i18n( "verb" );
147  }
148  else if( entry->isAdjective() )
149  {
150  m_wordType = i18n( "adjective" );
151  }
152  }
153 
154  results->append( entry );
155  break;
156  }
157  }
158  }
159  }
160  delete entries;
161  return results;
162 }
163 
164 bool Deinflection::load()
165 {
166  if ( conjugationList != NULL )
167  {
168  return true;
169  }
170 
171  conjugationList = new QList<Conjugation>;
172 
173  QString vconj;
174  KStandardDirs *dirs = KGlobal::dirs();
175  vconj = dirs->findResource( "data", "kiten/vconj" );
176 
177  //Find the file
178  if ( vconj.isEmpty() )
179  {
180  KMessageBox::error( 0, i18n( "Verb deinflection information not found, so verb deinflection cannot be used." ) );
181  return false;
182  }
183 
184  QHash<unsigned long,QString> names;
185  //Open the file
186  QFile f( vconj );
187  if ( ! f.open( QIODevice::ReadOnly ) )
188  {
189  KMessageBox::error( 0, i18n( "Verb deinflection information could not be loaded, so verb deinflection cannot be used." ) );
190  return false;
191  }
192 
193  QTextStream t( &f );
194  t.setCodec( QTextCodec::codecForName( "eucJP" ) );
195 
196  //The file starts out with a number -> name list of the conjugation types
197  //In the format "#[#] NAME\n"
198  //The next section beginning is flagged with a $ at the beginning of the line
199  for( QString text = t.readLine(); ! t.atEnd() && text.at( 0 ) != '$';
200  text = t.readLine() )
201  {
202  if( text.at( 0 ) != '#' )
203  {
204  unsigned long number = text.left( 2 ).trimmed().toULong();
205  QString name = text.right( text.length() - 2 ).trimmed();
206  names[ number ] = name;
207  }
208  }
209 
210  //Now for the actual conjugation data
211  //Format is "NUMBER_FROM_LIST_ABOVE ENDING_TO_REPLACE\n"
212  QString replacement = QString();
213  for( QString text = t.readLine(); ! t.atEnd(); text = t.readLine() )
214  {
215  if( ! text.isEmpty() && text.at( 0 ) == '$' )
216  {
217  replacement = text.right( 1 ).trimmed();
218  }
219  else if( ! text.trimmed().isEmpty() && text.at( 0 ) != '#' )
220  {
221  unsigned long labelIndex = text.section( ' ', 0, 1 ).trimmed().toULong();
222 
223  Conjugation conj;
224  conj.label = names.value( labelIndex );
225  conj.ending = text.section( ' ', 2 ).trimmed();
226  conj.replace = replacement;
227 
228  conjugationList->append( conj );
229  }
230  }
231 
232  f.close();
233 
234  return true;
235 }
236 
237 inline EntryEdict* Deinflection::makeEntry( QString entry )
238 {
239  return new EntryEdict( m_dictionaryName, entry );
240 }
QTextStream::setCodec
void setCodec(QTextCodec *codec)
QString::toULong
ulong toULong(bool *ok, int base) const
QString::truncate
void truncate(int position)
QListIterator::next
const T & next()
QTextStream::readLine
QString readLine(qint64 maxlen)
Deinflection::search
EntryList * search(const DictQuery &query, const QVector< QString > &preliminaryResults)
Definition: deinflection.cpp:70
deinflection.h
DictQuery::getPronunciation
QString getPronunciation() const
Accessor for the Pronunciation field (generally kana)
Definition: dictquery.cpp:528
Entry::getWord
QString getWord() const
Get the word from this Entry.
Definition: entry.cpp:100
Deinflection::getDeinflectionLabel
QString * getDeinflectionLabel()
Definition: deinflection.cpp:60
QStringList::join
QString join(const QString &separator) const
Deinflection::load
bool load()
Definition: deinflection.cpp:164
QFile
QTextStream
entryedict.h
EntryEdict::isAdjective
bool isAdjective() const
Definition: entryedict.cpp:76
EdictFormatting::Adjectives
QStringList Adjectives
Definition: entryedict.cpp:478
QList::append
void append(const T &value)
QTextStream::atEnd
bool atEnd() const
EntryEdict
Definition: entryedict.h:60
EdictFormatting::Verbs
QStringList Verbs
Definition: entryedict.cpp:483
QHash
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
DictQuery::getWord
QString getWord() const
Accessor for the Word/Kanji field (this is usually used for anything containing kanji).
Definition: dictquery.cpp:554
QString
QList
EntryEdict::getTypesList
QStringList getTypesList() const
Simple accessor.
Definition: entryedict.cpp:71
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QString::right
QString right(int n) const
QHash::value
const T value(const Key &key) const
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
EntryEdict::isVerb
bool isVerb() const
Definition: entryedict.cpp:203
QFile::close
virtual void close()
QString::replace
QString & replace(int position, int n, QChar after)
EntryList::deleteAll
void deleteAll()
Delete all Entry objects in our list.
Definition: entrylist.cpp:99
Deinflection::getWordType
QString * getWordType()
Definition: deinflection.cpp:65
QVector
QTextCodec::codecForName
QTextCodec * codecForName(const QByteArray &name)
DictQuery
A class to allow users of libkiten to properly setup a database query.
Definition: dictquery.h:89
QString::length
int length() const
QString::section
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
QString::left
QString left(int n) const
dictquery.h
QListIterator
Deinflection::Deinflection
Deinflection(const QString name)
Definition: deinflection.cpp:53
dictfileedict.h
EntryList
EntryList is a simple container for Entry objects, and is-a QList A few simple overrides allo...
Definition: entrylist.h:38
entrylist.h
QListIterator::hasNext
bool hasNext() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kiten/lib

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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