~hexmode/+junk/main

« back to all changes in this revision

Viewing changes to install-files/apps/sqlitemanager1.2.0/include/sqlite_fulltextsearch.class.php

  • Committer: Mark A. Hershberger
  • Date: 2008-01-05 19:38:56 UTC
  • Revision ID: hershberger@spawn-xp-20080105193856-6rnzgwa4nehue3qj
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php 
 
2
 
 
3
class sqlite_fulltextsearch {
 
4
 
 
5
        var $_againsts_cache = array ();
 
6
        
 
7
        var $use_against_cache = false;
 
8
        var $word_separators_pattern = "/[\s\.,'\"\/\|]+/";
 
9
        
 
10
        function sqlite_fulltextsearch () {
 
11
                $this->_againsts_cache = array ();
 
12
                $this->use_against_cache = false;
 
13
        }
 
14
 
 
15
        function wordspreparation (&$string, &$against) {
 
16
                // do nothing here, override in subclasses, see sqlite_fulltextsearchex example
 
17
        }
 
18
        
 
19
        function _wordspreparation ($string, $against, $usecase, &$string_words, &$against_words, &$string_words_count, &$against_words_count) {
 
20
                if (!$usecase) {
 
21
                        $string  = strtolower ($string);
 
22
                        $against = strtolower ($against);
 
23
                }       
 
24
                
 
25
                $this->wordspreparation ($string, $against);
 
26
                
 
27
                $string_words  = preg_split ("/ +/", preg_replace ($this->word_separators_pattern, ' ', $string));
 
28
                $against_words = null;
 
29
 
 
30
                if ($this->use_against_cache && isset ($this->_againsts_cache[$against])) {
 
31
                        $against_words = $this->_againsts_cache[$against];      
 
32
                } else {
 
33
                        $against_words = preg_split ("/ +/", preg_replace ($this->word_separators_pattern, ' ', $against));
 
34
                        if ($this->use_against_cache) {
 
35
                                $this->_againsts_cache[$against] = $against_words;
 
36
                        }
 
37
                }
 
38
                
 
39
                $string_words_count  = count ($string_words);
 
40
                $against_words_count = count ($against_words);
 
41
                
 
42
                return true;    
 
43
        }
 
44
 
 
45
        function prominence ($position, $string_words_count, $against_words_count) {
 
46
                /* lenear prominence */
 
47
                return $position;
 
48
        }
 
49
 
 
50
        function _internal_wordprominence ($string_words, $against_words, $string_words_count, $against_words_count) {
 
51
 
 
52
                if ($string_words_count == 0) {
 
53
                        return 0;
 
54
                }
 
55
                                
 
56
                $global_prominance = 0;
 
57
                foreach ($against_words as $against_word) {
 
58
                        foreach ($string_words as $position => $string_word) {
 
59
                                $position++;
 
60
                                if ($string_word == $against_word) {
 
61
                                        $global_prominance += $this->prominence ($position, $string_words_count, $against_words_count);
 
62
                                }
 
63
                        }
 
64
                }
 
65
                
 
66
                $divisor = ($string_words_count + 1) * ($string_words_count / 2); // sum of N integer numbers from 1 to N
 
67
                
 
68
                $result = $global_prominance / $divisor;                
 
69
                
 
70
                return $result;                 
 
71
        }
 
72
 
 
73
        function _wordprominence ($string, $against, $usecase) {
 
74
                
 
75
                $this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
 
76
                
 
77
                $string_words = array_reverse ($string_words);
 
78
 
 
79
                $result = $this->_internal_wordprominence ($string_words, $against_words, $string_words_count, $against_words_count);
 
80
                
 
81
                return $result;
 
82
        }
 
83
 
 
84
        function _reversewordprominence ($string, $against, $usecase) {
 
85
 
 
86
                $this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
 
87
                
 
88
                $result = $this->_internal_wordprominence ($string_words, $against_words, $string_words_count, $against_words_count);
 
89
                
 
90
                return $result;
 
91
        }
 
92
        
 
93
        function _centerwordprominence ($string, $against, $usecase) {
 
94
                
 
95
                $this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
 
96
                
 
97
                /* begin centering keywords, from 'a, b, c, d, e' to 'c, d, b, e, a' */
 
98
                $start = ceil ($string_words_count / 2) - 1;
 
99
                $left = $start;
 
100
                $right = $start;
 
101
                $centered_string_words = array ();
 
102
                $centered_string_words[] = $string_words[$start];
 
103
                while (($left > 0) && ($right < $string_words_count)) {
 
104
                        $left--;
 
105
                        $right++;
 
106
                        $centered_string_words[] = $string_words[$right];
 
107
                        $centered_string_words[] = $string_words[$left];
 
108
                }
 
109
                
 
110
                if ($right < $string_words_count - 1) {
 
111
                        $centered_string_words[] = $string_words[$string_words_count - 1];
 
112
                }
 
113
                /* end centering keywords */
 
114
 
 
115
                $centered_string_words = array_reverse ($centered_string_words);
 
116
 
 
117
                $result = $this->_internal_wordprominence ($centered_string_words, $against_words, $string_words_count, $against_words_count);
 
118
                
 
119
                return $result;
 
120
        }
 
121
        
 
122
        function _fulltextsearch ($string, $against, $usecase) {
 
123
                
 
124
                $result = 0;
 
125
        
 
126
                $this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
 
127
                
 
128
                if ($string_words_count == 0) {
 
129
                        return $result;
 
130
                }
 
131
                                
 
132
                $string = ' ' . implode (' ', $string_words) . ' ';
 
133
                $against_ex = ' ';
 
134
                for ($from = 0; $from < $against_words_count; $from++) {
 
135
                        $against_ex .= $against_words[$from] . ' ';
 
136
                        $result += (substr_count ($string, $against_ex) * ($from + 1)) / $string_words_count; 
 
137
                }       
 
138
        
 
139
                return $result;
 
140
        }
 
141
        
 
142
        function register (&$dbhandle) {
 
143
                if(is_object($dbhandle)) {
 
144
                        $dbhandle->create_function ('fulltextsearch',        array (&$this, '_fulltextsearch'),        3);      
 
145
                        $dbhandle->create_function ('wordprominence',        array (&$this, '_wordprominence'),        3);      
 
146
                        $dbhandle->create_function ('reversewordprominence', array (&$this, '_reversewordprominence'), 3);      
 
147
                        $dbhandle->create_function ('centerwordprominence',  array (&$this, '_centerwordprominence'),  3);      
 
148
                }
 
149
        }
 
150
 
 
151
}
 
152
 
 
153
?>
 
 
b'\\ No newline at end of file'