• 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
dictfileedict.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 "dictfileedict.h"
25 
26 #include <KApplication>
27 #include <KConfig>
28 #include <KConfigSkeleton>
29 #include <KDebug>
30 #include <KGlobal>
31 #include <KProcess>
32 #include <KStandardDirs>
33 
34 #include <QByteArray>
35 #include <QFile>
36 #include <QString>
37 #include <QTextCodec>
38 #include <QTextStream>
39 #include <QVector>
40 
41 #include "deinflection.h"
42 #include "dictfilefieldselector.h"
43 #include "dictquery.h"
44 #include "entryedict.h"
45 #include "entrylist.h"
46 #include "kitenmacros.h"
47 
48 QString *DictFileEdict::deinflectionLabel = NULL;
49 QStringList *DictFileEdict::displayFields = NULL;
50 QString *DictFileEdict::wordType = NULL;
51 
56 DictFileEdict::DictFileEdict()
57 : DictFile( EDICT )
58 , m_deinflection( 0 )
59 , m_hasDeinflection( false )
60 {
61  m_dictionaryType = EDICT;
62  m_searchableAttributes.insert( "common", "common" );
63 }
64 
69 DictFileEdict::~DictFileEdict()
70 {
71  delete m_deinflection;
72  m_deinflection = 0;
73 }
74 
75 QMap<QString,QString> DictFileEdict::displayOptions() const
76 {
77  QMap<QString,QString> list;
78  list[ "Part of speech(type)" ] = "type";
79  return list;
80 }
81 
88 EntryList *DictFileEdict::doSearch( const DictQuery &query )
89 {
90  if( query.isEmpty() || ! m_edictFile.valid() ) //No query or dict, no results.
91  {
92  return new EntryList();
93  }
94 
95  kDebug()<< "Search from : " << getName();
96 
97  QString firstChoice = query.getWord();
98  if( firstChoice.length() == 0 )
99  {
100  firstChoice = query.getPronunciation();
101  if( firstChoice.length() == 0 )
102  {
103  firstChoice = query.getMeaning().split( ' ' ).first().toLower();
104  if( firstChoice.length() == 0 )
105  {
106  //The nastiest situation... we have to assemble a search string
107  //from the first property
108  QList<QString> keys = query.listPropertyKeys();
109  if( keys.size() == 0 ) //Shouldn't happen... but maybe in the future
110  {
111  return new EntryList();
112  }
113  firstChoice = keys[ 0 ];
114  firstChoice = firstChoice + query.getProperty( firstChoice );
115  //TODO: doSearch: some accomodation for searching for ranges and such of properties
116  }
117  }
118  }
119  else
120  {
121  // Only search for one kanji or the
122  // binary lookup mechanism breaks
123  firstChoice = firstChoice.at( 0 );
124  }
125 
126  QVector<QString> preliminaryResults = m_edictFile.findMatches( firstChoice );
127 
128  if( preliminaryResults.size() == 0 ) //If there were no matches... return an empty list
129  {
130  return new EntryList();
131  }
132 
133  EntryList *results = new EntryList();
134  foreach( const QString &it, preliminaryResults )
135  {
136 // kDebug() << "result: " << it << endl;
137  Entry *result = makeEntry( it );
138  EntryEdict *resultEdict = static_cast<EntryEdict*>( result );
139  if( result->matchesQuery( query ) && resultEdict->matchesWordType( query ) )
140  {
141  results->append( result );
142  }
143  else
144  {
145  delete result;
146  }
147  }
148 
149  // At this point we should have some preliminary results
150  // and if there were no matches, it probably means the user
151  // input was a verb or adjective, so we have to deinflect it.
152  bool isAnyQuery = query.getMatchWordType() == DictQuery::Any;
153  bool isVerbQuery = query.getMatchWordType() == DictQuery::Verb;
154  bool isAdjectiveQuery = query.getMatchWordType() == DictQuery::Adjective;
155  if( results->count() == 0 && ( isAnyQuery || isVerbQuery || isAdjectiveQuery ) )
156  {
157  delete results;
158  results = m_deinflection->search( query, preliminaryResults );
159  QString *label = m_deinflection->getDeinflectionLabel();
160  if( ! label->isEmpty() && ! m_hasDeinflection )
161  {
162  deinflectionLabel = label;
163  m_hasDeinflection = true;
164  wordType = m_deinflection->getWordType();
165  }
166  }
167  else
168  {
169  deinflectionLabel = NULL;
170  wordType = NULL;
171  m_hasDeinflection = false;
172  }
173 
174  if( results )
175  {
176  EntryList *common = new EntryList();
177  EntryList *uncommon = new EntryList();
178  EntryList::EntryIterator i( *results );
179  while( i.hasNext() )
180  {
181  EntryEdict *entry = static_cast<EntryEdict*>( i.next() );
182  if( entry->isCommon() )
183  {
184  common->append( entry );
185  }
186  else
187  {
188  uncommon->append( entry );
189  }
190  }
191 
192  delete results;
193  results = new EntryList();
194  results->appendList( common );
195  results->appendList( uncommon );
196 
197  EntryList *exact = new EntryList();
198  EntryList *beginning = new EntryList();
199  EntryList *ending = new EntryList();
200  EntryList *anywhere = new EntryList();
201  EntryList::EntryIterator it( *results );
202  while( it.hasNext() )
203  {
204  Entry *entry = it.next();
205 
206  if( entry->getWord() == query.getWord() )
207  {
208  exact->append( entry );
209  }
210  else if( entry->getWord().startsWith( query.getWord() ) )
211  {
212  beginning->append( entry );
213  }
214  else if( entry->getWord().endsWith( query.getWord() ) )
215  {
216  ending->append( entry );
217  }
218  else
219  {
220  anywhere->append( entry );
221  }
222  }
223 
224  delete results;
225  results = new EntryList();
226  results->appendList( exact );
227  results->appendList( beginning );
228  results->appendList( ending );
229  results->appendList( anywhere );
230  }
231 
232  return results;
233 }
234 
239 QStringList DictFileEdict::listDictDisplayOptions( QStringList x ) const
240 {
241  x += displayOptions().keys();
242  return x;
243 }
244 
248 bool DictFileEdict::loadDictionary( const QString &fileName, const QString &dictName )
249 {
250  if( m_edictFile.valid() )
251  {
252  return false; //Already loaded
253  }
254 
255  if( m_edictFile.loadFile( fileName ) )
256  {
257  m_dictionaryName = dictName;
258  m_dictionaryFile = fileName;
259 
260  m_deinflection = new Deinflection( m_dictionaryName );
261  m_deinflection->load();
262 
263  return true;
264  }
265 
266  return false;
267 }
268 
269 QMap<QString,QString> DictFileEdict::loadDisplayOptions() const
270 {
271  QMap<QString,QString> list = displayOptions();
272  list[ "Word/Kanji" ] = "Word/Kanji";
273  list[ "Reading" ] = "Reading";
274  list[ "Meaning" ] = "Meaning";
275  list[ "--Newline--" ] = "--Newline--";
276 
277  return list;
278 }
279 
280 QStringList* DictFileEdict::loadListType( KConfigSkeletonItem *item
281  , QStringList *list
282  , const QMap<QString,QString> &long2short )
283 {
284  QStringList listFromItem;
285 
286  if( item != NULL )
287  {
288  listFromItem = item->property().toStringList();
289  }
290 
291  if( ! listFromItem.isEmpty() )
292  {
293  delete list;
294 
295  list = new QStringList();
296  foreach( const QString &it, listFromItem )
297  {
298  if( long2short.contains( it ) )
299  {
300  list->append( long2short[ it ] );
301  }
302  }
303  }
304 
305  return list;
306 }
307 
308 void DictFileEdict::loadSettings()
309 {
310  this->displayFields = new QStringList( loadDisplayOptions().values() );
311 }
312 
313 void DictFileEdict::loadSettings( KConfigSkeleton *config )
314 {
315  QMap<QString,QString> long2short = displayOptions();
316  long2short[ "Word/Kanji" ] = "Word/Kanji";
317  long2short[ "Reading" ] = "Reading";
318  long2short[ "Meaning" ] = "Meaning";
319  long2short[ "--Newline--" ] = "--Newline--";
320 
321  KConfigSkeletonItem *item = config->findItem( getType() + "__displayFields" );
322  this->displayFields = loadListType( item, this->displayFields, long2short );
323 }
324 
325 inline Entry* DictFileEdict::makeEntry( const QString &entry )
326 {
327  return new EntryEdict( getName(), entry );
328 }
329 
330 DictionaryPreferenceDialog *DictFileEdict::preferencesWidget( KConfigSkeleton *config, QWidget *parent )
331 {
332  DictFileFieldSelector *dialog = new DictFileFieldSelector( config, getType(), parent );
333  dialog->addAvailable( listDictDisplayOptions( QStringList() ) );
334  return dialog;
335 }
336 
345 bool DictFileEdict::validDictionaryFile( const QString &filename )
346 {
347  QFile file( filename );
348  bool returnFlag = true;
349 
350  if( ! file.exists() || ! file.open( QIODevice::ReadOnly ) )
351  {
352  return false;
353  }
354 
355  //Now we can actually check the file
356  QTextStream fileStream( &file );
357  fileStream.setCodec( QTextCodec::codecForName( "eucJP" ) );
358  QString commentMarker( "????" ); //Note: Don't touch this! vim seems to have
359  //An odd text codec error here too :(
360  QRegExp formattedLine( "^\\S+\\s+(\\[\\S+\\]\\s+)?/.*/$" );
361  while( ! fileStream.atEnd() )
362  {
363  QString line = fileStream.readLine();
364 
365  if( line.left( 4 ) == commentMarker )
366  {
367  continue;
368  }
369  if( line.contains( formattedLine ) ) //If it matches our regex
370  {
371  continue;
372  }
373 
374  returnFlag = false;
375  break;
376  }
377 
378  file.close();
379  return returnFlag;
380 }
381 
385 //TODO: Actually write this method (validQuery)
386 bool DictFileEdict::validQuery( const DictQuery &query )
387 {
388  return true;
389 }
DictQuery::Any
Definition: dictquery.h:307
DictFileFieldSelector
Definition: dictfilefieldselector.h:36
DictionaryPreferenceDialog
This abstract base class specifies the interface for dictionary preference dialogs in user applicatio...
Definition: dictionarypreferencedialog.h:42
DictFileEdict::validQuery
bool validQuery(const DictQuery &query)
Reject queries that specify anything we don't understand.
Definition: dictfileedict.cpp:386
Deinflection::search
EntryList * search(const DictQuery &query, const QVector< QString > &preliminaryResults)
Definition: deinflection.cpp:70
DictQuery::isEmpty
bool isEmpty() const
Definition: dictquery.cpp:111
deinflection.h
Entry
The Entry class is a generic base class for each particular entry in a given dictionary.
Definition: entry.h:44
DictFileEdict::EntryEdict
friend class EntryEdict
Definition: dictfileedict.h:51
EntryEdict::matchesWordType
bool matchesWordType(const DictQuery &query) const
Definition: entryedict.cpp:333
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
EDICT
#define EDICT
Definition: kitenmacros.h:24
QWidget
DictFileEdict::wordType
static QString * wordType
Definition: dictfileedict.h:69
Deinflection::getDeinflectionLabel
QString * getDeinflectionLabel()
Definition: deinflection.cpp:60
Deinflection
Definition: deinflection.h:36
Deinflection::load
bool load()
Definition: deinflection.cpp:160
DictQuery::Adjective
Definition: dictquery.h:310
DictFileFieldSelector::addAvailable
void addAvailable(const QStringList &list)
Definition: dictfilefieldselector.cpp:76
DictFile::getName
virtual QString getName() const
Returns the name of the dictionary.
Definition: dictfile.h:132
DictFileEdict::deinflectionLabel
static QString * deinflectionLabel
Definition: dictfileedict.h:68
DictFileEdict::makeEntry
virtual Entry * makeEntry(const QString &entry)
Definition: dictfileedict.cpp:325
DictFile::m_dictionaryName
QString m_dictionaryName
Name is the 'primary key' of the list of dictionaries.
Definition: dictfile.h:152
entryedict.h
DictFileEdict::doSearch
virtual EntryList * doSearch(const DictQuery &query)
Do a search, respond with a list of entries.
Definition: dictfileedict.cpp:88
Entry::matchesQuery
virtual bool matchesQuery(const DictQuery &) const
Fairly important method, this tests if this particular entry matches a query.
Definition: entry.cpp:294
DictFile::m_dictionaryType
QString m_dictionaryType
This MUST BE SET IN THE CONSTRUCTOR.
Definition: dictfile.h:168
DictFileEdict::displayFields
static QStringList * displayFields
Definition: dictfileedict.h:81
EntryEdict
Definition: entryedict.h:60
DictFileEdict::loadSettings
void loadSettings()
Definition: dictfileedict.cpp:308
DictFile::m_dictionaryFile
QString m_dictionaryFile
This is mostly a placeholder, but your class will get asked what file it is using, so either be sure to put something here, or override getFile() and respond with something that will be sensical in a dictionary selection dialog box.
Definition: dictfile.h:160
DictFileEdict::loadListType
QStringList * loadListType(KConfigSkeletonItem *item, QStringList *list, const QMap< QString, QString > &long2short)
Definition: dictfileedict.cpp:280
DictQuery::getMeaning
QString getMeaning() const
Accessor for the non-japanese meaning field.
Definition: dictquery.cpp:502
DictFileEdict::~DictFileEdict
virtual ~DictFileEdict()
The destructor...
Definition: dictfileedict.cpp:69
DictQuery::getProperty
QString getProperty(const QString &key) const
Get a specific property by key (is the same as using operator[] const)
Definition: dictquery.cpp:440
DictQuery::getWord
QString getWord() const
Accessor for the Word/Kanji field (this is usually used for anything containing kanji).
Definition: dictquery.cpp:554
DictFile::getType
virtual QString getType() const
Returns the type of files this dictFile object deals with.
Definition: dictfile.h:136
EntryList::appendList
void appendList(const EntryList *other)
Append another EntryList onto this one.
Definition: entrylist.cpp:306
dictfilefieldselector.h
LinearEdictFile::findMatches
QVector< QString > findMatches(const QString &searchString) const
Get everything that looks remotely like a given search string.
Definition: linearedictfile.cpp:45
EntryList::EntryIterator
QListIterator< Entry * > EntryIterator
A simple overridden iterator for working with the Entries.
Definition: entrylist.h:44
DictQuery::getMatchWordType
MatchWordType getMatchWordType() const
Get which word type is currently set on the DictQuery.
Definition: dictquery.cpp:616
Deinflection::getWordType
QString * getWordType()
Definition: deinflection.cpp:65
LinearEdictFile::loadFile
bool loadFile(const QString &filename)
Load a file, generate the index if it doesn't already exist.
Definition: linearedictfile.cpp:59
DictFile::m_searchableAttributes
QMap< QString, QString > m_searchableAttributes
This allows the programming user to see a list of possible search types (probably through a drop down...
Definition: dictfile.h:178
EntryEdict::isCommon
bool isCommon() const
Definition: entryedict.cpp:102
DictQuery
A class to allow users of libkiten to properly setup a database query.
Definition: dictquery.h:89
kitenmacros.h
DictFileEdict::m_edictFile
LinearEdictFile m_edictFile
Definition: dictfileedict.h:79
DictFileEdict::preferencesWidget
virtual DictionaryPreferenceDialog * preferencesWidget(KConfigSkeleton *config, QWidget *parent=NULL)
If you want your own dialog to pick preferences for your dict, then override this.
Definition: dictfileedict.cpp:330
DictFileEdict::loadDictionary
bool loadDictionary(const QString &file, const QString &name)
Load up the dictionary.
Definition: dictfileedict.cpp:248
dictquery.h
DictFile
Abstract base class, used internally by the library for handling different types of dictionaries This...
Definition: dictfile.h:47
dictfileedict.h
DictFileEdict::displayOptions
virtual QMap< QString, QString > displayOptions() const
Definition: dictfileedict.cpp:75
DictFileEdict::listDictDisplayOptions
virtual QStringList listDictDisplayOptions(QStringList x) const
Make a list of all the extra fields in our db.
Definition: dictfileedict.cpp:239
EntryList
EntryList is a simple container for Entry objects, and is-a QList A few simple overrides allo...
Definition: entrylist.h:38
DictFileEdict::DictFileEdict
DictFileEdict()
Per instructions in the super-class, this constructor basically sets the dictionaryType member variab...
Definition: dictfileedict.cpp:56
entrylist.h
DictFileEdict::validDictionaryFile
virtual bool validDictionaryFile(const QString &filename)
Scan a potential file for the correct format, remembering to skip comment characters.
Definition: dictfileedict.cpp:345
QList
DictQuery::listPropertyKeys
const QList< QString > listPropertyKeys() const
Use this to get a list of all the property keys in the query.
Definition: dictquery.cpp:445
LinearEdictFile::valid
bool valid() const
Test if the file was properly loaded.
Definition: linearedictfile.cpp:91
DictQuery::Verb
Definition: dictquery.h:308
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