radicalfile.cpp 4.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*****************************************************************************
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 *                                                                           *
 * This library is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Library General Public               *
 * License as published by the Free Software Foundation; either              *
 * version 2 of the License, or (at your option) any later version.          *
 *                                                                           *
 * This library is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public License *
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 * Boston, MA 02110-1301, USA.                                               *
 *****************************************************************************/
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
20

21
#include "radicalfile.h"
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
22

23
24
25
26
#include <QFile>
#include <QString>
#include <QTextCodec>
#include <QTextStream>
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
27

28
29
RadicalFile::RadicalFile( QString &radkfile )
{
30
  loadRadicalFile( radkfile );
31
}
32

33
34
35
36
37
38
39
40
41
42
43
44
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
45
  foreach( const QString &rad, radicallist )
46
  {
47
    kanjiStringSet &= m_radicals[ rad ].getKanji();
48
  }
49

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

56
  return result;
57
58
}

59
bool RadicalFile::loadRadicalFile( QString &radkfile )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
60
61
{
  QFile f( radkfile );
62
  if ( ! f.open( QIODevice::ReadOnly ) )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
63
64
65
66
67
68
  {
    return false;
  }

  //Read our radical file through a eucJP codec (helpfully builtin to Qt)
  QTextStream t( &f );
Laurent Montel's avatar
Laurent Montel committed
69
  Radical *newestRadical = nullptr;
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
70
71
72
73
74
75
  QHash< QString, QSet<QString> > krad;

  t.setCodec( QTextCodec::codecForName( "eucJP" ) );
  while ( ! t.atEnd() )
  {
    QString line = t.readLine();
76
    if( line.length() == 0 || line.at( 0 ) == '#' )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
77
78
79
80
81
82
83
    {
      //Skip comment characters
      continue;
    }
    else if( line.at( 0 ) == '$' )
    {
      //Start of a new radical
Laurent Montel's avatar
Laurent Montel committed
84
      if( newestRadical != nullptr )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
85
86
87
      {
        m_radicals.insert( *newestRadical, *newestRadical );
      }
Laurent Montel's avatar
Laurent Montel committed
88
      delete newestRadical;
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
89
      newestRadical = new Radical(  QString( line.at( 2 ) )
Laurent Montel's avatar
Laurent Montel committed
90
                                  , line.rightRef( 2 ).toUInt() );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
91
    }
Laurent Montel's avatar
Laurent Montel committed
92
    else if( newestRadical != nullptr )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
93
94
    {
      // List of m_kanji, potentially
95
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
Laurent Montel's avatar
Laurent Montel committed
96
      QList<QString> m_kanjiList = line.trimmed().split( QLatin1String(""), QString::SkipEmptyParts );
97
98
99
#else
      QList<QString> m_kanjiList = line.trimmed().split( QLatin1String(""), Qt::SkipEmptyParts );
#endif
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
100
      newestRadical->addKanji( m_kanjiList.toSet() );
101
      foreach( const QString &kanji, m_kanjiList )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
102
      {
103
        krad[ kanji ] += *newestRadical;
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
104
105
106
      }
    }
  }
Laurent Montel's avatar
Laurent Montel committed
107
  if( newestRadical != nullptr )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
108
109
  {
    m_radicals[ *newestRadical ] = *newestRadical;
Maximiliano Curia's avatar
Maximiliano Curia committed
110
    delete newestRadical;
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
111
112
113
114
115
116
  }

  //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 )
  {
117
118
119
    m_kanji.insert(   it.key()
                    , Kanji( it.key()
                    , it.value() ) )->calculateStrokes( m_radicals.values() );
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
120
121
122
  }
  f.close();
  return true;
123
124
}

125
QMultiMap<int,Radical>* RadicalFile::mapRadicalsByStrokes() const
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
126
127
128
129
130
131
132
{
  QMultiMap<int, Radical> *result = new QMultiMap<int, Radical>();
  foreach( const Radical &rad, m_radicals )
  {
    result->insert( m_radicals[ rad ].strokes(), m_radicals[ rad ] );
  }
  return result;
133
134
}

135
QSet<QString> RadicalFile::radicalsInKanji( QSet<Kanji> &kanjilist ) const
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
136
137
{
  QSet<QString> possibleRadicals;
138
  foreach( const QString &kanji, kanjilist )
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
139
  {
140
    possibleRadicals |= m_kanji[ kanji ].getRadicals();
Daniel E. Moctezuma's avatar
Daniel E. Moctezuma committed
141
142
143
  }

  return possibleRadicals;
144
}