• 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
entry.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 "entry.h"
25 #include "dictquery.h"
26 
27 #include <KDebug>
28 #include <KLocale>
29 #include <KMessageBox>
30 #include <KStandardDirs>
31 
32 #include <QFileInfo>
33 #include <QRegExp>
34 #include <QTextCodec>
35 
36 #include <iostream>
37 #include <cassert>
38 #include <sys/mman.h>
39 #include <stdio.h>
40 
47 Entry::Entry()
48 {
49  init();
50 }
51 
52 Entry::Entry( const QString &sourceDictionary )
53 : sourceDict( sourceDictionary )
54 {
55  init();
56 }
57 
58 Entry::Entry( const QString &sourceDictionary, const QString &word,
59  const QStringList &reading, const QStringList &meanings )
60 : Word( word )
61 , Meanings( meanings )
62 , Readings( reading )
63 , sourceDict( sourceDictionary )
64 {
65  init();
66 }
67 
68 Entry::Entry( const Entry &src )
69 : Word( src.Word )
70 , Meanings( src.Meanings )
71 , Readings( src.Readings )
72 , ExtendedInfo( src.ExtendedInfo )
73 , sourceDict( src.sourceDict )
74 {
75  outputListDelimiter = src.outputListDelimiter;
76 }
77 
78 Entry::~Entry()
79 {
80 // kdDebug() << "nuking : " << Word << endl;
81 }
82 
83 bool Entry::extendedItemCheck( const QString& key, const QString &value ) const
84 {
85  return getExtendedInfoItem( key ) == value;
86 }
87 
91 const QString& Entry::getDictName() const
92 {
93  return sourceDict;
94 }
95 
100 QString Entry::getWord() const
101 {
102  return Word;
103 }
104 
108 QString Entry::getMeanings() const
109 {
110  return Meanings.join(outputListDelimiter);
111 }
112 
116 QStringList Entry::getMeaningsList() const
117 {
118  return Meanings;
119 }
120 
124 QString Entry::getReadings() const
125 {
126  return Readings.join( outputListDelimiter );
127 }
128 
132 QStringList Entry::getReadingsList() const
133 {
134  return Readings;
135 }
136 
140 const QHash<QString,QString> &Entry::getExtendedInfo() const
141 {
142  return ExtendedInfo;
143 }
144 
150 QString Entry::getExtendedInfoItem( const QString &x ) const
151 {
152  return ExtendedInfo[ x ];
153 }
154 
158 inline QString Entry::HTMLMeanings() const
159 {
160  return QString( "<span class=\"Meanings\">%1</span>" )
161  .arg( Meanings.join( outputListDelimiter ) );
162 }
163 
164 /* Prepares Readings for output as HTML */
165 inline QString Entry::HTMLReadings() const
166 {
167  QStringList list;
168  foreach( const QString &it, Readings )
169  {
170  list += makeLink( it );
171  }
172 
173  return QString( "<span class=\"Readings\">%1</span>" )
174  .arg( list.join( outputListDelimiter ) );
175 }
176 
180 inline QString Entry::HTMLWord() const
181 {
182  return QString( "<span class=\"Word\">%1</span>" ).arg( Word );
183 }
184 
185 void Entry::init()
186 {
187  outputListDelimiter = i18n( "; " );
188 }
189 
193 bool Entry::isKanji( const QChar &character ) const
194 {
195  ushort value = character.unicode();
196  if( value < 255 )
197  {
198  return false;
199  }
200  if( 0x3040 <= value && value <= 0x30FF )
201  {
202  return false; //Kana
203  }
204 
205  return true; //Note our folly here... we assuming any non-ascii/kana is kanji
206 }
207 
211 bool Entry::listMatch( const QStringList &list, const QStringList &test, DictQuery::MatchType type ) const
212 {
213  if( type == DictQuery::Exact )
214  {
215  foreach( const QString &it, test )
216  {
217  if( ! list.contains( it ) )
218  {
219  return false;
220  }
221  }
222  }
223  else if( type == DictQuery::Beginning )
224  {
225  foreach( const QString &it, test )
226  {
227  bool found = false;
228  foreach( const QString &it2, list )
229  {
230  if( it2.startsWith( it ) )
231  {
232  found = true;
233  break;
234  }
235  }
236  if( ! found )
237  {
238  return false;
239  }
240  }
241  }
242  else if( type == DictQuery::Ending )
243  {
244  foreach( const QString &it, test )
245  {
246  bool found = false;
247  foreach( const QString &it2, list )
248  {
249  if( it2.endsWith( it ) )
250  {
251  found = true;
252  break;
253  }
254  }
255  if( ! found )
256  {
257  return false;
258  }
259  }
260  }
261  else
262  {
263  foreach( const QString &it, test )
264  {
265  bool found = false;
266  foreach( const QString &it2, list )
267  {
268  if( it2.contains( it ) )
269  {
270  found = true;
271  break;
272  }
273  }
274  if( ! found )
275  {
276  return false;
277  }
278  }
279  }
280 
281  return true;
282 }
283 
289 inline QString Entry::makeLink( const QString &entryString ) const
290 {
291  return QString( "<a href=\"%1\">%1</a>" ).arg( entryString );
292 }
293 
294 bool Entry::matchesQuery( const DictQuery &query ) const
295 {
296  if( ! query.getWord().isEmpty() )
297  {
298  if( query.getMatchType() == DictQuery::Exact
299  && this->getWord() != query.getWord() )
300  {
301  return false;
302  }
303  if( query.getMatchType() == DictQuery::Beginning
304  && ! this->getWord().startsWith( query.getWord() ) )
305  {
306  return false;
307  }
308  if( query.getMatchType() == DictQuery::Ending
309  && ! this->getWord().endsWith( query.getWord() ) )
310  {
311  return false;
312  }
313  if( query.getMatchType() == DictQuery::Anywhere
314  && ! this->getWord().contains( query.getWord() ) )
315  {
316  return false;
317  }
318  }
319 
320  if( ! query.getPronunciation().isEmpty() && ! getReadings().isEmpty() )
321  {
322  if( ! listMatch( Readings, query.getPronunciation().split( DictQuery::mainDelimiter ),
323  query.getMatchType() ) )
324  {
325  return false;
326  }
327  }
328 
329  if( ! query.getPronunciation().isEmpty() && getReadings().isEmpty() && ! getWord().isEmpty() )
330  {
331  switch ( query.getMatchType() )
332  {
333  case DictQuery::Exact:
334  if ( getWord() != query.getPronunciation() )
335  {
336  return false;
337  }
338  break;
339  case DictQuery::Beginning:
340  if ( ! getWord().startsWith( query.getPronunciation() ) )
341  {
342  return false;
343  }
344  break;
345  case DictQuery::Ending:
346  if ( ! getWord().endsWith( query.getPronunciation() ) )
347  {
348  return false;
349  }
350  case DictQuery::Anywhere:
351  if ( ! getWord().contains( query.getPronunciation() ) )
352  {
353  return false;
354  }
355  break;
356  }
357  }
358 
359  if( ! query.getMeaning().isEmpty() )
360  {
361  if( ! listMatch( Meanings.join( " " ).toLower().split( ' ' )
362  , query.getMeaning().toLower().split( DictQuery::mainDelimiter )
363  , query.getMatchType() ) )
364  {
365  return false;
366  }
367  }
368 
369  QList<QString> propList = query.listPropertyKeys();
370  foreach( const QString &key, propList )
371  {
372  if( ! extendedItemCheck( key, query.getProperty( key ) ) )
373  {
374  return false;
375  }
376  }
377 
378  return true;
379 }
380 
384 QString Entry::toHTML() const
385 {
386  return QString( "<div class=\"Entry\">%1%2%3</div>" )
387  .arg( HTMLWord() )
388  .arg( HTMLReadings() )
389  .arg( HTMLMeanings() );
390 }
391 
392 inline QString Entry::toKVTML() const
393 {
394  /*
395  <e m="1" s="1">
396  <o width="414" l="en" q="t">(eh,) excuse me</o>
397  <t width="417" l="jp" q="o">(あのう、) すみません </t>
398  </e>
399  */
400  //TODO: en should not necessarily be the language here.
401  return QString( "<e>\n<o l=\"en\">%1</o>\n"
402  "<t l=\"jp-kanji\">%2</t>\n"
403  "<t l=\"jp-kana\">%3</t></e>\n\n" ).arg( getMeanings() )
404  .arg( getWord() )
405  .arg( getReadings() );
406 }
407 
413 QString Entry::toString() const
414 {
415  return QString( "%1 (%2) %3" ).arg( Word )
416  .arg( getReadings() )
417  .arg( getMeanings() );
418 }
419 
425 bool Entry::sort( const Entry &that, const QStringList &dictOrder, const QStringList &fields ) const
426 {
427  if( this->sourceDict != that.sourceDict )
428  {
429  foreach( const QString &dict, dictOrder )
430  {
431  if( dict == that.sourceDict )
432  {
433  return false;
434  }
435  if( dict == this->sourceDict )
436  {
437  return true;
438  }
439  }
440  }
441  else
442  {
443  foreach( const QString &field, fields )
444  {
445  if( field == QString( "Word/Kanji" ) )
446  {
447  return this->getWord() < that.getWord();
448  }
449  else if( field == QString( "Meaning" ) )
450  {
451  return listMatch( that.getMeaningsList(), this->getMeaningsList(), DictQuery::Exact )
452  && ( that.getMeaningsList().count() != this->getMeaningsList().count() );
453  }
454  else if( field == QString( "Reading" ) )
455  {
456  return listMatch( that.getReadingsList(), this->getReadingsList(), DictQuery::Exact )
457  && ( that.getReadingsList().count() != this->getReadingsList().count() );
458  }
459  else
460  {
461  const QString thisOne = this->getExtendedInfoItem( field );
462  const QString thatOne = that.getExtendedInfoItem( field );
463  //Only sort by this field if the values differ, otherwise move to the next field
464  if( thisOne != thatOne )
465  {
466  //If the second item does not have this field, sort this one first
467  if( thatOne.isEmpty() )
468  {
469  return true;
470  }
471  //If we don't have this field, sort "this" to second
472  if( thisOne.isEmpty() )
473  {
474  return false;
475  }
476  //Otherwise, send it to a virtual function (to allow dictionaries to override sorting)
477  return this->sortByField( that, field );
478  }
479  }
480  }
481  }
482  return false; //If we reach here, they match as much as possible
483 }
484 
485 bool Entry::sortByField( const Entry &that, const QString &field ) const
486 {
487  return this->getExtendedInfoItem( field ) < that.getExtendedInfoItem( field );
488 }
Entry::HTMLWord
virtual QString HTMLWord() const
Return and HTML version of a word.
Definition: entry.cpp:180
Entry::extendedItemCheck
virtual bool extendedItemCheck(const QString &key, const QString &value) const
Simple accessor.
Definition: entry.cpp:83
Entry::getExtendedInfo
const QHash< QString, QString > & getExtendedInfo() const
Simple accessor.
Definition: entry.cpp:140
Entry
The Entry class is a generic base class for each particular entry in a given dictionary.
Definition: entry.h:44
QChar
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
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
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
DictQuery::MatchType
MatchType
This enum is used to define the type of matching this query is supposed to do.
Definition: dictquery.h:285
QStringList::join
QString join(const QString &separator) const
Entry::HTMLMeanings
virtual QString HTMLMeanings() const
Return and HTML version of a meaning list.
Definition: entry.cpp:158
Entry::HTMLReadings
virtual QString HTMLReadings() const
Return and HTML version of a reading list.
Definition: entry.cpp:165
Entry::Readings
QStringList Readings
The Readings (usually kana) that match this entry.
Definition: entry.h:203
Entry::makeLink
virtual QString makeLink(const QString &entryString) const
Handy function for generating a link from a given QString.
Definition: entry.cpp:289
Entry::matchesQuery
virtual bool matchesQuery(const DictQuery &) const
Fairly important method, this tests if this particular entry matches a query.
Definition: entry.cpp:294
QList::count
int count(const T &value) const
Entry::getExtendedInfoItem
QString getExtendedInfoItem(const QString &x) const
Simple accessor.
Definition: entry.cpp:150
Entry::toKVTML
virtual QString toKVTML() const
KVTML format for exporting.
Definition: entry.cpp:392
QHash< QString, QString >
Entry::~Entry
virtual ~Entry()
Generic Destructor.
Definition: entry.cpp:78
entry.h
DictQuery::getMeaning
QString getMeaning() const
Accessor for the non-japanese meaning field.
Definition: dictquery.cpp:502
QString::isEmpty
bool isEmpty() const
Entry::toHTML
virtual QString toHTML() const
An entry should be able to generate a representation of itself in (valid) HTML.
Definition: entry.cpp:384
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
DictQuery::Ending
Definition: dictquery.h:289
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
Entry::Word
QString Word
The Word (usually containing kanji) that matches this entry.
Definition: entry.h:193
Entry::listMatch
bool listMatch(const QStringList &list, const QStringList &test, DictQuery::MatchType type) const
Handy Utility functions for matching to lists and identifying char types.
Definition: entry.cpp:211
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< QString >
QChar::unicode
ushort unicode() const
QStringList
Entry::getMeanings
QString getMeanings() const
Get a QString containing all of the meanings known, connected by the outputListDelimiter.
Definition: entry.cpp:108
QString::toLower
QString toLower() const
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
Entry::getReadingsList
QStringList getReadingsList() const
Simple accessor.
Definition: entry.cpp:132
Entry::isKanji
bool isKanji(const QChar &character) const
Handy Utility functions for matching to lists and identifying char types.
Definition: entry.cpp:193
Entry::getReadings
QString getReadings() const
Simple accessor.
Definition: entry.cpp:124
Entry::sortByField
virtual bool sortByField(const Entry &that, const QString &field) const
Overrideable sorting mechanism for sorting by individual fields.
Definition: entry.cpp:485
DictQuery::Anywhere
Definition: dictquery.h:290
Entry::Meanings
QStringList Meanings
The Meanings that match this entry.
Definition: entry.h:198
Entry::sort
virtual bool sort(const Entry &that, const QStringList &dictionaryList, const QStringList &fieldList) const
An overrideable sorting function, similer to operator< in most contexts The default version will sort...
Definition: entry.cpp:425
Entry::getMeaningsList
QStringList getMeaningsList() const
Simple accessor.
Definition: entry.cpp:116
DictQuery::mainDelimiter
static const QString mainDelimiter
This is the main delimiter that the DictQuery uses when parsing strings.
Definition: dictquery.h:96
DictQuery
A class to allow users of libkiten to properly setup a database query.
Definition: dictquery.h:89
DictQuery::Beginning
Definition: dictquery.h:288
DictQuery::getMatchType
MatchType getMatchType() const
Get which match type is currently set on the DictQuery.
Definition: dictquery.cpp:606
Entry::init
void init()
This is used by the constructors to set some default values.
Definition: entry.cpp:185
dictquery.h
Entry::toString
virtual QString toString() const
This will return a pure text interpretation of the Entry.
Definition: entry.cpp:413
Entry::sourceDict
QString sourceDict
The dictionary that this entry originated at.
Definition: entry.h:212
Entry::getDictName
const QString & getDictName() const
Get the dictionary name that generated this Entry.
Definition: entry.cpp:91
Entry::ExtendedInfo
QHash< QString, QString > ExtendedInfo
A hash of extended information.
Definition: entry.h:207
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Entry::outputListDelimiter
QString outputListDelimiter
The delimiter for lists...
Definition: entry.h:216
DictQuery::Exact
Definition: dictquery.h:287
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
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