File: radicalfile.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 (167 lines) | stat: -rw-r--r-- 4,466 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
/*
    This file is part of Kiten, a KDE Japanese Reference Tool
    SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>

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

#include "DictKanjidic/dictfilekanjidic.h"
#include "kitenmacros.h"
#include "radicalfile.h"

#include <QFile>
#include <QRegExp>
#include <QString>
#include <QTextCodec>
#include <QTextStream>

RadicalFile::RadicalFile( QString &radkfile, const QString &kanjidic )
{
  loadRadicalFile( radkfile );
  if( ! kanjidic.isEmpty() )
  {
    loadKanjidic( kanjidic );
  }
}

QSet<Kanji> RadicalFile::kanjiContainingRadicals( QSet<QString> &radicallist ) const
{
  QSet<QString> kanjiStringSet;
  QSet<Kanji> result;
  if( m_radicals.count() < 1 || radicallist.count() < 1 )
  {
    return result;
  }

  //Start out with our first set
  kanjiStringSet = m_radicals[ *radicallist.begin() ].getKanji();
  //Make a set intersection of these m_kanji
  foreach( const QString &rad, radicallist )
  {
    kanjiStringSet &= m_radicals[ rad ].getKanji();
  }

  //Convert our set of QString to a set of Kanji
  foreach( const QString &kanji, kanjiStringSet )
  {
    result += m_kanji[ kanji ];
  }

  return result;
}

bool RadicalFile::loadRadicalFile( QString &radkfile )
{
  QFile f( radkfile );
  if ( ! f.open( QIODevice::ReadOnly ) )
  {
    return false;
  }

  //Read our radical file through a eucJP codec (helpfully builtin to Qt)
  QTextStream t( &f );
  Radical *newestRadical = nullptr;
  QHash< QString, QSet<QString> > krad;

  t.setCodec( QTextCodec::codecForName( "eucJP" ) );
  while ( ! t.atEnd() )
  {
    QString line = t.readLine();
    if( line.length() == 0 || line.at( 0 ) == '#' )
    {
      //Skip comment characters
      continue;
    }
    else if( line.at( 0 ) == '$' )
    {
      //Start of a new radical
      if( newestRadical != nullptr )
      {
        m_radicals.insert( newestRadical->toString(), *newestRadical );
      }
      delete newestRadical;
      QStringList lineElements = line.split( QRegExp( QStringLiteral( "\\s+" ) ) );
      newestRadical = new Radical(  lineElements.at( 1 )
                                  , lineElements.at( 2 ).toUInt()
                                  , m_radicals.size() );
    }
    else if( newestRadical != nullptr )
    {
      // List of m_kanji, potentially
      const QList<QString> m_kanjiList = line.trimmed().split( QLatin1String(""), Qt::SkipEmptyParts );
      const QSet<QString> kanjiSet = QSet<QString>(m_kanjiList.begin(), m_kanjiList.end());
      newestRadical->addKanji( kanjiSet );
      foreach( const QString &kanji, m_kanjiList )
      {
        krad[ kanji ] += newestRadical->toString();
      }
    }
  }
  if( newestRadical != nullptr )
  {
    m_radicals[ newestRadical->toString() ] = *newestRadical;
    delete newestRadical;
  }

  //Move contents of our krad QHash into our hash of m_kanji
  QHash<QString,QSet<QString> >::iterator it;
  for( it = krad.begin(); it != krad.end(); ++it )
  {
    m_kanji.insert(   it.key()
                    , Kanji( it.key()
                    , it.value() ) );
  }
  f.close();
  return true;
}

// Mostly copied from KanjiBrowser::loadKanji()
bool RadicalFile::loadKanjidic( const QString &kanjidic )
{
  DictFileKanjidic dictFileKanjidic;
  dictFileKanjidic.loadSettings();
  dictFileKanjidic.loadDictionary( kanjidic, KANJIDIC );

  QRegExp strokeMatch( "^S\\d+" );
  foreach( const QString &line, dictFileKanjidic.dumpDictionary() )
  {
    const QString kanji = line[ 0 ];

    QStringList strokesSection = line.split( " ", Qt::SkipEmptyParts )
                                     .filter( strokeMatch );

    unsigned int strokes = strokesSection.first().remove( 0, 1 ).toInt();

    if( m_kanji.contains( kanji ) ) {
      m_kanji[ kanji ].setStrokes( strokes );
    }
  }

  return true;
}

QMultiMap<int,Radical>* RadicalFile::mapRadicalsByStrokes( int max_strokes ) const
{
  QMultiMap<int, Radical> *result = new QMultiMap<int, Radical>();
  foreach( const Radical &rad, m_radicals )
  {
    int strokes = rad.strokes();
    if( ( max_strokes > 0 ) && ( strokes > max_strokes ) )
    {
      strokes = max_strokes;
    }
    result->insert( strokes, rad );
  }
  return result;
}

QSet<QString> RadicalFile::radicalsInKanji( QSet<Kanji> &kanjilist ) const
{
  QSet<QString> possibleRadicals;
  foreach( const QString &kanji, kanjilist )
  {
    possibleRadicals |= m_kanji[ kanji ].getRadicals();
  }

  return possibleRadicals;
}