~ubuntu-branches/ubuntu/quantal/kiten/quantal-proposed

« back to all changes in this revision

Viewing changes to radselect/radicalfile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2011-07-10 11:23:47 UTC
  • Revision ID: james.westby@ubuntu.com-20110710112347-ykfhtvam3kgssspo
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 
3
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 
4
 *                                                                           *
 
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.          *
 
9
 *                                                                           *
 
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.                          *
 
14
 *                                                                           *
 
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
 *****************************************************************************/
 
20
 
 
21
#include "radicalfile.h"
 
22
 
 
23
#include <QFile>
 
24
#include <QString>
 
25
#include <QTextCodec>
 
26
#include <QTextStream>
 
27
 
 
28
RadicalFile::RadicalFile( QString &radkfile )
 
29
{
 
30
  loadRadicalFile(radkfile);
 
31
}
 
32
 
 
33
QSet<Kanji> RadicalFile::kanjiContainingRadicals( QSet<QString> &radicallist ) const
 
34
{
 
35
  QSet<QString> kanjiStringSet;
 
36
  QSet<Kanji> result;
 
37
  if( m_radicals.count() < 1 || radicallist.count() < 1 )
 
38
  {
 
39
    return result;
 
40
  }
 
41
 
 
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 )
 
46
  {
 
47
    kanjiStringSet &= m_radicals[ a_rad ].getKanji();
 
48
  }
 
49
 
 
50
  //Convert our set of QString to a set of Kanji
 
51
  foreach( const QString &akanji, kanjiStringSet )
 
52
  {
 
53
    result += m_kanji[ akanji ];
 
54
  }
 
55
 
 
56
  return result;
 
57
}
 
58
 
 
59
bool RadicalFile::loadRadicalFile( QString &radkfile )
 
60
{
 
61
  QFile f( radkfile );
 
62
  if ( ! f.open(QIODevice::ReadOnly ) )
 
63
  {
 
64
    return false;
 
65
  }
 
66
 
 
67
  //Read our radical file through a eucJP codec (helpfully builtin to Qt)
 
68
  QTextStream t( &f );
 
69
  Radical *newestRadical = NULL;
 
70
  QHash< QString, QSet<QString> > krad;
 
71
 
 
72
  t.setCodec( QTextCodec::codecForName( "eucJP" ) );
 
73
  while ( ! t.atEnd() )
 
74
  {
 
75
    QString line = t.readLine();
 
76
    if( line.length() == 0 || line.at(0) == '#' )
 
77
    {
 
78
      //Skip comment characters
 
79
      continue;
 
80
    }
 
81
    else if( line.at( 0 ) == '$' )
 
82
    {
 
83
      //Start of a new radical
 
84
      if( newestRadical != NULL )
 
85
      {
 
86
        m_radicals.insert( *newestRadical, *newestRadical );
 
87
      }
 
88
      newestRadical = new Radical(  QString( line.at( 2 ) )
 
89
                                  , line.right( 2 ).toUInt() );
 
90
    }
 
91
    else if( newestRadical != NULL )
 
92
    {
 
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 )
 
97
      {
 
98
        krad[ am_kanji ] += *newestRadical;
 
99
      }
 
100
    }
 
101
  }
 
102
  if( newestRadical != NULL )
 
103
  {
 
104
    m_radicals[ *newestRadical ] = *newestRadical;
 
105
  }
 
106
 
 
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 )
 
110
  {
 
111
    m_kanji.insert( it.key(), Kanji( it.key(), it.value() ) )
 
112
                              ->calculateStrokes( m_radicals.values() );
 
113
  }
 
114
  f.close();
 
115
  return true;
 
116
}
 
117
 
 
118
QMultiMap<int,Radical>* RadicalFile::mapRadicalsByStrokes() const
 
119
{
 
120
  QMultiMap<int, Radical> *result = new QMultiMap<int, Radical>();
 
121
  foreach( const Radical &rad, m_radicals )
 
122
  {
 
123
    result->insert( m_radicals[ rad ].strokes(), m_radicals[ rad ] );
 
124
  }
 
125
  return result;
 
126
}
 
127
 
 
128
QSet<QString> RadicalFile::radicalsInKanji( QSet<Kanji> &kanjilist ) const
 
129
{
 
130
  QSet<QString> possibleRadicals;
 
131
  foreach( const QString &akanji, kanjilist )
 
132
  {
 
133
    possibleRadicals |= m_kanji[ akanji ].getRadicals();
 
134
  }
 
135
 
 
136
  return possibleRadicals;
 
137
}