| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Zend Framework
|
|
|
4 |
*
|
|
|
5 |
* LICENSE
|
|
|
6 |
*
|
|
|
7 |
* This source file is subject to version 1.0 of the Zend Framework
|
|
|
8 |
* license, that is bundled with this package in the file LICENSE, and
|
|
|
9 |
* is available through the world-wide-web at the following URL:
|
|
|
10 |
* http://www.zend.com/license/framework/1_0.txt. If you did not receive
|
|
|
11 |
* a copy of the Zend Framework license and are unable to obtain it
|
|
|
12 |
* through the world-wide-web, please send a note to license@zend.com
|
|
|
13 |
* so we can mail you a copy immediately.
|
|
|
14 |
*
|
|
|
15 |
* @package Zend_Search_Lucene
|
|
|
16 |
* @subpackage Search
|
|
|
17 |
* @copyright Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
|
|
|
18 |
* @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
/** Zend_Search_Lucene_Search_Similarity_Default */
|
|
|
23 |
require_once 'Zend/Search/Lucene/Search/Similarity/Default.php';
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @package Zend_Search_Lucene
|
|
|
28 |
* @subpackage Search
|
|
|
29 |
* @copyright Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
|
|
|
30 |
* @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
|
|
|
31 |
*/
|
|
|
32 |
abstract class Zend_Search_Lucene_Search_Similarity
|
|
|
33 |
{
|
|
|
34 |
/**
|
|
|
35 |
* The Similarity implementation used by default.
|
|
|
36 |
*
|
|
|
37 |
* @var Zend_Search_Lucene_Search_Similarity
|
|
|
38 |
*/
|
|
|
39 |
static private $_defaultImpl;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Cache of decoded bytes.
|
|
|
43 |
* Array of floats
|
|
|
44 |
*
|
|
|
45 |
* @var array
|
|
|
46 |
*/
|
|
|
47 |
static private $_normTable = array( 0 => 0.0,
|
|
|
48 |
1 => 5.820766E-10,
|
|
|
49 |
2 => 6.9849193E-10,
|
|
|
50 |
3 => 8.1490725E-10,
|
|
|
51 |
4 => 9.313226E-10,
|
|
|
52 |
5 => 1.1641532E-9,
|
|
|
53 |
6 => 1.3969839E-9,
|
|
|
54 |
7 => 1.6298145E-9,
|
|
|
55 |
8 => 1.8626451E-9,
|
|
|
56 |
9 => 2.3283064E-9,
|
|
|
57 |
10 => 2.7939677E-9,
|
|
|
58 |
11 => 3.259629E-9,
|
|
|
59 |
12 => 3.7252903E-9,
|
|
|
60 |
13 => 4.656613E-9,
|
|
|
61 |
14 => 5.5879354E-9,
|
|
|
62 |
15 => 6.519258E-9,
|
|
|
63 |
16 => 7.4505806E-9,
|
|
|
64 |
17 => 9.313226E-9,
|
|
|
65 |
18 => 1.1175871E-8,
|
|
|
66 |
19 => 1.3038516E-8,
|
|
|
67 |
20 => 1.4901161E-8,
|
|
|
68 |
21 => 1.8626451E-8,
|
|
|
69 |
22 => 2.2351742E-8,
|
|
|
70 |
23 => 2.6077032E-8,
|
|
|
71 |
24 => 2.9802322E-8,
|
|
|
72 |
25 => 3.7252903E-8,
|
|
|
73 |
26 => 4.4703484E-8,
|
|
|
74 |
27 => 5.2154064E-8,
|
|
|
75 |
28 => 5.9604645E-8,
|
|
|
76 |
29 => 7.4505806E-8,
|
|
|
77 |
30 => 8.940697E-8,
|
|
|
78 |
31 => 1.0430813E-7,
|
|
|
79 |
32 => 1.1920929E-7,
|
|
|
80 |
33 => 1.4901161E-7,
|
|
|
81 |
34 => 1.7881393E-7,
|
|
|
82 |
35 => 2.0861626E-7,
|
|
|
83 |
36 => 2.3841858E-7,
|
|
|
84 |
37 => 2.9802322E-7,
|
|
|
85 |
38 => 3.5762787E-7,
|
|
|
86 |
39 => 4.172325E-7,
|
|
|
87 |
40 => 4.7683716E-7,
|
|
|
88 |
41 => 5.9604645E-7,
|
|
|
89 |
42 => 7.1525574E-7,
|
|
|
90 |
43 => 8.34465E-7,
|
|
|
91 |
44 => 9.536743E-7,
|
|
|
92 |
45 => 1.1920929E-6,
|
|
|
93 |
46 => 1.4305115E-6,
|
|
|
94 |
47 => 1.66893E-6,
|
|
|
95 |
48 => 1.9073486E-6,
|
|
|
96 |
49 => 2.3841858E-6,
|
|
|
97 |
50 => 2.861023E-6,
|
|
|
98 |
51 => 3.33786E-6,
|
|
|
99 |
52 => 3.8146973E-6,
|
|
|
100 |
53 => 4.7683716E-6,
|
|
|
101 |
54 => 5.722046E-6,
|
|
|
102 |
55 => 6.67572E-6,
|
|
|
103 |
56 => 7.6293945E-6,
|
|
|
104 |
57 => 9.536743E-6,
|
|
|
105 |
58 => 1.1444092E-5,
|
|
|
106 |
59 => 1.335144E-5,
|
|
|
107 |
60 => 1.5258789E-5,
|
|
|
108 |
61 => 1.9073486E-5,
|
|
|
109 |
62 => 2.2888184E-5,
|
|
|
110 |
63 => 2.670288E-5,
|
|
|
111 |
64 => 3.0517578E-5,
|
|
|
112 |
65 => 3.8146973E-5,
|
|
|
113 |
66 => 4.5776367E-5,
|
|
|
114 |
67 => 5.340576E-5,
|
|
|
115 |
68 => 6.1035156E-5,
|
|
|
116 |
69 => 7.6293945E-5,
|
|
|
117 |
70 => 9.1552734E-5,
|
|
|
118 |
71 => 1.0681152E-4,
|
|
|
119 |
72 => 1.2207031E-4,
|
|
|
120 |
73 => 1.5258789E-4,
|
|
|
121 |
74 => 1.8310547E-4,
|
|
|
122 |
75 => 2.1362305E-4,
|
|
|
123 |
76 => 2.4414062E-4,
|
|
|
124 |
77 => 3.0517578E-4,
|
|
|
125 |
78 => 3.6621094E-4,
|
|
|
126 |
79 => 4.272461E-4,
|
|
|
127 |
80 => 4.8828125E-4,
|
|
|
128 |
81 => 6.1035156E-4,
|
|
|
129 |
82 => 7.324219E-4,
|
|
|
130 |
83 => 8.544922E-4,
|
|
|
131 |
84 => 9.765625E-4,
|
|
|
132 |
85 => 0.0012207031,
|
|
|
133 |
86 => 0.0014648438,
|
|
|
134 |
87 => 0.0017089844,
|
|
|
135 |
88 => 0.001953125,
|
|
|
136 |
89 => 0.0024414062,
|
|
|
137 |
90 => 0.0029296875,
|
|
|
138 |
91 => 0.0034179688,
|
|
|
139 |
92 => 0.00390625,
|
|
|
140 |
93 => 0.0048828125,
|
|
|
141 |
94 => 0.005859375,
|
|
|
142 |
95 => 0.0068359375,
|
|
|
143 |
96 => 0.0078125,
|
|
|
144 |
97 => 0.009765625,
|
|
|
145 |
98 => 0.01171875,
|
|
|
146 |
99 => 0.013671875,
|
|
|
147 |
100 => 0.015625,
|
|
|
148 |
101 => 0.01953125,
|
|
|
149 |
102 => 0.0234375,
|
|
|
150 |
103 => 0.02734375,
|
|
|
151 |
104 => 0.03125,
|
|
|
152 |
105 => 0.0390625,
|
|
|
153 |
106 => 0.046875,
|
|
|
154 |
107 => 0.0546875,
|
|
|
155 |
108 => 0.0625,
|
|
|
156 |
109 => 0.078125,
|
|
|
157 |
110 => 0.09375,
|
|
|
158 |
111 => 0.109375,
|
|
|
159 |
112 => 0.125,
|
|
|
160 |
113 => 0.15625,
|
|
|
161 |
114 => 0.1875,
|
|
|
162 |
115 => 0.21875,
|
|
|
163 |
116 => 0.25,
|
|
|
164 |
117 => 0.3125,
|
|
|
165 |
118 => 0.375,
|
|
|
166 |
119 => 0.4375,
|
|
|
167 |
120 => 0.5,
|
|
|
168 |
121 => 0.625,
|
|
|
169 |
122 => 0.75,
|
|
|
170 |
123 => 0.875,
|
|
|
171 |
124 => 1.0,
|
|
|
172 |
125 => 1.25,
|
|
|
173 |
126 => 1.5,
|
|
|
174 |
127 => 1.75,
|
|
|
175 |
128 => 2.0,
|
|
|
176 |
129 => 2.5,
|
|
|
177 |
130 => 3.0,
|
|
|
178 |
131 => 3.5,
|
|
|
179 |
132 => 4.0,
|
|
|
180 |
133 => 5.0,
|
|
|
181 |
134 => 6.0,
|
|
|
182 |
135 => 7.0,
|
|
|
183 |
136 => 8.0,
|
|
|
184 |
137 => 10.0,
|
|
|
185 |
138 => 12.0,
|
|
|
186 |
139 => 14.0,
|
|
|
187 |
140 => 16.0,
|
|
|
188 |
141 => 20.0,
|
|
|
189 |
142 => 24.0,
|
|
|
190 |
143 => 28.0,
|
|
|
191 |
144 => 32.0,
|
|
|
192 |
145 => 40.0,
|
|
|
193 |
146 => 48.0,
|
|
|
194 |
147 => 56.0,
|
|
|
195 |
148 => 64.0,
|
|
|
196 |
149 => 80.0,
|
|
|
197 |
150 => 96.0,
|
|
|
198 |
151 => 112.0,
|
|
|
199 |
152 => 128.0,
|
|
|
200 |
153 => 160.0,
|
|
|
201 |
154 => 192.0,
|
|
|
202 |
155 => 224.0,
|
|
|
203 |
156 => 256.0,
|
|
|
204 |
157 => 320.0,
|
|
|
205 |
158 => 384.0,
|
|
|
206 |
159 => 448.0,
|
|
|
207 |
160 => 512.0,
|
|
|
208 |
161 => 640.0,
|
|
|
209 |
162 => 768.0,
|
|
|
210 |
163 => 896.0,
|
|
|
211 |
164 => 1024.0,
|
|
|
212 |
165 => 1280.0,
|
|
|
213 |
166 => 1536.0,
|
|
|
214 |
167 => 1792.0,
|
|
|
215 |
168 => 2048.0,
|
|
|
216 |
169 => 2560.0,
|
|
|
217 |
170 => 3072.0,
|
|
|
218 |
171 => 3584.0,
|
|
|
219 |
172 => 4096.0,
|
|
|
220 |
173 => 5120.0,
|
|
|
221 |
174 => 6144.0,
|
|
|
222 |
175 => 7168.0,
|
|
|
223 |
176 => 8192.0,
|
|
|
224 |
177 => 10240.0,
|
|
|
225 |
178 => 12288.0,
|
|
|
226 |
179 => 14336.0,
|
|
|
227 |
180 => 16384.0,
|
|
|
228 |
181 => 20480.0,
|
|
|
229 |
182 => 24576.0,
|
|
|
230 |
183 => 28672.0,
|
|
|
231 |
184 => 32768.0,
|
|
|
232 |
185 => 40960.0,
|
|
|
233 |
186 => 49152.0,
|
|
|
234 |
187 => 57344.0,
|
|
|
235 |
188 => 65536.0,
|
|
|
236 |
189 => 81920.0,
|
|
|
237 |
190 => 98304.0,
|
|
|
238 |
191 => 114688.0,
|
|
|
239 |
192 => 131072.0,
|
|
|
240 |
193 => 163840.0,
|
|
|
241 |
194 => 196608.0,
|
|
|
242 |
195 => 229376.0,
|
|
|
243 |
196 => 262144.0,
|
|
|
244 |
197 => 327680.0,
|
|
|
245 |
198 => 393216.0,
|
|
|
246 |
199 => 458752.0,
|
|
|
247 |
200 => 524288.0,
|
|
|
248 |
201 => 655360.0,
|
|
|
249 |
202 => 786432.0,
|
|
|
250 |
203 => 917504.0,
|
|
|
251 |
204 => 1048576.0,
|
|
|
252 |
205 => 1310720.0,
|
|
|
253 |
206 => 1572864.0,
|
|
|
254 |
207 => 1835008.0,
|
|
|
255 |
208 => 2097152.0,
|
|
|
256 |
209 => 2621440.0,
|
|
|
257 |
210 => 3145728.0,
|
|
|
258 |
211 => 3670016.0,
|
|
|
259 |
212 => 4194304.0,
|
|
|
260 |
213 => 5242880.0,
|
|
|
261 |
214 => 6291456.0,
|
|
|
262 |
215 => 7340032.0,
|
|
|
263 |
216 => 8388608.0,
|
|
|
264 |
217 => 1.048576E7,
|
|
|
265 |
218 => 1.2582912E7,
|
|
|
266 |
219 => 1.4680064E7,
|
|
|
267 |
220 => 1.6777216E7,
|
|
|
268 |
221 => 2.097152E7,
|
|
|
269 |
222 => 2.5165824E7,
|
|
|
270 |
223 => 2.9360128E7,
|
|
|
271 |
224 => 3.3554432E7,
|
|
|
272 |
225 => 4.194304E7,
|
|
|
273 |
226 => 5.0331648E7,
|
|
|
274 |
227 => 5.8720256E7,
|
|
|
275 |
228 => 6.7108864E7,
|
|
|
276 |
229 => 8.388608E7,
|
|
|
277 |
230 => 1.00663296E8,
|
|
|
278 |
231 => 1.17440512E8,
|
|
|
279 |
232 => 1.34217728E8,
|
|
|
280 |
233 => 1.6777216E8,
|
|
|
281 |
234 => 2.01326592E8,
|
|
|
282 |
235 => 2.34881024E8,
|
|
|
283 |
236 => 2.68435456E8,
|
|
|
284 |
237 => 3.3554432E8,
|
|
|
285 |
238 => 4.02653184E8,
|
|
|
286 |
239 => 4.69762048E8,
|
|
|
287 |
240 => 5.3687091E8,
|
|
|
288 |
241 => 6.7108864E8,
|
|
|
289 |
242 => 8.0530637E8,
|
|
|
290 |
243 => 9.395241E8,
|
|
|
291 |
244 => 1.07374182E9,
|
|
|
292 |
245 => 1.34217728E9,
|
|
|
293 |
246 => 1.61061274E9,
|
|
|
294 |
247 => 1.87904819E9,
|
|
|
295 |
248 => 2.14748365E9,
|
|
|
296 |
249 => 2.68435456E9,
|
|
|
297 |
250 => 3.22122547E9,
|
|
|
298 |
251 => 3.75809638E9,
|
|
|
299 |
252 => 4.2949673E9,
|
|
|
300 |
253 => 5.3687091E9,
|
|
|
301 |
254 => 6.4424509E9,
|
|
|
302 |
255 => 7.5161928E9 );
|
|
|
303 |
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Set the default Similarity implementation used by indexing and search
|
|
|
307 |
* code.
|
|
|
308 |
*
|
|
|
309 |
* @param Zend_Search_Lucene_Search_Similarity $similarity
|
|
|
310 |
*/
|
|
|
311 |
static public function setDefault(Zend_Search_Lucene_Search_Similarity $similarity)
|
|
|
312 |
{
|
|
|
313 |
self::$_defaultImpl = $similarity;
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
|
|
|
317 |
/**
|
|
|
318 |
* Return the default Similarity implementation used by indexing and search
|
|
|
319 |
* code.
|
|
|
320 |
*
|
|
|
321 |
* @return Zend_Search_Lucene_Search_Similarity
|
|
|
322 |
*/
|
|
|
323 |
static public function getDefault()
|
|
|
324 |
{
|
|
|
325 |
if (!self::$_defaultImpl instanceof Zend_Search_Lucene_Search_Similarity) {
|
|
|
326 |
self::$_defaultImpl = new Zend_Search_Lucene_Search_Similarity_Default();
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
return self::$_defaultImpl;
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
/**
|
|
|
334 |
* Computes the normalization value for a field given the total number of
|
|
|
335 |
* terms contained in a field. These values, together with field boosts, are
|
|
|
336 |
* stored in an index and multipled into scores for hits on each field by the
|
|
|
337 |
* search code.
|
|
|
338 |
*
|
|
|
339 |
* Matches in longer fields are less precise, so implemenations of this
|
|
|
340 |
* method usually return smaller values when 'numTokens' is large,
|
|
|
341 |
* and larger values when 'numTokens' is small.
|
|
|
342 |
*
|
|
|
343 |
* That these values are computed under
|
|
|
344 |
* IndexWriter::addDocument(Document) and stored then using
|
|
|
345 |
* encodeNorm(float). Thus they have limited precision, and documents
|
|
|
346 |
* must be re-indexed if this method is altered.
|
|
|
347 |
*
|
|
|
348 |
* fieldName - name of field
|
|
|
349 |
* numTokens - the total number of tokens contained in fields named
|
|
|
350 |
* 'fieldName' of 'doc'.
|
|
|
351 |
* Returns a normalization factor for hits on this field of this document
|
|
|
352 |
*
|
|
|
353 |
* @param string $fieldName
|
|
|
354 |
* @param integer $numTokens
|
|
|
355 |
* @return float
|
|
|
356 |
*/
|
|
|
357 |
abstract public function lengthNorm($fieldName, $numTokens);
|
|
|
358 |
|
|
|
359 |
/**
|
|
|
360 |
* Computes the normalization value for a query given the sum of the squared
|
|
|
361 |
* weights of each of the query terms. This value is then multipled into the
|
|
|
362 |
* weight of each query term.
|
|
|
363 |
*
|
|
|
364 |
* This does not affect ranking, but rather just attempts to make scores
|
|
|
365 |
* from different queries comparable.
|
|
|
366 |
*
|
|
|
367 |
* sumOfSquaredWeights - the sum of the squares of query term weights
|
|
|
368 |
* Returns a normalization factor for query weights
|
|
|
369 |
*
|
|
|
370 |
* @param float $sumOfSquaredWeights
|
|
|
371 |
* @return float
|
|
|
372 |
*/
|
|
|
373 |
abstract public function queryNorm($sumOfSquaredWeights);
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
/**
|
|
|
377 |
* Decodes a normalization factor stored in an index.
|
|
|
378 |
*
|
|
|
379 |
* @param integer $byte
|
|
|
380 |
* @return float
|
|
|
381 |
*/
|
|
|
382 |
static public function decodeNorm($byte)
|
|
|
383 |
{
|
|
|
384 |
return self::$_normTable[$byte & 0xFF];
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
/**
|
|
|
389 |
* Encodes a normalization factor for storage in an index.
|
|
|
390 |
*
|
|
|
391 |
* The encoding uses a five-bit exponent and three-bit mantissa, thus
|
|
|
392 |
* representing values from around 7x10^9 to 2x10^-9 with about one
|
|
|
393 |
* significant decimal digit of accuracy. Zero is also represented.
|
|
|
394 |
* Negative numbers are rounded up to zero. Values too large to represent
|
|
|
395 |
* are rounded down to the largest representable value. Positive values too
|
|
|
396 |
* small to represent are rounded up to the smallest positive representable
|
|
|
397 |
* value.
|
|
|
398 |
*
|
|
|
399 |
* @param float $f
|
|
|
400 |
* @return integer
|
|
|
401 |
*/
|
|
|
402 |
static function encodeNorm($f)
|
|
|
403 |
{
|
|
|
404 |
return self::_floatToByte($f);
|
|
|
405 |
}
|
|
|
406 |
|
|
|
407 |
/**
|
|
|
408 |
* Float to byte conversion
|
|
|
409 |
*
|
|
|
410 |
* @param integer $b
|
|
|
411 |
* @return float
|
|
|
412 |
*/
|
|
|
413 |
static private function _floatToByte($f)
|
|
|
414 |
{
|
|
|
415 |
// round negatives up to zero
|
|
|
416 |
if ($f <= 0.0) {
|
|
|
417 |
return 0;
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
// search for appropriate value
|
|
|
421 |
$lowIndex = 0;
|
|
|
422 |
$highIndex = 255;
|
|
|
423 |
while ($highIndex >= $lowIndex) {
|
|
|
424 |
// $mid = ($highIndex - $lowIndex)/2;
|
|
|
425 |
$mid = ($highIndex + $lowIndex) >> 1;
|
|
|
426 |
$delta = $f - self::$_normTable[$mid];
|
|
|
427 |
|
|
|
428 |
if ($delta < 0) {
|
|
|
429 |
$highIndex = $mid-1;
|
|
|
430 |
} elseif ($delta > 0) {
|
|
|
431 |
$lowIndex = $mid+1;
|
|
|
432 |
} else {
|
|
|
433 |
return $mid; // We got it!
|
|
|
434 |
}
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
// round to closest value
|
|
|
438 |
if ($highIndex != 255 &&
|
|
|
439 |
$f - self::$_normTable[$highIndex] > self::$_normTable[$highIndex+1] - $f ) {
|
|
|
440 |
return $highIndex + 1;
|
|
|
441 |
} else {
|
|
|
442 |
return $highIndex;
|
|
|
443 |
}
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
|
|
|
447 |
/**
|
|
|
448 |
* Computes a score factor based on a term or phrase's frequency in a
|
|
|
449 |
* document. This value is multiplied by the idf(Term, Searcher)
|
|
|
450 |
* factor for each term in the query and these products are then summed to
|
|
|
451 |
* form the initial score for a document.
|
|
|
452 |
*
|
|
|
453 |
* Terms and phrases repeated in a document indicate the topic of the
|
|
|
454 |
* document, so implementations of this method usually return larger values
|
|
|
455 |
* when 'freq' is large, and smaller values when 'freq'
|
|
|
456 |
* is small.
|
|
|
457 |
*
|
|
|
458 |
* freq - the frequency of a term within a document
|
|
|
459 |
* Returns a score factor based on a term's within-document frequency
|
|
|
460 |
*
|
|
|
461 |
* @param float $freq
|
|
|
462 |
* @return float
|
|
|
463 |
*/
|
|
|
464 |
abstract public function tf($freq);
|
|
|
465 |
|
|
|
466 |
/**
|
|
|
467 |
* Computes the amount of a sloppy phrase match, based on an edit distance.
|
|
|
468 |
* This value is summed for each sloppy phrase match in a document to form
|
|
|
469 |
* the frequency that is passed to tf(float).
|
|
|
470 |
*
|
|
|
471 |
* A phrase match with a small edit distance to a document passage more
|
|
|
472 |
* closely matches the document, so implementations of this method usually
|
|
|
473 |
* return larger values when the edit distance is small and smaller values
|
|
|
474 |
* when it is large.
|
|
|
475 |
*
|
|
|
476 |
* distance - the edit distance of this sloppy phrase match
|
|
|
477 |
* Returns the frequency increment for this match
|
|
|
478 |
*
|
|
|
479 |
* @param integer $distance
|
|
|
480 |
* @return float
|
|
|
481 |
*/
|
|
|
482 |
abstract public function sloppyFreq($distance);
|
|
|
483 |
|
|
|
484 |
|
|
|
485 |
/**
|
|
|
486 |
* Computes a score factor for a simple term or a phrase.
|
|
|
487 |
*
|
|
|
488 |
* The default implementation is:
|
|
|
489 |
* return idfFreq(searcher.docFreq(term), searcher.maxDoc());
|
|
|
490 |
*
|
|
|
491 |
* input - the term in question or array of terms
|
|
|
492 |
* reader - reader the document collection being searched
|
|
|
493 |
* Returns a score factor for the term
|
|
|
494 |
*
|
|
|
495 |
* @param mixed $input
|
|
|
496 |
* @param Zend_Search_Lucene $reader
|
|
|
497 |
* @return a score factor for the term
|
|
|
498 |
*/
|
|
|
499 |
public function idf($input, $reader)
|
|
|
500 |
{
|
|
|
501 |
if (!is_array($input)) {
|
|
|
502 |
return $this->idfFreq($reader->docFreq($input), $reader->count());
|
|
|
503 |
} else {
|
|
|
504 |
$idf = 0.0;
|
|
|
505 |
foreach ($input as $term) {
|
|
|
506 |
$idf += $this->idfFreq($reader->docFreq($term), $reader->count());
|
|
|
507 |
}
|
|
|
508 |
return $idf;
|
|
|
509 |
}
|
|
|
510 |
}
|
|
|
511 |
|
|
|
512 |
/**
|
|
|
513 |
* Computes a score factor based on a term's document frequency (the number
|
|
|
514 |
* of documents which contain the term). This value is multiplied by the
|
|
|
515 |
* tf(int) factor for each term in the query and these products are
|
|
|
516 |
* then summed to form the initial score for a document.
|
|
|
517 |
*
|
|
|
518 |
* Terms that occur in fewer documents are better indicators of topic, so
|
|
|
519 |
* implemenations of this method usually return larger values for rare terms,
|
|
|
520 |
* and smaller values for common terms.
|
|
|
521 |
*
|
|
|
522 |
* docFreq - the number of documents which contain the term
|
|
|
523 |
* numDocs - the total number of documents in the collection
|
|
|
524 |
* Returns a score factor based on the term's document frequency
|
|
|
525 |
*
|
|
|
526 |
* @param integer $docFreq
|
|
|
527 |
* @param integer $numDocs
|
|
|
528 |
* @return float
|
|
|
529 |
*/
|
|
|
530 |
abstract public function idfFreq($docFreq, $numDocs);
|
|
|
531 |
|
|
|
532 |
/**
|
|
|
533 |
* Computes a score factor based on the fraction of all query terms that a
|
|
|
534 |
* document contains. This value is multiplied into scores.
|
|
|
535 |
*
|
|
|
536 |
* The presence of a large portion of the query terms indicates a better
|
|
|
537 |
* match with the query, so implemenations of this method usually return
|
|
|
538 |
* larger values when the ratio between these parameters is large and smaller
|
|
|
539 |
* values when the ratio between them is small.
|
|
|
540 |
*
|
|
|
541 |
* overlap - the number of query terms matched in the document
|
|
|
542 |
* maxOverlap - the total number of terms in the query
|
|
|
543 |
* Returns a score factor based on term overlap with the query
|
|
|
544 |
*
|
|
|
545 |
* @param integer $overlap
|
|
|
546 |
* @param integer $maxOverlap
|
|
|
547 |
* @return float
|
|
|
548 |
*/
|
|
|
549 |
abstract public function coord($overlap, $maxOverlap);
|
|
|
550 |
}
|
|
|
551 |
|