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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: Query.cxx,v $
*
* $Revision: 1.9 $
*
* last change: $Author: rt $ $Date: 2005-09-09 12:23:21 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
#ifndef _XMLSEARCH_QE_QUERY_HXX_
#include <qe/Query.hxx>
#endif
#ifndef _XMLSEARCH_QE_XMLINDEX_HXX_
#include <qe/XmlIndex.hxx>
#endif
#ifndef _XMLSEARCH_QE_CONCEPTDATA_HXX_
#include <qe/ConceptData.hxx>
#endif
#ifndef _XMLSEARCH_QE_QUERYPROCESSOR_HXX_
#include <qe/QueryProcessor.hxx>
#endif
#ifndef _XMLSEARCH_QE_CONTEXTTABLES_HXX_
#include <qe/ContextTables.hxx>
#endif
using namespace xmlsearch::qe;
sal_Int32* QueryHit::getMatches( sal_Int32& matchesL )
{
matchesL = matchesL_;
return matches_;
}
/******************************************************************************/
/* */
/* HitStore */
/* */
/******************************************************************************/
HitStore::HitStore( double initialStandard,sal_Int32 limit,sal_Int32 nColumns )
: standard_( initialStandard ),
limit_( limit ),
heap_( limit ),
nColumns_( nColumns ),
free_( 0 ),
index_( 0 )
{
for( sal_uInt32 i = 0; i < heap_.size(); ++i )
heap_[i] = 0;
}
HitStore::~HitStore()
{
for( sal_uInt32 i = 0; i < heap_.size(); ++i )
delete heap_[i];
}
bool HitStore::goodEnough( double penalty, sal_Int32 begin, sal_Int32 end )
{
return free_ == limit_ ? heap_[0]->worseThan( penalty,begin,end ) : true;
}
QueryHit* HitStore::createQueryHit( double penalty,sal_Int32 doc,sal_Int32 begin,sal_Int32 end )
{
QueryHit* hit = new QueryHit( nColumns_,penalty,doc,begin,end );
if( free_ == limit_ )
{ // goodEnough'ness checked already
delete heap_[0];
heap_[0] = hit;
heapify( 0 );
standard_ = heap_[0]->getPenalty();
}
else if( free_ < limit_ )
{
heap_[ free_++ ] = hit;
if( free_ == limit_ )
{ // we have the needed number
for( sal_Int32 i = free_/2; i >= 0; --i ) // build heap
heapify( i );
standard_ = heap_[0]->getPenalty();
}
}
return hit;
}
struct CompareQueryHit
{
bool operator()( const QueryHit* l,const QueryHit* r )
{
return l->compareTo( r );
}
};
#include <algorithm>
QueryHit* HitStore::firstBestQueryHit()
{
if( free_ > 0)
{
CompareQueryHit bla;
heap_.resize( free_ );
std::stable_sort( heap_.begin(),heap_.end(),bla );
index_ = 0;
return nextBestQueryHit();
}
else
return 0;
}
QueryHit* HitStore::nextBestQueryHit()
{
return index_ < free_ ? heap_[ index_++ ] : 0;
}
void HitStore::heapify( sal_Int32 i )
{
for( sal_Int32 r,l,worst; ; )
{
r = (i + 1) << 1; l = r - 1;
worst = l < free_ && heap_[i]->betterThan( heap_[l] ) ? l : i;
if( r < free_ && heap_[ worst ]->betterThan( heap_[r] ) )
worst = r;
if (worst != i)
{
QueryHit* temp = heap_[ worst ];
heap_[ worst ] = heap_[ i ];
heap_[i] = temp;
i = worst; // continue
}
else
break;
}
}
// sal_Int32 HitStore::partition( sal_Int32 p,sal_Int32 r )
// {
// QueryHit* x = heap_[ ((p + r) >> 1) & 0x7FFFFFFF ];
// sal_Int32 i = p - 1, j = r + 1;
// while( true )
// {
// while( x->compareTo( heap_[--j] ) )
// ;
// while( heap_[++i]->compareTo( x ) )
// ;
// if( i < j )
// {
// QueryHit* t = heap_[i];
// heap_[i] = heap_[j];
// heap_[j] = t;
// }
// else
// return j;
// }
// }
// void HitStore::quicksort( sal_Int32 p,sal_Int32 r )
// {
// while( p < r )
// {
// sal_Int32 q = partition( p,r );
// quicksort(p, q);
// p = q + 1;
// }
// }
/******************************************************************************/
/* */
/* Query */
/* */
/******************************************************************************/
#define MissingTermPenalty 10.0
Query::Query( XmlIndex* env,
sal_Int32 nColumns,
sal_Int32 nHits,
sal_Int32 missingPenaltiesL,
double* missingPenalties )
: env_( env ),
ctx_( env ? env->getContextInfo() : 0 ),
nColumns_( nColumns ),
nHitsRequested_( nHits ),
missingPenaltyL_( nColumns ),
missingPenalty_( new double[ nColumns ] ),
upperboundTemplateL_( nColumns ),
upperboundTemplate_( new double[ nColumns ] ),
penaltiesL_( missingPenaltiesL ),
penalties_( missingPenalties ),
currentStandard_( nColumns * MissingTermPenalty - 0.0001 ),
missingTermsPenalty_( 0.0 ),
store_( nColumns * MissingTermPenalty - 0.0001,nHits,nColumns ),
ignoredElementsL_( 0 ),
ignoredElements_( 0 )
{
// for the EmptyQuery case (awaits arch improvement pass)
if( missingPenalties )
for( sal_Int32 i = 0;i < nColumns_; ++i )
missingPenalty_[i] = missingPenalties[i];
else
for( sal_Int32 i = 0;i < nColumns_; ++i )
missingPenalty_[i] = MissingTermPenalty;
makePenaltiesTable();
// _roleFillerList = RoleFiller.STOP;
}
Query::~Query()
{
delete[] missingPenalty_;
delete[] upperboundTemplate_;
delete[] penalties_;
delete[] ignoredElements_;
}
void Query::setIgnoredElements( const sal_Int32 ignoredElementsL,const rtl::OUString* ignoredElements )
{
if( ctx_ )
ignoredElements_ = ctx_->getIgnoredElementsSet( ignoredElementsL_,
ignoredElementsL,ignoredElements );
if( ! ctx_ )
{
ignoredElementsL_ = 0;
ignoredElements_ = 0;
}
}
void Query::missingTerms( sal_Int32 nMissingTerms )
{
missingTermsPenalty_ = MissingTermPenalty * nMissingTerms;
}
ConceptData* Query::makeConceptData( sal_Int32 col,sal_Int32 concept,double penalty,sal_Int32 queryNo )
{
return new ConceptData( concept,col,penalty,queryNo,nColumns_,env_->getContextInfo() );;
}
void Query::getHits( std::vector< QueryHitData* >& data,sal_Int32 n )
{
if( n <= 0 )
return;
QueryHit* qh = store_.firstBestQueryHit();
while( qh )
{
data.push_back( env_->hitToData( qh ) );
qh = data.size() < sal_uInt32( n ) ? store_.nextBestQueryHit() : 0;
}
}
QueryHit* Query::maybeCreateQueryHit( double penalty,
sal_Int32 doc, sal_Int32 begin, sal_Int32 end, sal_Int32 parentContext )
{
// hits are located using only terms actually present in text
// if B is not present, the query A B C reduces to A C and penalties
// are computed as if B did not occur in query
// to meaningfully merge results from different servers, some of which
// may have B, penalty has to be normalized to the common computing scheme
QueryHit* res =
( store_.goodEnough( penalty += missingTermsPenalty_,begin,end )
&& ( ! ignoredElements_ || ctx_->notIgnored( parentContext,ignoredElementsL_,ignoredElements_ ) ) )
?
store_.createQueryHit( penalty,doc,begin,end )
:
0;
return res;
}
void Query::makePenaltiesTable()
{
sal_Int32 nPatterns = 1 << nColumns_;
delete[] penalties_;
penalties_ = new double[ penaltiesL_ = nPatterns ];
for (sal_Int32 i = 0; i < nPatterns; ++i )
penalties_[i] = computePenalty(i);
}
double Query::computePenalty( sal_Int32 n )
{
double penalty = 0.0;
for( sal_Int32 i = 0; i < nColumns_; ++i )
if( ( n & 1 << i ) == 0 )
penalty += missingPenalty_[i];
return penalty;
}
void Query::resetForNextDocument()
{
currentStandard_ = store_.getCurrentStandard();
// "everything's missing"
for( sal_Int32 i = 0; i < nColumns_; i++ )
upperboundTemplate_[i] = missingPenalty_[i];
vote_ = false;
}
bool Query::vote()
{
double sum = 0.0;
for( sal_Int32 i = 0; i < nColumns_; i++ )
sum += upperboundTemplate_[i];
return vote_ = (sum <= currentStandard_ );
}
void Query::updateEstimate( sal_Int32 role,double penalty )
{
if( penalty < upperboundTemplate_[ role ] )
upperboundTemplate_[ role ] = penalty;
}
/******************************************************************************/
/* */
/* QueryHitIterator */
/* */
/******************************************************************************/
QueryHitIterator::QueryHitIterator( const QueryResults* result )
: result_( result ),
index_( -1 )
{
}
QueryHitIterator::~QueryHitIterator()
{
delete result_;
}
bool QueryHitIterator::next()
{
return accessible_ = ( ++index_ < sal_Int32( result_->queryHits_.size() ) );
}
QueryHitData* QueryHitIterator::getHit( const PrefixTranslator* ) const
{
if( accessible_ )
return result_->queryHits_[index_];
else
return 0;
}
double QueryHitIterator::getPenalty()
{
if( accessible_ )
return result_->queryHits_[index_]->getPenalty();
else
return 1.0E30;
}
|