1
/*****************************************************************************
2
* This file is part of Kiten, a KDE Japanese Reference Tool *
3
* Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Library General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2 of the License, or (at your option) any later version. *
10
* This library is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13
* Library General Public License for more details. *
15
* You should have received a copy of the GNU Library General Public License *
16
* along with this library; see the file COPYING.LIB. If not, write to *
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18
* Boston, MA 02110-1301, USA. *
19
*****************************************************************************/
21
#include "radicalfile.h"
26
#include <QTextStream>
28
RadicalFile::RadicalFile( QString &radkfile )
30
loadRadicalFile(radkfile);
33
QSet<Kanji> RadicalFile::kanjiContainingRadicals( QSet<QString> &radicallist ) const
35
QSet<QString> kanjiStringSet;
37
if( m_radicals.count() < 1 || radicallist.count() < 1 )
42
//Start out with our first set
43
kanjiStringSet = m_radicals[ *radicallist.begin() ].getKanji();
44
//Make a set intersection of these m_kanji
45
foreach( const QString &a_rad, radicallist )
47
kanjiStringSet &= m_radicals[ a_rad ].getKanji();
50
//Convert our set of QString to a set of Kanji
51
foreach( const QString &akanji, kanjiStringSet )
53
result += m_kanji[ akanji ];
59
bool RadicalFile::loadRadicalFile( QString &radkfile )
62
if ( ! f.open(QIODevice::ReadOnly ) )
67
//Read our radical file through a eucJP codec (helpfully builtin to Qt)
69
Radical *newestRadical = NULL;
70
QHash< QString, QSet<QString> > krad;
72
t.setCodec( QTextCodec::codecForName( "eucJP" ) );
75
QString line = t.readLine();
76
if( line.length() == 0 || line.at(0) == '#' )
78
//Skip comment characters
81
else if( line.at( 0 ) == '$' )
83
//Start of a new radical
84
if( newestRadical != NULL )
86
m_radicals.insert( *newestRadical, *newestRadical );
88
newestRadical = new Radical( QString( line.at( 2 ) )
89
, line.right( 2 ).toUInt() );
91
else if( newestRadical != NULL )
93
// List of m_kanji, potentially
94
QList<QString> m_kanjiList = line.trimmed().split( "", QString::SkipEmptyParts );
95
newestRadical->addKanji( m_kanjiList.toSet() );
96
foreach( const QString &am_kanji, m_kanjiList )
98
krad[ am_kanji ] += *newestRadical;
102
if( newestRadical != NULL )
104
m_radicals[ *newestRadical ] = *newestRadical;
107
//Move contents of our krad QHash into our hash of m_kanji
108
QHash<QString,QSet<QString> >::iterator it;
109
for( it = krad.begin(); it != krad.end(); ++it )
111
m_kanji.insert( it.key(), Kanji( it.key(), it.value() ) )
112
->calculateStrokes( m_radicals.values() );
118
QMultiMap<int,Radical>* RadicalFile::mapRadicalsByStrokes() const
120
QMultiMap<int, Radical> *result = new QMultiMap<int, Radical>();
121
foreach( const Radical &rad, m_radicals )
123
result->insert( m_radicals[ rad ].strokes(), m_radicals[ rad ] );
128
QSet<QString> RadicalFile::radicalsInKanji( QSet<Kanji> &kanjilist ) const
130
QSet<QString> possibleRadicals;
131
foreach( const QString &akanji, kanjilist )
133
possibleRadicals |= m_kanji[ akanji ].getRadicals();
136
return possibleRadicals;