File: entrykanjidic.cpp

package info (click to toggle)
kiten 4%3A22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 34,072 kB
  • sloc: cpp: 7,384; ansic: 248; xml: 168; makefile: 7; sh: 2
file content (478 lines) | stat: -rw-r--r-- 14,964 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
    This file is part of Kiten, a KDE Japanese Reference Tool
    SPDX-FileCopyrightText: 2001 Jason Katz-Brown <jason@katzbrown.com>
    SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
    SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>
    SPDX-FileCopyrightText: 2011 Daniel E. Moctezuma <democtezuma@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "entrykanjidic.h"

#include "dictfilekanjidic.h"
#include "kitenmacros.h"

#include <KLocalizedString>
#include <QDebug>

#define QSTRINGLISTCHECK(x) (x==NULL?QStringList():*x)

EntryKanjidic::EntryKanjidic( const EntryKanjidic &dict )
: Entry( dict )
{
}

EntryKanjidic::EntryKanjidic( const QString &dict )
: Entry( dict )
{
}

EntryKanjidic::EntryKanjidic( const QString &dict, const QString &entry )
: Entry( dict )
{
  loadEntry( entry );
}

QString EntryKanjidic::addReadings( const QStringList &list ) const
{
  QString readings;
  foreach( const QString &reading, list )
  {
    readings.append( makeReadingLink( reading ) + outputListDelimiter );
  }

  return readings;
}

Entry* EntryKanjidic::clone() const
{
  return new EntryKanjidic( *this );
}

/**
 * This reproduces a kanjidic-formatted line from the Entry.
 *  Look at the above parser to see how the format works.
 */
QString EntryKanjidic::dumpEntry() const
{
  /* Loop over the ExtendedInfo to add it to the line we produce */
  QString dumpExtendedInfo;
  QHash<QString,QString>::const_iterator it;
  for( it = ExtendedInfo.constBegin(); it != ExtendedInfo.constEnd(); ++it )
  {
    dumpExtendedInfo += ' ' + it.key() + it.value();
  }

  return QStringLiteral( "%1 %2%3" ).arg( Word )
                             .arg( Readings.join( QLatin1Char(' ') ) )
                             .arg( dumpExtendedInfo );
}

bool EntryKanjidic::extendedItemCheck( const QString &key, const QString &value ) const
{
  if( key == QLatin1String("common") )
  {
    return ! getExtendedInfoItem( QStringLiteral("G") ).isEmpty();
  }

  return Entry::extendedItemCheck( key, value );
}

QString EntryKanjidic::getAsRadicalReadings() const
{
  return AsRadicalReadings.join( outputListDelimiter );
}

QStringList EntryKanjidic::getAsRadicalReadingsList() const
{
  return AsRadicalReadings;
}

QString EntryKanjidic::getDictionaryType() const
{
  return KANJIDIC;
}

QString EntryKanjidic::getInNamesReadings() const
{
  return InNamesReadings.join( outputListDelimiter );
}

QStringList EntryKanjidic::getInNamesReadingsList() const
{
  return InNamesReadings;
}

QString EntryKanjidic::getKanjiGrade() const
{
  return getExtendedInfoItem( QStringLiteral("G") );
}

QString EntryKanjidic::getKunyomiReadings() const
{
  return KunyomiReadings.join( outputListDelimiter );
}

QStringList EntryKanjidic::getKunyomiReadingsList() const
{
  return KunyomiReadings;
}

QString EntryKanjidic::getOnyomiReadings() const
{
  return OnyomiReadings.join( outputListDelimiter );
}

QStringList EntryKanjidic::getOnyomiReadingsList() const
{
  return OnyomiReadings;
}

QString EntryKanjidic::getStrokesCount() const
{
  return getExtendedInfoItem( QStringLiteral("S") );
}

QString EntryKanjidic::HTMLExtendedInfo( const QString &field ) const
{
  //qDebug() << field;
  return QStringLiteral( "<span class=\"ExtendedInfo\">%1: %2</span>" )
             .arg( field )
             .arg( ExtendedInfo[ field ] );
}

/**
 * Prepares Readings for output as HTML
 */
QString EntryKanjidic::HTMLReadings() const
{
  QString htmlReadings;
  htmlReadings += addReadings( originalReadings );

  if( InNamesReadings.count() > 0 )
  {
    htmlReadings += i18n( "In names: " );
    htmlReadings += addReadings( InNamesReadings );
  }

  if( AsRadicalReadings.count() > 0 )
  {
    htmlReadings += i18n( "As radical: " );
    htmlReadings += addReadings( AsRadicalReadings );
  }

  // get rid of last ,
  htmlReadings.truncate( htmlReadings.length() - outputListDelimiter.length() );
  return QStringLiteral( "<span class=\"Readings\">%1</span>" ).arg( htmlReadings );
}

QString EntryKanjidic::HTMLWord() const
{
  return QStringLiteral( "<span class=\"Word\">%1</span>" ).arg( makeLink( Word ) );
}

/**
 * Fill the fields of our Entry object appropriate to the given
 * entry line from Kanjidic.
 */
/* TODO: Error checking */
bool EntryKanjidic::loadEntry( const QString &entryLine )
{
  unsigned int length = entryLine.length();

  /* The loop would be a bit faster if we first grabbed the kanji (2 bytes) and then the
     space that follows, etc. for the fixed-space portion of the entries let's try that.
     First the first 2 bytes are guaranteed to be our kanji.  The 3rd byte is a space.
     The 4th through 7th are an ascii representation of the JIS code.  One more space
     Currently, kana are not detected so readings are anything that is not otherwise
     in the 8th position. */
  Word = entryLine.left( 1 );
  //	QString strjis = raw.mid( 2, 4 );

  /* variables for the loop */
  QChar ichar;
  QString curString;

  /* we would need to do these exact things ... many times so here now. */
  #define INCI if(i < length) \
  { \
    i++; \
    ichar = entryLine.at(i); \
  }
  #define LOADSTRING(stringToLoad) while(entryLine.at(i) != ' ') \
  { \
    stringToLoad += entryLine.at(i); \
    if(i < length) i++; \
    else break; \
  }

  //	qDebug() << "LOADSTRING: '" << stringToLoad << "'";

  /* We can start looping at 8 because we have guarantees about the initial
     data.  This loop is used because the kanjidic format allows the data
     to be in any order until the end of the line.  The format was designed
     such that the data can be identified by the first byte. */
  for ( unsigned int i = 7; i < length - 1; i++ )
  {
      ichar = entryLine.at( i );

      curString = QLatin1String("");
      switch( ichar.unicode() )
      {
        case ' ':
          /* as far as I can tell, there is no real rule forcing only 1 space so
                  there's not really any significance to them.  This block is not
                  reached in kanjidic itself. */
          break;
        case 'B':
          /* the radical, or busyu, number */
        case 'C':
          /* the classical radical number, usually doesn't differ from busyu number */
        case 'E':
          /* Henshell's "A Guide To Remembering Japanese Characters" index number */
        case 'F':
          /* frequency ranking */
        case 'G':
          /* grade level Jouyou 1 - 6 or 8 for common use or 9 for Jinmeiyou */
        case 'H':
          /* number from Halpern's New Japanese-English Character Dictionary */
        case 'K':
          /* Gakken Kanji Dictionary index */
        case 'L':
          /* Heisig's "Remembering The Kanji" index */
        case 'N':
          /* number from Nelson's Modern Reader's Japanese-English Character Dictionary */
        case 'O':
          /* O'Neill's "Japanese Names" index number */
        case 'P':
          /* SKIP code ... #-#-# format */
        case 'Q':
          /* Four Corner codes, it seems, can be multiple though I'm tempted just to take the last one. */
        case 'U':
          /* unicode which we are ignoring as it is found in another way */
        case 'V':
          /* number from Haig's New Nelson Japanese-English Character Dictionary */
        case 'W':
          /* korean reading */
        case 'X':
          /* I don't entirely understand this field. */
        case 'Y':
          /* Pinyin reading */
        case 'Z':
          /* SKIP misclassifications */

          /* All of the above are of the format <Char><Data> where <Char> is
                  exactly 1 character. */
          i++;
          LOADSTRING( curString );
          ExtendedInfo.insert( QString( ichar ), curString );
          break;
        case 'I':
          /* index codes for Spahn & Hadamitzky reference books we need the next
                  char to know what to do with it. */
          INCI
          if( ichar == 'N' )
          {
            /* a Kanji & Kana book number */
            LOADSTRING( curString )
          }
          else
          {
            /* The Kanji Dictionary number, we need the current ichar. */
            LOADSTRING( curString )
          }
          ExtendedInfo.insert( 'I' + QString( ichar ), curString );
          break;
        case 'M':
          /* index and page numbers for Morohashi's Daikanwajiten 2 fields possible */
          INCI
          if( ichar == 'N' )
          {
            LOADSTRING( curString )
            /* index number */
          }
          else if( ichar == 'P' )
          {
            LOADSTRING( curString )
            /* page number in volume.page format */
          }
          ExtendedInfo.insert( 'M' + QString( ichar ), curString );
          break;
        case 'S':
          /* stroke count: may be multiple.  In that case, first is actual, others common
                  miscounts */
          i++;
          if( ! ExtendedInfo.contains(QStringLiteral("S") ) )
          {
            LOADSTRING( curString )
            ExtendedInfo.insert( QString( ichar ), curString );
          }
          else
          {
            LOADSTRING( curString )
            ExtendedInfo.insert( '_' + QString( ichar ), curString );
          }
          break;
        case 'D':
          /* dictionary codes */
          INCI
          LOADSTRING( curString )
          ExtendedInfo.insert( 'D' + QString( ichar ), curString );
          break;
        case '{':
          /* This should be starting with the first '{' character of a meaning section.
                  Let us get take it to the last. */
          INCI
          while( ichar != '}' )
          {
            curString += ichar;
            /* sanity */
            if( i < length )
            {
              i++;
            }
            else
            {
              break;
            }
            ichar = entryLine.at( i );
          }
          INCI
//           qDebug() << "Meaning's curString: '" << curString << "'";
          Meanings.append( curString );
          break;
        case 'T': /* a reading that is used in names for T1, radical names for T2 */
        {
          i++;
          LOADSTRING( curString )
          // Get the type number (1 for T1, 2 for T2).
          int type = curString.toInt();
          bool finished = false;
          while( ! finished )
          {
            // Skip all whitespaces.
            INCI
            while( ichar == ' ' )
            {
              INCI
            }
            // Check if the current character is Kana.
            if( 0x3040 <= ichar.unicode() && ichar.unicode() <= 0x30FF )
            {
              // Reset our variable and load it with
              // all available kana until we find a whitespace.
              curString = QLatin1String("");
              LOADSTRING( curString );
              switch( type )
              {
                case 1: // Special reading used in names.
                  InNamesReadings.append( curString );
                  break;
                case 2: // Reading as radical.
                  AsRadicalReadings.append( curString );
                  break;
              }
            }
            else
            {
              // There are not more kana characters,
              // so we finish this loop for now.
              finished = true;
            }
          }
          // Now 'i' points to a '{' character. We decrease its value
          // so in the next loop we can reach the "case '{'" section.
          i--;
        }
        break;
        case '-':
          /* a reading that is only in postposition */
          /* any of those 2 signals a reading is to ensue. */
          LOADSTRING( curString )
          originalReadings.append( curString );

          // If it is Hiragana (Kunyomi)
          if( 0x3040 <= ichar.unicode() && ichar.unicode() <= 0x309F )
          {
            KunyomiReadings.append( curString );
          }
          // If it is Katakana (Onyomi)
          else if( 0x30A0 <= ichar.unicode() && ichar.unicode() <= 0x30FF )
          {
            OnyomiReadings.append( curString );
          }

          curString = curString.remove( '-' ).remove( '.' );
          Readings.append( curString );
          break;
        default:
          /* either a character we don't address or a problem...we should ignore it */
// 	  qDebug() << "hit default in kanji parser.  Unicode: '" << ichar.unicode() << "'";

          /* This should detect unicode kana */
          // Hiragana 0x3040 - 0x309F, Katakana: 0x30A0 - 0x30FF
          if( 0x3040 <= ichar.unicode() && ichar.unicode() <= 0x30FF )
          {
            LOADSTRING( curString )
            originalReadings.append( curString );

            // If it is Hiragana (Kunyomi)
            if( 0x3040 <=ichar.unicode() && ichar.unicode() <= 0x309F )
            {
              KunyomiReadings.append( curString );
            }
            // If it is Katakana (Onyomi)
            else if( 0x30A0 <= ichar.unicode() && ichar.unicode() <= 0x30FF )
            {
              OnyomiReadings.append( curString );
            }

            curString = curString.remove( '-' ).remove( '.' );
            Readings.append( curString );
            break;
          }
          /* if it's not a kana reading ... it is something unhandled ...
             possibly a new field in kanjidic.  Let's treat it as the
             oh-so-common <char><data> type of entry.  It could be hotly
             debated what we should actually do about these. */
          i++;
          LOADSTRING( curString );
          ExtendedInfo.insert( QString( ichar ), curString );

          break;
      }
  }
//   qDebug() << "Parsed: '"<<Word<<"' ("<<Readings.join("^")<<") \""<<
//   Meanings.join("|")<<"\ and " <<ExtendedInfo.keys() << " from :"<<entryLine<<endl;

  return true;
}

QString EntryKanjidic::makeReadingLink( const QString &inReading ) const
{
  QString reading = inReading;
  return QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( reading.remove( '.' ).remove( '-' ) )
                                           .arg( inReading );
}

/**
 * Returns a HTML version of an Entry
 */
QString EntryKanjidic::toHTML() const
{
  QString result = QStringLiteral("<div class=\"KanjidicBrief\">");

  foreach( const QString &field, QSTRINGLISTCHECK( DictFileKanjidic::displayFields ) )
  {
    //qDebug() << "Display: "<<field;
    if( field == QLatin1String("--NewLine--") )              result += QLatin1String("<br>");
    else if( field == QLatin1String("Word/Kanji") )          result += HTMLWord() + ' ';
    else if( field == QLatin1String("Meaning") )             result += HTMLMeanings() + ' ';
    else if( field == QLatin1String("Reading") )             result += HTMLReadings() + ' ';
    else if( ExtendedInfo.contains( field ) ) result += HTMLExtendedInfo( field ) + ' ';
  }

  result += QLatin1String("</div>");
  return result;
}