import
[web.mtrack] / inc / lib / Zend / Search / Lucene / Search / Similarity / Default.php
1 <?php
2 /**
3  * Zend Framework
4  *
5  * LICENSE
6  *
7  * This source file is subject to the new BSD license that is bundled
8  * with this package in the file LICENSE.txt.
9  * It is also available through the world-wide-web at this URL:
10  * http://framework.zend.com/license/new-bsd
11  * If you did not receive a copy of the license and are unable to
12  * obtain it through the world-wide-web, please send an email
13  * to license@zend.com so we can send you a copy immediately.
14  *
15  * @category   Zend
16  * @package    Zend_Search_Lucene
17  * @subpackage Search
18  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
20  * @version    $Id: Default.php 16541 2009-07-07 06:59:03Z bkarwin $
21  */
22
23
24 /** Zend_Search_Lucene_Search_Similarity */
25 require_once 'Zend/Search/Lucene/Search/Similarity.php';
26
27
28 /**
29  * @category   Zend
30  * @package    Zend_Search_Lucene
31  * @subpackage Search
32  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
34  */
35 class Zend_Search_Lucene_Search_Similarity_Default extends Zend_Search_Lucene_Search_Similarity
36 {
37
38     /**
39      * Implemented as '1/sqrt(numTerms)'.
40      *
41      * @param string $fieldName
42      * @param integer $numTerms
43      * @return float
44      */
45     public function lengthNorm($fieldName, $numTerms)
46     {
47         if ($numTerms == 0) {
48             return 1E10;
49         }
50
51         return 1.0/sqrt($numTerms);
52     }
53
54     /**
55      * Implemented as '1/sqrt(sumOfSquaredWeights)'.
56      *
57      * @param float $sumOfSquaredWeights
58      * @return float
59      */
60     public function queryNorm($sumOfSquaredWeights)
61     {
62         return 1.0/sqrt($sumOfSquaredWeights);
63     }
64
65     /**
66      * Implemented as 'sqrt(freq)'.
67      *
68      * @param float $freq
69      * @return float
70      */
71     public function tf($freq)
72     {
73         return sqrt($freq);
74     }
75
76     /**
77      * Implemented as '1/(distance + 1)'.
78      *
79      * @param integer $distance
80      * @return float
81      */
82     public function sloppyFreq($distance)
83     {
84         return 1.0/($distance + 1);
85     }
86
87     /**
88      * Implemented as 'log(numDocs/(docFreq+1)) + 1'.
89      *
90      * @param integer $docFreq
91      * @param integer $numDocs
92      * @return float
93      */
94     public function idfFreq($docFreq, $numDocs)
95     {
96         return log($numDocs/(float)($docFreq+1)) + 1.0;
97     }
98
99     /**
100      * Implemented as 'overlap/maxOverlap'.
101      *
102      * @param integer $overlap
103      * @param integer $maxOverlap
104      * @return float
105      */
106     public function coord($overlap, $maxOverlap)
107     {
108         return $overlap/(float)$maxOverlap;
109     }
110 }