• 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.12
  • 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  }
102 
103  EntryList *results = new EntryList();
104  EntryList::EntryIterator it( *entries );
105  while( it.hasNext() )
106  {
107  EntryEdict *entry = static_cast<EntryEdict*>( it.next() );
108 
109  QString text = query.getWord();
110  if( text.isEmpty() )
111  {
112  text = query.getPronunciation();
113 
114  if( text.isEmpty() )
115  {
116  return NULL;
117  }
118  }
119 
120  QString word = entry->getWord();
121  foreach( const Deinflection::Conjugation &conj, *conjugationList )
122  {
123  if( text.endsWith( conj.ending )
124  && word.endsWith( conj.replace )
125  && text.startsWith( word.left( word.length() - conj.replace.length() ) ) )
126  {
127  QString replacement = text;
128  replacement.truncate( text.length() - conj.ending.length() );
129  replacement += conj.replace;
130 
131  if( word == replacement )
132  {
133  if( m_deinflectionLabel.isEmpty() )
134  {
135  m_deinflectionLabel = conj.label;
136  }
137 
138  if( m_wordType.isEmpty() )
139  {
140  if( entry->isVerb() )
141  {
142  m_wordType = i18n( "verb" );
143  }
144  else if( entry->isAdjective() )
145  {
146  m_wordType = i18n( "adjective" );
147  }
148  }
149 
150  results->append( entry );
151  break;
152  }
153  }
154  }
155  }
156 
157  return results;
158 }
159 
160 bool Deinflection::load()
161 {
162  if ( conjugationList != NULL )
163  {
164  return true;
165  }
166 
167  conjugationList = new QList<Conjugation>;
168 
169  QString vconj;
170  KStandardDirs *dirs = KGlobal::dirs();
171  vconj = dirs->findResource( "data", "kiten/vconj" );
172 
173  //Find the file
174  if ( vconj.isEmpty() )
175  {
176  KMessageBox::error( 0, i18n( "Verb deinflection information not found, so verb deinflection cannot be used." ) );
177  return false;
178  }
179 
180  QHash<unsigned long,QString> names;
181  //Open the file
182  QFile f( vconj );
183  if ( ! f.open( QIODevice::ReadOnly ) )
184  {
185  KMessageBox::error( 0, i18n( "Verb deinflection information could not be loaded, so verb deinflection cannot be used." ) );
186  return false;
187  }
188 
189  QTextStream t( &f );
190  t.setCodec( QTextCodec::codecForName( "eucJP" ) );
191 
192  //The file starts out with a number -> name list of the conjugation types
193  //In the format "#[#] NAME\n"
194  //The next section beginning is flagged with a $ at the beginning of the line
195  for( QString text = t.readLine(); ! t.atEnd() && text.at( 0 ) != '$';
196  text = t.readLine() )
197  {
198  if( text.at( 0 ) != '#' )
199  {
200  unsigned long number = text.left( 2 ).trimmed().toULong();
201  QString name = text.right( text.length() - 2 ).trimmed();
202  names[ number ] = name;
203  }
204  }
205 
206  //Now for the actual conjugation data
207  //Format is "NUMBER_FROM_LIST_ABOVE ENDING_TO_REPLACE\n"
208  QString replacement = QString();
209  for( QString text = t.readLine(); ! t.atEnd(); text = t.readLine() )
210  {
211  if( ! text.isEmpty() && text.at( 0 ) == '$' )
212  {
213  replacement = text.right( 1 ).trimmed();
214  }
215  else if( ! text.trimmed().isEmpty() && text.at( 0 ) != '#' )
216  {
217  unsigned long labelIndex = text.section( ' ', 0, 1 ).trimmed().toULong();
218 
219  Conjugation conj;
220  conj.label = names.value( labelIndex );
221  conj.ending = text.section( ' ', 2 ).trimmed();
222  conj.replace = replacement;
223 
224  conjugationList->append( conj );
225  }
226  }
227 
228  f.close();
229 
230  return true;
231 }
232 
233 inline EntryEdict* Deinflection::makeEntry( QString entry )
234 {
235  return new EntryEdict( m_dictionaryName, entry );
236 }
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
Deinflection::load
bool load()
Definition: deinflection.cpp:160
entryedict.h
EntryEdict::isAdjective
bool isAdjective() const
Definition: entryedict.cpp:76
EdictFormatting::Adjectives
QStringList Adjectives
Definition: entryedict.cpp:478
EntryEdict
Definition: entryedict.h:60
EdictFormatting::Verbs
QStringList Verbs
Definition: entryedict.cpp:483
DictQuery::getWord
QString getWord() const
Accessor for the Word/Kanji field (this is usually used for anything containing kanji).
Definition: dictquery.cpp:554
EntryEdict::getTypesList
QStringList getTypesList() const
Simple accessor.
Definition: entryedict.cpp:71
EntryList::EntryIterator
QListIterator< Entry * > EntryIterator
A simple overridden iterator for working with the Entries.
Definition: entrylist.h:44
EntryEdict::isVerb
bool isVerb() const
Definition: entryedict.cpp:203
Deinflection::getWordType
QString * getWordType()
Definition: deinflection.cpp:65
DictQuery
A class to allow users of libkiten to properly setup a database query.
Definition: dictquery.h:89
dictquery.h
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
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:54 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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