湘里妹子学术网

 找回密码
 注册
查看: 2964|回复: 1

全文檢索準確度與關鍵字句高亮(Highlight)效果(原始碼)

[复制链接]
发表于 2004-10-25 12:43:42 | 显示全部楼层 |阅读模式
全文檢索準確度與關鍵字句高亮(Highlight)效果(繁簡通用),已經成功。原始程式資料如下↓

hilight_wizard.php -精確關鍵字標色[適用於/gb2312/big5/英美語系/unicode環境]
/*#########################################
hilight_wizard.php
** Intensive_Hilight functions Via 水電工**
精確關鍵字標色:適用於使用gb2312、big5、英美語系、unicode 之搜尋系統
所有的語系、甚至其他論壇都能用
http://artvine.com.tw
使用方法:
require_once('hilight_wizard.php');
$srchtxt = hilight_wizard($terms,$srchtxt);
BUGS & TODO:
超連結還會落入關鍵字標色範圍,待改善
################################*/


function hilight_wizard($terms , $srchtxt){
        if(got_num($terms) && is_UniCode($srchtxt))
                $srchtxt=unicode_hilight($terms , $srchtxt);
        else
                $srchtxt = highlight($terms,$srchtxt);

return $srchtxt;
}

function unicode_hilight($terms , $srchtxt){
        
        $arr= arr_terms($terms);

        $av=$arr[0];
        $ad=$arr[1];

        if(is_array($av))foreach( $av AS $v )$av2.= " ".$v;
        if(is_array($ad))foreach( $ad AS $v )$ad2.= " ".$v;
        
        $v_ret=trim($av2);
        $d_ret=trim($ad2);

                if($v_ret):

                        preg_match_all("/(&#)([1-3])([0-9]{4})([;])/",  $srchtxt, $uni);
                        $matches=array_unique($uni[0]);
                        $list_arr=uni_fake($matches,$srchtxt);
               
                        $fake_uni_str=strtr($srchtxt, $list_arr[0]);
                        $highlight_str=hilight_uni($v_ret, $fake_uni_str);
                        $srchtxt=strtr($highlight_str, $list_arr[1]);
                        
                endif;
        
                if($d_ret) $srchtxt= hilight_uni($d_ret, $srchtxt);

        return $srchtxt;
}

function is_chinese($str){
if(preg_match_all("/([\xa1-\xf9][\x40-\xfe])/",$str,$chi)>0)
        return true;
}      

function got_num($terms){
        $text=chop_terms($terms);        
        foreach(explode(' ', $text) as $ret){
                        if(is_numeric($ret)){
                                $num= true;
                                break;
                        }
                }               
        return $num;
}


function is_UniCode($str){
        if(preg_match_all("/([&#][1-3][0-9]{4}[;])/",$str,$uni)>0)
        return true;
}

function single_space($text) {
        $text=ereg_replace(' +', ' ', trim($text));
        return $text;
}

function chop_terms($text) {
        
        $search_terms=array(":",",",",",":","<",">","?","/","=","+","-"," the "," and "," an "," in "," on ", " or ");
        $text=str_replace($search_terms," ",$text);
        $text=single_space($text);
        
        return $text;
}

function arr_terms($text) {
        
        $text=chop_terms($text);
        $arr = explode(" ", $text);
        foreach($arr as $v){        
                if(is_numeric($v))$av[]=$v;
                else $ad[]=chr(10+strlen($v)).':'.$v;
                }

        if(is_array($av)){
                rsort($av);
                reset($av);
                }
        
        if(is_array($ad)){
                rsort($ad);
                reset($ad);
                }
        
        for ($i=0; $i                 list($n,$p) = explode(':',$ad[$i]);
                $newad[]= $p;
                }
        
        return array($av,$newad);
}

function sort_terms($terms) {
        $arr=arr_terms(stripslashes(stripslashes(stripslashes($terms))));
        $new = array_merge($arr[0] ,$arr[1]);
        
        for ($i=0; $i                 $new_terms.= $new[$i].'+';
        }
        
        return $new_terms;
}


function highlight($terms,$srchtxt,$fcolor='yellow'){
        $terms=sort_terms($terms);
        foreach(explode('+', $terms) as $ret){
                if((is_chinese($ret)==false && strlen($ret)>3) || is_chinese($ret))
                $srchtxt = preg_replace('|('.quotemeta($ret).')|iU',
                                    '\\1',$srchtxt);
        }
        
return $srchtxt;
}

function hilight_uni($terms,$srchtxt,$fcolor='yellow')
{
        $terms=stripslashes(stripslashes(stripslashes($terms)));
                        foreach(explode(' ', $terms) as $ret){
                        $srchtxt = preg_replace('|('.quotemeta($ret).')|iU',
                                    '\\1',$srchtxt);
                        }
return $srchtxt;
}

function uni_fake($matches,$str){
        
        $fake = array(0=>'z',1=>'y',2=>'x',3=>'w',4=>'v',5=>'u',6=>'t',7=>'s',8=>'r',9=>'q');

        foreach($matches as $data){
                $temp = strtr($data, $fake);
                $str1[$data]=$temp;
                $str2[$temp]=$data;
        }        
        return array($str1,$str2);
}

?>

/*#########################################
以上存為hilight_wizard.php
#########################################
hilight_wizard.php 在Discuz!的安裝及配合修正(2.0~3.12 plus)
###################################################
1.把 hilight_wizard.php 放在資料夾 include/底下

2.打開 include/discuzcode.php

在169行附近,找到  if ($highlight){

(原先的代碼程序可以通通刪除)把他改成:

        if ($highlight){
                 require_once('hilight_wizard.php');
                 $message = hilight_wizard($highlight,$message);
                }

/*底下就是原代碼程序
        if($highlight) {
                foreach(explode('+', $highlight) as $ret) {
                        $ret = addcslashes($ret, "/()[]|.:!=<>?^\$");
                        if($ret) {
                                $message = preg_replace("/([ \f\r\t\n]|^)$ret([ \f\r\t\n]|$)/is", "\\1$ret\\2", $message);
                        }
                }
        }
*/
/*#########################################
Discuz!精確搜尋search.php修正加強版
##############################################################################
#Discuz! 精確搜尋
#水電工   http://artvine.com.tw                                                                            #
#      
##############################################################################

##修正對象:search.php
///////////////////////////////////////////////////////////////////////////////////////////

##在62行附近找到        $srchtxt = trim($srchtxt);
##=>>把他改為
        $srchtxt=ereg_replace(' +', ' ', trim($srchtxt));
        $srchtxt=str_replace(' | ',' OR ', $srchtxt);
        $srchtxt=mysql_escape_string($srchtxt);


//////////////////////////////////////////////////////////
##在98行附近找到        if(preg_match("(AND|\+|&|\s)", $srchtxt) && !preg_match("(OR|\|)", $srchtxt)) {
        

##=>在下一行插入
        $srchtxt=preg_replace ("/(&#)([1-3])([0-9]{4})([;])/", "uNc\\2\\3\\4", $srchtxt);

////////////////////////////////////////////////////////////
##在108行附近找到        $srchtxt = str_replace('*', '%', $srchtxt);

##=>在下一行插入        
        $srchtxt=preg_replace ("/(uNc)([1-3])([0-9]{4})([;])/", "&#\\2\\3\\4", $srchtxt);
/////////////OK search.php fixed//////the end/////////
 楼主| 发表于 2004-10-29 01:52:46 | 显示全部楼层
<?php
/**
* This file contains the keyhighlighter class that highlight the chosen keyword in the current output buffer.
*
* @package keyhighlighter
*/

/**
* keyhighlighter class
*
* This class highlight the chosen keywords in the current output buffer
*
* @package keyhighlighter
* @author Setec Astronomy
* @version 1.0
* @abstract Highlight specific keywords.
* @copyright 2004
* @example sample.php A sample code.
* @link http://setecastronomy.stufftoread.com
*/

class keyhighlighter {
   
    /**
    * @access private
    */
    var $preg_keywords = '';
    /**
    * @access private
    */
    var $keywords = '';
    /**
    * @access private
    */
    var $singlewords = false;
    /**
    * @access private
    */
    var $replace_callback = null;

    /**
    * Main constructor
    *
    * This is the main constructor of keyhighlighter class. <br />
    * It's the only public method of the class.
    * @param string $keywords the keywords you want to highlight
    * @param boolean $singlewords specify if it has to highlight also the single words.
    * @param callback $replace_callback a custom callback for keyword highlight.
    * <code>   
    * <?php
    * require ('keyhighlighter.class.php');
    *
    * function my_highlighter ($matches) {
    *     return '<span style="font-weight: bolder; color: #FF0000;">' . $matches[0] . '</span>';
    * }
    *     
    * new keyhighlighter ('W3C', false, 'my_highlighter');
    * readfile ('http://www.w3c.org/');
    * ?>
    * </code>
    */
    // public function __construct ()
    function keyhighlighter ($keywords, $singlewords = false, $replace_callback = null) {
        $this->keywords = $keywords;
        $this->singlewords = $singlewords;
        $this->replace_callback = $replace_callback;
        ob_start (array (&$this, 'highlight'));
    }
   
    /**
    * @access private
    */
    function replace ($replace_matches) {
        
        $patterns = array ();
        if ($this->singlewords) {
              $keywords = explode (' ', $this->preg_keywords);
              foreach ($keywords as $keyword) {
                  $patterns[] = '/(?' . '>' . $keyword . '+)/si';
              }
        } else {
              $patterns[] = '/(?' . '>' . $this->preg_keywords . '+)/si';
        }

        $result = $replace_matches[0];

        foreach ($patterns as $pattern) {
              if (!is_null ($this->replace_callback)) {
                  $result = preg_replace_callback ($pattern, $this->replace_callback, $result);
              } else {
                  $result = preg_replace ($pattern, '<span class="highlightedkey">\\0</span>', $result);
              }
        }

        return $result;
    }
   
    /**
    * @access private
    */
    function highlight ($buffer) {
        $buffer = '>' . $buffer . '<';
        $this->preg_keywords = preg_replace ('/[^\w ]/si', '', $this->keywords);
        $buffer = preg_replace_callback ("/(\>(((?" . ">[^><]+)|(?R))*)\<)/is", array (&$this, 'replace'), $buffer);
        $buffer = substr ($buffer, 1, -1);
        return $buffer;
    }
}

?>
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|湘里妹子学术网 ( 粤ICP备2022147245号 )

GMT++8, 2024-5-17 21:58 , Processed in 0.136972 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表