Убрал phptal и лишнее
This commit is contained in:
parent
651a841187
commit
96043d70e6
95 changed files with 4 additions and 10833 deletions
101
search/searcher.php
Normal file
101
search/searcher.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
require_once 'core/search/search.php';
|
||||
require_once 'core/search/htmlhelper.php';
|
||||
require_once 'core/search/stemmer.php';
|
||||
|
||||
class Searcher {
|
||||
/* protected */ public $index;
|
||||
protected $text;
|
||||
protected $search;
|
||||
public function __construct ()
|
||||
{
|
||||
// Может передавать обьект метод по умлочанию getWordStat??
|
||||
$this->search = new Search (array ($this, 'getWord'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Читает содержимое индексного файла
|
||||
*
|
||||
* @param string $file Имя файла
|
||||
*/
|
||||
function setSource ($fileName)
|
||||
{
|
||||
$file = fopen($fileName, "r");
|
||||
$words = fread($file, 4);
|
||||
$all = unpack("Swords/Stexts", $words);
|
||||
for ($i = 0; $i < $all['words']; $i++) {
|
||||
$pos = fread($file, 4);
|
||||
$size = unpack("Sword/Sindex", $pos);
|
||||
|
||||
$word = fread($file, $size['word']);
|
||||
$index = unpack("S*", fread($file, $size['index']*2));
|
||||
$this->index[$word] = $index;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $all['texts']; $i++) {
|
||||
$pos = fread($file, 6);
|
||||
$size = unpack("Stitle/Surl/Stext", $pos);
|
||||
//
|
||||
$title = fread($file, $size['title']);
|
||||
$url = fread($file, $size['url']);
|
||||
$text = fread($file, $size['text']);
|
||||
$this->text [] = array ($title, $url, $text);
|
||||
}
|
||||
}
|
||||
|
||||
// По слову возвращаем список файлов и слово
|
||||
public function getWord ($word)
|
||||
{
|
||||
$preword = Stemmer::russian($word); // Index?? -> clean
|
||||
if (isset($this->index[$preword])) { // Search??
|
||||
return array ('files' => $this->index[$preword], 'words' => array ($preword));
|
||||
}
|
||||
return array ('files' => array (), 'words' => array ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Список документов в которых встечается слово
|
||||
*
|
||||
*/
|
||||
function getResult (&$query)
|
||||
{
|
||||
$result = array ();
|
||||
$word = $query['words'];
|
||||
$list = $query['files'];
|
||||
//
|
||||
foreach ($list as $n) {
|
||||
$result [] = array (
|
||||
'title' => $this->text[$n][0],
|
||||
'file' => $this->text[$n][1],
|
||||
'text' => self::getSlice ($word[0], $this->text[$n][2]));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Часть документа в котором встречается слово
|
||||
*
|
||||
* @param $word Слово
|
||||
* @param $text Текст содержащий слово
|
||||
*/
|
||||
function getSlice ($word, $text)
|
||||
{
|
||||
$pos = stripos($text, $word);
|
||||
$offset = max(max ($pos-100, indexRight($text, array ("."), $pos) + 1), 0);
|
||||
$real = substr($text, $pos, strlen($word)) ;
|
||||
return substr($text, $offset, $pos - $offset)
|
||||
. "<span style='color: red'>" . $real . "</span>" . substr ($text, $pos + strlen($word), 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск по запросу
|
||||
*
|
||||
*/
|
||||
function search ($query)
|
||||
{
|
||||
$result = $this->search->getQuery($query);
|
||||
return $this->getResult($result);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue