fts : add supports to detect sentences, update word or sentence by external method, get updated query
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6105 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
81
fts.php
81
fts.php
@@ -12,6 +12,7 @@
|
|||||||
* Manage the sentences (enclosed in quotes), or the standalone words,
|
* Manage the sentences (enclosed in quotes), or the standalone words,
|
||||||
* Manage the non wanted field (beginning by -),
|
* Manage the non wanted field (beginning by -),
|
||||||
* Do not search if the word is smaller than a parameter.
|
* Do not search if the word is smaller than a parameter.
|
||||||
|
* Each sentence or word can be modified by external methods separately
|
||||||
*/
|
*/
|
||||||
class fts
|
class fts
|
||||||
{
|
{
|
||||||
@@ -35,6 +36,14 @@ class fts
|
|||||||
/** The regexes created by the parser
|
/** The regexes created by the parser
|
||||||
*/
|
*/
|
||||||
private $regexes = null;
|
private $regexes = null;
|
||||||
|
|
||||||
|
/** The callable method to run on each word of the query
|
||||||
|
*/
|
||||||
|
private $callTokenWord = null;
|
||||||
|
|
||||||
|
/** The callable method to run on each sentence of the query
|
||||||
|
*/
|
||||||
|
private $callTokenSentence = null;
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
@@ -81,10 +90,43 @@ class fts
|
|||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
|
/** Set the method to call on tokens word only
|
||||||
|
* The method must return the token updated
|
||||||
|
* @param callable $method The callable method
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function callTokenWord ($callable)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
if (! is_callable ($callable))
|
||||||
|
throw new \Exception (dgettext ("domframework",
|
||||||
|
"SSE : callTokenWord : provided method is not callable"), 500);
|
||||||
|
$this->callTokenWord = $callable;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/** Set the method to call on tokens sentence only
|
||||||
|
* The method must return the token updated
|
||||||
|
* @param callable $method The callable method
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function callTokenSentence ($callable)
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
if (! is_callable ($callable))
|
||||||
|
throw new \Exception (dgettext ("domframework",
|
||||||
|
"SSE : callTokenSentence : provided method is not callable"), 500);
|
||||||
|
$this->callTokenSentence = $callable;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
//////////////////////////////
|
//////////////////////////////
|
||||||
//// PUBLIC METHODS ////
|
//// PUBLIC METHODS ////
|
||||||
//////////////////////////////
|
//////////////////////////////
|
||||||
/** Search the text provided in $query in the database
|
/** Explode the query text provided in $query, to be used to search in
|
||||||
|
* database, file...
|
||||||
* @param string $query The text to found in the database
|
* @param string $query The text to found in the database
|
||||||
* @return array The operator and the associated regex value to search
|
* @return array The operator and the associated regex value to search
|
||||||
*/
|
*/
|
||||||
@@ -93,6 +135,14 @@ class fts
|
|||||||
{
|
{
|
||||||
$query = trim ($query);
|
$query = trim ($query);
|
||||||
$this->tokens = $this->tokenizer ($query);
|
$this->tokens = $this->tokenizer ($query);
|
||||||
|
foreach ($this->tokens["tokens"] as $key => &$token)
|
||||||
|
{
|
||||||
|
if ($this->tokens["sentences"][$key] === true && $this->callTokenSentence)
|
||||||
|
$token = call_user_func ($this->callTokenSentence, $token);
|
||||||
|
elseif ($this->tokens["sentences"][$key] === false &&
|
||||||
|
$this->callTokenWord)
|
||||||
|
$token = call_user_func ($this->callTokenWord, $token);
|
||||||
|
}
|
||||||
$this->tokensMin = $this->tokenMinLength ($this->tokens["tokens"],
|
$this->tokensMin = $this->tokenMinLength ($this->tokens["tokens"],
|
||||||
$this->tokens["minuses"]);
|
$this->tokens["minuses"]);
|
||||||
$this->regexes = $this->regex ($this->tokensMin["tokens"],
|
$this->regexes = $this->regex ($this->tokensMin["tokens"],
|
||||||
@@ -101,6 +151,28 @@ class fts
|
|||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
|
/** Construct the query based on the tokens.
|
||||||
|
* The tokens can be updated by methods so the query may be modified by the
|
||||||
|
* external methods
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getQuery ()
|
||||||
|
// {{{
|
||||||
|
{
|
||||||
|
$res = "";
|
||||||
|
foreach ($this->tokens["tokens"] as $key => $token)
|
||||||
|
{
|
||||||
|
if ($key > 0)
|
||||||
|
$res .= " ";
|
||||||
|
if ($this->tokens["sentences"][$key] === true)
|
||||||
|
$res .= "\"$token\"";
|
||||||
|
else
|
||||||
|
$res .= "$token";
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
|
||||||
/** Return $line if the $query match against $line, or false if not
|
/** Return $line if the $query match against $line, or false if not
|
||||||
* @param string $line The line to examine
|
* @param string $line The line to examine
|
||||||
* @param string $query The query to apply on it
|
* @param string $query The query to apply on it
|
||||||
@@ -252,6 +324,7 @@ class fts
|
|||||||
throw new \Exception ("Invalid query provided to fts:tokenizer", 500);
|
throw new \Exception ("Invalid query provided to fts:tokenizer", 500);
|
||||||
$debug = false;
|
$debug = false;
|
||||||
$tokens = array ();
|
$tokens = array ();
|
||||||
|
$sentences = array ();
|
||||||
$minuses = array ();
|
$minuses = array ();
|
||||||
// Look for sentences
|
// Look for sentences
|
||||||
$offset = 0;
|
$offset = 0;
|
||||||
@@ -281,6 +354,7 @@ class fts
|
|||||||
" with $nbchars chars)\n";
|
" with $nbchars chars)\n";
|
||||||
$token = substr ($query, $offset + 1, $nbchars);
|
$token = substr ($query, $offset + 1, $nbchars);
|
||||||
$tokens[] = $token;
|
$tokens[] = $token;
|
||||||
|
$sentences[] = true;
|
||||||
$minuses[] = $minus;
|
$minuses[] = $minus;
|
||||||
$offset = $end + 1;
|
$offset = $end + 1;
|
||||||
continue;
|
continue;
|
||||||
@@ -294,11 +368,14 @@ class fts
|
|||||||
if ($debug) echo "WORD FOUND (Start $offset with $nbchars chars)\n";
|
if ($debug) echo "WORD FOUND (Start $offset with $nbchars chars)\n";
|
||||||
$token = substr ($query, $offset, $nbchars);
|
$token = substr ($query, $offset, $nbchars);
|
||||||
$tokens[] = $token;
|
$tokens[] = $token;
|
||||||
|
$sentences[] = false;
|
||||||
$minuses[] = $minus;
|
$minuses[] = $minus;
|
||||||
$offset = $end + 1;
|
$offset = $end + 1;
|
||||||
}
|
}
|
||||||
if ($debug) print_r ($tokens);
|
if ($debug) print_r ($tokens);
|
||||||
return array ("tokens"=>$tokens, "minuses"=>$minuses);
|
return array ("tokens" => $tokens,
|
||||||
|
"sentences" => $sentences,
|
||||||
|
"minuses" => $minuses);
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user