summaryrefslogtreecommitdiffstats
path: root/kbabel/kbabeldict/modules/tmx/tmxcompendiumdata.cpp
blob: 3fa84aa462f335c9136a2a27024c743f4303f24f (plain)
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
/* ****************************************************************************
  This file is part of KBabel

  Copyright (C) 2001 by Matthias Kiefer
                            <matthias.kiefer@gmx.de>
		2002 by Stanislav Visnovsky
			    <visnovsky@kde.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the Qt library by Trolltech AS, Norway (or with modified versions
  of Qt that use the same license as Qt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  Qt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.
	
**************************************************************************** */
#include "tmxcompendiumdata.h"

#include <qdom.h>
#include <qfile.h>

#include <tagextractor.h>
#include <resources.h>

#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kio/netaccess.h>

using namespace KBabel;

TmxCompendiumData::TmxCompendiumData(QObject *parent)
            : QObject(parent)
            , _active(false)
            , _error(false)
            , _initialized(false)
            , _exactDict(9887)
            , _allDict(9887)
            , _wordDict(9887)
{
    _exactDict.setAutoDelete(true);
    _allDict.setAutoDelete(true);
    _wordDict.setAutoDelete(true);
}


bool TmxCompendiumData::load(const KURL& url, const QString& language)
{
    kdDebug(KBABEL_SEARCH) << "Load " << url.url() << " in " << language << endl;
    if(_active)
        return false;


    _error = false;
    _active = true;

    _exactDict.clear();
    _allDict.clear();
    _wordDict.clear();
    

    emit progressStarts(i18n("Loading TMX compendium"));

    QDomDocument doc( "mydocument" );
    
    QString target;
    
    if( KIO::NetAccess::download( url, target ) )
    {
	QFile f( target );
	if ( !f.open( IO_ReadOnly ) )
	{
    	    _error = true;
	    _errorMsg = i18n( "Cannot open the file." );
	}    
	else if ( !doc.setContent( &f ) ) {
	    _error = true;
	    _errorMsg = i18n( "Cannot parse XML data." );
	}
	f.close();
	KIO::NetAccess::removeTempFile(target);
    } else {
	_error = true;
	_errorMsg = i18n( "Cannot open the file." );
    } 
       
    QDomElement docElem = doc.documentElement();
    
    if( docElem.tagName() != "tmx" || !(docElem.hasAttribute("version")
	  && docElem.attribute("version") == "1.4" ) )
    {
	_error = true;
	_errorMsg = i18n( "Unsupported format.");
    }
			      
				    
    if( _error )
    {
        _errorMsg = i18n("Error while trying to read file for TMX Compendium module:\n"
	    "%1\n"
	    "Reason: %2")
            .arg(url.prettyURL()).arg(_errorMsg);

        kdDebug(KBABEL_SEARCH) << "Error:  " << _errorMsg << endl;

        emit progressEnds();

        _active = false;
        _initialized=true;

        return false;
    }

    emit progressStarts(i18n("Building indices"));
    
    QDomNodeList tuTags = docElem.elementsByTagName("tu");
    uint total = tuTags.count();
    
    _originals.clear();
    _originals.resize(total);
    _translations.clear();
    _translations.resize(total);
    
    uint lastindex = 0;
    
    for(uint i=0; i < total; i++)
    { 
        if( (100*(i+1))%total < 100 )
        {
            emit progress((100*(i+1))/total);
            kapp->processEvents(100);
        }
	
    	QDomNodeList tuvTags = tuTags.item(i).toElement().elementsByTagName("tuv");
	QString english, other;
	for( uint j = 0 ; j < tuvTags.count() ; j++ )
        {
	    QDomElement el = tuvTags.item(j).toElement();
	    if( el.attribute("xml:lang").upper() == "EN" ) english = el.text();
	    if( el.attribute("xml:lang").upper().startsWith(language.upper()) )
		other = el.text(); 
	}
	
	if( !english.isEmpty() && !other.isEmpty() )
	{
	    kdDebug(KBABEL_SEARCH) << english << " to " << lastindex << endl;
	    _originals[lastindex] = english;
	    _translations[lastindex] = other;
		
	    QString temp = english;
        
    	    int *index = new int(lastindex);
    	    _exactDict.insert(temp,index);

	    temp = simplify(temp);
    	    temp = temp.lower();

	    if(!temp.isEmpty() && temp.length() > 1)
    	    {
    		QValueList<int> *indexList=_allDict[temp];
            
        	if(!indexList)
        	{
            	    indexList = new QValueList<int>;
	            _allDict.insert(temp,indexList);
    		}

	        indexList->append(lastindex);
	    
	        QStringList wList = wordList(temp);
	        for ( QStringList::Iterator it = wList.begin()
                	; it != wList.end(); ++it ) 
        	{
            	    if( (*it).length() > 1)
            	    {
	                indexList=_wordDict[*it];
            
    		        if(!indexList)
            		{
                    	    indexList = new QValueList<int>;
	                    _wordDict.insert(*it,indexList);
        	        }
			
			indexList->append(lastindex);
            	    }
        	}
	    }
	    lastindex++;
	}
    }


    // remove words, that are too frequent
    uint max=_allDict.count()/10;
    QDictIterator< QValueList<int> > it(_wordDict);
    while ( it.current() ) 
    {
        if(it.current()->count() > max)
        {
            _wordDict.remove(it.currentKey());
        }
        else
        {
            ++it;
        }
    }
    
    if( lastindex == 0) 
    {
	_error = true;
	_errorMsg = i18n("Empty database.");
    }
    
    kdDebug(KBABEL_SEARCH) << "load done" << endl;

    _initialized=true;

    emit progressEnds();

    _active = false;

    return true;
}

const int* TmxCompendiumData::exactDict(const QString& text) const
{
    return _exactDict[text];
}

const QValueList<int>* TmxCompendiumData::allDict(const QString& text) const
{
    return _allDict[text];
}

const QValueList<int>* TmxCompendiumData::wordDict(const QString& text) const
{
    return _wordDict[text];
}

const QString TmxCompendiumData::msgid(const int index) const
{
    return _originals[index];
}

const QString TmxCompendiumData::msgstr(const int index) const
{
    return _translations[index];
}


void TmxCompendiumData::registerObject(QObject *obj)
{
    if(!_registered.containsRef(obj))
        _registered.append(obj);
}

bool TmxCompendiumData::unregisterObject(QObject *obj)
{
    _registered.removeRef(obj);

    return _registered.count()==0;
}

bool TmxCompendiumData::hasObjects() const
{
    return _registered.count()==0;
}

QString TmxCompendiumData::simplify(const QString& string)
{ 
    QString result;

    TagExtractor te;
    te.setString(string);
    result=te.plainString();

    result=result.simplifyWhiteSpace();
    result=result.stripWhiteSpace();

    return result;
}

QStringList TmxCompendiumData::wordList(const QString& string)
{    
    QString result=TmxCompendiumData::simplify(string);
   
    return QStringList::split(' ',result);
}

#include "tmxcompendiumdata.moc"