3
class sqlite_fulltextsearch {
5
var $_againsts_cache = array ();
7
var $use_against_cache = false;
8
var $word_separators_pattern = "/[\s\.,'\"\/\|]+/";
10
function sqlite_fulltextsearch () {
11
$this->_againsts_cache = array ();
12
$this->use_against_cache = false;
15
function wordspreparation (&$string, &$against) {
16
// do nothing here, override in subclasses, see sqlite_fulltextsearchex example
19
function _wordspreparation ($string, $against, $usecase, &$string_words, &$against_words, &$string_words_count, &$against_words_count) {
21
$string = strtolower ($string);
22
$against = strtolower ($against);
25
$this->wordspreparation ($string, $against);
27
$string_words = preg_split ("/ +/", preg_replace ($this->word_separators_pattern, ' ', $string));
28
$against_words = null;
30
if ($this->use_against_cache && isset ($this->_againsts_cache[$against])) {
31
$against_words = $this->_againsts_cache[$against];
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;
39
$string_words_count = count ($string_words);
40
$against_words_count = count ($against_words);
45
function prominence ($position, $string_words_count, $against_words_count) {
46
/* lenear prominence */
50
function _internal_wordprominence ($string_words, $against_words, $string_words_count, $against_words_count) {
52
if ($string_words_count == 0) {
56
$global_prominance = 0;
57
foreach ($against_words as $against_word) {
58
foreach ($string_words as $position => $string_word) {
60
if ($string_word == $against_word) {
61
$global_prominance += $this->prominence ($position, $string_words_count, $against_words_count);
66
$divisor = ($string_words_count + 1) * ($string_words_count / 2); // sum of N integer numbers from 1 to N
68
$result = $global_prominance / $divisor;
73
function _wordprominence ($string, $against, $usecase) {
75
$this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
77
$string_words = array_reverse ($string_words);
79
$result = $this->_internal_wordprominence ($string_words, $against_words, $string_words_count, $against_words_count);
84
function _reversewordprominence ($string, $against, $usecase) {
86
$this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
88
$result = $this->_internal_wordprominence ($string_words, $against_words, $string_words_count, $against_words_count);
93
function _centerwordprominence ($string, $against, $usecase) {
95
$this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
97
/* begin centering keywords, from 'a, b, c, d, e' to 'c, d, b, e, a' */
98
$start = ceil ($string_words_count / 2) - 1;
101
$centered_string_words = array ();
102
$centered_string_words[] = $string_words[$start];
103
while (($left > 0) && ($right < $string_words_count)) {
106
$centered_string_words[] = $string_words[$right];
107
$centered_string_words[] = $string_words[$left];
110
if ($right < $string_words_count - 1) {
111
$centered_string_words[] = $string_words[$string_words_count - 1];
113
/* end centering keywords */
115
$centered_string_words = array_reverse ($centered_string_words);
117
$result = $this->_internal_wordprominence ($centered_string_words, $against_words, $string_words_count, $against_words_count);
122
function _fulltextsearch ($string, $against, $usecase) {
126
$this->_wordspreparation ($string, $against, $usecase, $string_words, $against_words, $string_words_count, $against_words_count);
128
if ($string_words_count == 0) {
132
$string = ' ' . implode (' ', $string_words) . ' ';
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;
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);
b'\\ No newline at end of file'