• 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
  • DictKanjidic
dictfilekanjidic.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 "dictfilekanjidic.h"
25 
26 #include "dictquery.h"
27 #include "entrykanjidic.h"
28 #include "entrylist.h"
29 #include "kitenmacros.h"
30 
31 #include <KConfigSkeleton>
32 #include <KDebug>
33 #include <KGlobal>
34 
35 #include <QFile>
36 #include <QTextCodec>
37 
38 QStringList *DictFileKanjidic::displayFields = NULL;
39 
40 DictFileKanjidic::DictFileKanjidic()
41 : DictFile( KANJIDIC )
42 {
43  m_dictionaryType = KANJIDIC;
44  m_searchableAttributes.clear();
45  m_searchableAttributes.insert( "bushu", "B" );
46  m_searchableAttributes.insert( "classical", "C" );
47  m_searchableAttributes.insert( "henshall", "E" );
48  m_searchableAttributes.insert( "frequency", "F" );
49  m_searchableAttributes.insert( "grade", "G" );
50  m_searchableAttributes.insert( "halpern", "H" );
51  m_searchableAttributes.insert( "spahn", "I" );
52  m_searchableAttributes.insert( "hadamitzky", "I" );
53  m_searchableAttributes.insert( "gakken", "K" );
54  m_searchableAttributes.insert( "heisig", "L" );
55  m_searchableAttributes.insert( "morohashi", "M" );
56  m_searchableAttributes.insert( "nelson", "N" );
57  m_searchableAttributes.insert( "oneill", "O" );
58  m_searchableAttributes.insert( "skip", "P" );
59  m_searchableAttributes.insert( "4cc", "Q" );
60  m_searchableAttributes.insert( "stroke", "S" );
61  m_searchableAttributes.insert( "strokes", "S" );
62  m_searchableAttributes.insert( "unicode", "U" );
63  m_searchableAttributes.insert( "haig", "V" );
64  m_searchableAttributes.insert( "korean", "W" );
65  m_searchableAttributes.insert( "pinyin", "Y" );
66  m_searchableAttributes.insert( "other", "D" );
67 }
68 
69 DictFileKanjidic::~DictFileKanjidic()
70 {
71 }
72 
73 QMap<QString,QString> DictFileKanjidic::displayOptions() const
74 {
75  // Enumerate the fields in our dict.... there are a rather lot of them here
76  // It will be useful for a few things to have the full list generated on it's own
77  QMap<QString,QString> list;
78  // TODO: Figure out how to internationalize these easily
79  list.insert( "Bushu Number(B)", "B");
80  list.insert( "Classical Radical Number(C)", "C");
81  list.insert( "Henshall's Index Number(E)", "E");
82  list.insert( "Frequency Ranking(F)", "F");
83  list.insert( "Grade Level(G)", "G");
84  list.insert( "Halpern's New J-E Char Dictionary(H)", "H");
85  list.insert( "Spahn & Hadamitzky Reference(I)", "I");
86  list.insert( "Gakken Kanji Dictionary Index(K)", "K");
87  list.insert( "Heisig's Index(L)", "L");
88  list.insert( "Morohashi's Daikanwajiten(M)", "M");
89  list.insert( "Nelsons Modern Reader's J-E Index(N)", "N");
90  list.insert( "O'Neill's 'Japanese Names' Index(O)", "O");
91  list.insert( "SKIP Code(P)", "P");
92  list.insert( "Four Corner codes(Q)", "Q");
93  list.insert( "Stroke Count(S)", "S");
94  list.insert( "Unicode Value(U)", "U");
95  list.insert( "Haig's New Nelson J-E Dict(V)", "V");
96  list.insert( "Korean Reading(W)", "W");
97  list.insert( "kanjidic field: X", "X");
98  list.insert( "Pinyin Reading(Y)", "Y");
99  list.insert( "Common SKIP Misclassifications(Z)", "Z");
100  list.insert( "Misc Dictionary Codes (D)", "D");
101  return list;
102 }
103 
104 EntryList* DictFileKanjidic::doSearch( const DictQuery &query )
105 {
106  if( query.isEmpty() || ! m_validKanjidic )
107  {
108  return new EntryList();
109  }
110 
111  kDebug() << "Search from:" << getName() << endl;
112  QString searchQuery = query.getWord();
113  if( searchQuery.length() == 0 )
114  {
115  searchQuery = query.getPronunciation();
116  if( searchQuery.length() == 0 )
117  {
118  searchQuery = query.getMeaning().split( ' ' ).first().toLower();
119  if( searchQuery.length() == 0 )
120  {
121  QList<QString> keys = query.listPropertyKeys();
122  if( keys.size() == 0 )
123  {
124  return new EntryList();
125  }
126  searchQuery = keys[ 0 ];
127  searchQuery = searchQuery + query.getProperty( searchQuery );
128  }
129  }
130  }
131 
132  EntryList *results = new EntryList();
133  foreach( const QString &line, m_kanjidic )
134  {
135  if( line.contains( searchQuery ) )
136  {
137  Entry *entry = makeEntry( line );
138  if( entry->matchesQuery( query ) )
139  {
140  results->append( entry );
141  }
142  else delete entry;
143  }
144  }
145 
146  return results;
147 }
148 
149 QStringList DictFileKanjidic::dumpDictionary()
150 {
151  if( ! m_validKanjidic )
152  {
153  return QStringList();
154  }
155 
156  return m_kanjidic;
157 }
158 
159 QStringList DictFileKanjidic::listDictDisplayOptions( QStringList list ) const
160 {
161  list += displayOptions().keys();
162  return list;
163 }
164 
165 bool DictFileKanjidic::loadDictionary( const QString &file, const QString &name )
166 {
167  if( ! m_kanjidic.isEmpty() )
168  {
169  return true;
170  }
171 
172  QFile dictionary( file );
173  if( ! dictionary.open( QIODevice::ReadOnly | QIODevice::Text ) )
174  {
175  return false;
176  }
177 
178  kDebug() << "Loading kanjidic from:" << file << endl;
179 
180  QTextStream fileStream( &dictionary );
181  fileStream.setCodec( QTextCodec::codecForName( "eucJP" ) );
182 
183  QString currentLine;
184  while( ! fileStream.atEnd() )
185  {
186  currentLine = fileStream.readLine();
187  if( currentLine[ 0 ] != '#' )
188  {
189  m_kanjidic << currentLine;
190  }
191  }
192 
193  dictionary.close();
194 
195  if( ! validDictionaryFile( file ) )
196  {
197  return false;
198  }
199 
200  m_dictionaryName = name;
201  m_dictionaryFile = file;
202 
203  return true;
204 }
205 
206 QMap<QString,QString> DictFileKanjidic::loadDisplayOptions() const
207 {
208  QMap<QString,QString> list = displayOptions();
209  list[ "Word/Kanji" ] = "Word/Kanji";
210  list[ "Reading" ] = "Reading";
211  list[ "Meaning" ] = "Meaning";
212  list[ "--Newline--" ] = "--Newline--";
213 
214  return list;
215 }
216 
217 QStringList* DictFileKanjidic::loadListType( KConfigSkeletonItem *item
218  , QStringList *list
219  , const QMap<QString,QString> &long2short )
220 {
221  QStringList listFromItem;
222 
223  if( item != NULL )
224  {
225  listFromItem = item->property().toStringList();
226  }
227 
228  if( ! listFromItem.isEmpty() )
229  {
230  delete list;
231 
232  list = new QStringList();
233  foreach( const QString &it, listFromItem )
234  {
235  if( long2short.contains( it ) )
236  {
237  list->append( long2short[ it ] );
238  }
239  }
240  }
241 
242  return list;
243 }
244 
245 void DictFileKanjidic::loadSettings()
246 {
247  this->displayFields = new QStringList( loadDisplayOptions().values() );
248 }
249 
250 void DictFileKanjidic::loadSettings( KConfigSkeleton *config )
251 {
252  KConfigSkeletonItem *item = config->findItem( getType() + "__displayFields" );
253  this->displayFields = loadListType( item, this->displayFields, loadDisplayOptions() );
254 }
255 
256 inline Entry* DictFileKanjidic::makeEntry( const QString &entry )
257 {
258  return new EntryKanjidic( getName(), entry );
259 }
260 
266 bool DictFileKanjidic::validDictionaryFile( const QString &filename )
267 {
268  QFile file( filename );
269  if( ! file.exists() || ! file.open( QIODevice::ReadOnly ) )
270  {
271  return false;
272  }
273 
274  QTextStream fileStream( &file );
275  fileStream.setCodec( QTextCodec::codecForName( "eucJP" ) );
276 
277  QRegExp format( "^\\S\\s+(\\S+\\s+)+(\\{(\\S+\\s?)+\\})+" );
278  m_validKanjidic = true;
279  while( ! fileStream.atEnd() )
280  {
281  QString currentLine = fileStream.readLine();
282 
283  if( currentLine[ 0 ] == '#' )
284  {
285  continue;
286  }
287  else if( currentLine.contains( format ) )
288  {
289  continue;
290  }
291 
292  m_validKanjidic = false;
293  break;
294  }
295 
296  file.close();
297  return m_validKanjidic;
298 }
299 
303 bool DictFileKanjidic::validQuery( const DictQuery &query )
304 {
305  //Multi kanji searches don't apply to this file
306  if( query.getWord().length() > 1 )
307  {
308  return false;
309  }
310 
311  //Now check if we have any properties specified that we don't understand
312  QStringList propertiesWeHandle = m_searchableAttributes.values() + m_searchableAttributes.keys();
313  propertiesWeHandle += "common"; // We map this to be (has a G value)
314 
315  const QStringList properties = query.listPropertyKeys();
316  for( QStringList::const_iterator it = properties.constBegin(); it != properties.constEnd(); ++it )
317  {
318  if( ! propertiesWeHandle.contains( *it ) )
319  {
320  return false;
321  }
322  }
323 
324  return true;
325 }
QTextStream::setCodec
void setCodec(QTextCodec *codec)
QMap::contains
bool contains(const Key &key) const
QMap::values
QList< T > values() const
QTextStream::readLine
QString readLine(qint64 maxlen)
DictQuery::isEmpty
bool isEmpty() const
Definition: dictquery.cpp:111
Entry
The Entry class is a generic base class for each particular entry in a given dictionary.
Definition: entry.h:44
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
dictfilekanjidic.h
QMap< QString, QString >
DictFileKanjidic::dumpDictionary
QStringList dumpDictionary()
Definition: dictfilekanjidic.cpp:149
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
DictFileKanjidic::displayOptions
QMap< QString, QString > displayOptions() const
Definition: dictfilekanjidic.cpp:73
DictFileKanjidic::listDictDisplayOptions
virtual QStringList listDictDisplayOptions(QStringList list) const
Return a list of the fields that can be displayed, note the following should probably always be retur...
Definition: dictfilekanjidic.cpp:159
DictFileKanjidic::validDictionaryFile
bool validDictionaryFile(const QString &filename)
Scan a potential file for the correct format, remembering to skip comment characters.
Definition: dictfilekanjidic.cpp:266
QFile::exists
bool exists() const
QMap::clear
void clear()
DictFile::getName
virtual QString getName() const
Returns the name of the dictionary.
Definition: dictfile.h:132
QList::const_iterator
DictFileKanjidic::loadSettings
void loadSettings()
Definition: dictfilekanjidic.cpp:245
QFile
entrykanjidic.h
QTextStream
DictFile::m_dictionaryName
QString m_dictionaryName
Name is the 'primary key' of the list of dictionaries.
Definition: dictfile.h:152
QList::size
int size() const
DictFileKanjidic::loadListType
QStringList * loadListType(KConfigSkeletonItem *item, QStringList *list, const QMap< QString, QString > &long2short)
Definition: dictfilekanjidic.cpp:217
QMap::keys
QList< Key > keys() const
DictFileKanjidic::doSearch
virtual EntryList * doSearch(const DictQuery &query)
This actually conducts the search on the given query.
Definition: dictfilekanjidic.cpp:104
QRegExp
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
KANJIDIC
#define KANJIDIC
Definition: kitenmacros.h:25
DictFileKanjidic::displayFields
static QStringList * displayFields
Definition: dictfilekanjidic.h:64
QList::append
void append(const T &value)
QTextStream::atEnd
bool atEnd() const
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
DictQuery::getMeaning
QString getMeaning() const
Accessor for the non-japanese meaning field.
Definition: dictquery.cpp:502
QList::isEmpty
bool isEmpty() const
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
QList::first
T & first()
QString
QList< QString >
DictFileKanjidic::makeEntry
virtual Entry * makeEntry(const QString &entry)
Definition: dictfilekanjidic.cpp:256
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QFile::close
virtual void close()
DictFileKanjidic::~DictFileKanjidic
virtual ~DictFileKanjidic()
Definition: dictfilekanjidic.cpp:69
DictFileKanjidic::validQuery
bool validQuery(const DictQuery &query)
Reject queries that specify anything we don't understand.
Definition: dictfilekanjidic.cpp:303
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
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
kitenmacros.h
QString::length
int length() const
DictFileKanjidic::DictFileKanjidic
DictFileKanjidic()
Definition: dictfilekanjidic.cpp:40
dictquery.h
QMap::insert
iterator insert(const Key &key, const T &value)
DictFile
Abstract base class, used internally by the library for handling different types of dictionaries This...
Definition: dictfile.h:47
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
DictFileKanjidic::EntryKanjidic
friend class EntryKanjidic
Definition: dictfilekanjidic.h:41
EntryList
EntryList is a simple container for Entry objects, and is-a QList A few simple overrides allo...
Definition: entrylist.h:38
entrylist.h
DictFileKanjidic::loadDictionary
bool loadDictionary(const QString &file, const QString &name)
Load a dictionary as at system startup.
Definition: dictfilekanjidic.cpp:165
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