php實現 scws中文分詞的搜尋

來源:互聯網
上載者:User

1、4個檔案解壓後,放到一個地方   eg:E:/wamp/scws

2、php.ini 中配置
 
extension = php_scws.dll
scws.default.charset = utf8   //配置預設的編碼方式
scws.default.fpath = "E:/wamp/scws"      //加壓後檔案的路徑

3、使用

  $so = scws_new(); 
   $so->set_charset('utf8');  //編碼
   // 這裡沒有調用 set_dict 和 set_rule 系統會自動試調用 ini 中指定路徑下的詞典和規則檔案 
   $so->add_dict(ini_get('scws.default.fpath') . '/dict.utf8.xdb');
   //$so->add_dict('./dd.txt',SCWS_XDICT_TXT);
   $so->set_rule(ini_get('scws.default.fpath') . '/rules.utf8.ini'); 
   
  $so->set_duality(0);   //散字二元
  $so->set_ignore(0);  //忽略標點符號
  $so->set_multi(0); 
 
  $wd='要搜尋的內容';
  $so->send_text($wd);
  while($tmp = $so->get_result()) //遍曆後即得到字元
      串分割後的內容,根據得到的內容分別到資料庫中尋找(like匹配)
 {
   
 }

 親自實驗了一下如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<?php
function search($keywords,$table,$key1,$key2){
// (要尋找的字串,表名,欄位一,欄位二)如果就一個欄位的話,$key2='';
 $arr=array();
 $str=array();
 if( preg_match("/^[^\x80-\xff]+$/", $keywords)){//判斷keywords是否全為英文,全為英文的話按照字串切割處理
  $str1=explode(' ',$keywords);  //先按照空格切割
  foreach($str1 as $key=>$value)
  {
   $str2=explode(',',$value); //,切割
   foreach($str2 as $k=>$v)
   {
    $str3=explode('.',$v);  //。切割
    foreach($str3 as $kk=>$vv)
    {
     array_push($arr,$vv);//處理完後,返回一個數組
    }
   }
  }
 }else{//對中文的分詞處理
  $so = scws_new(); 
  $so->set_charset('utf8'); 
   // 這裡沒有調用 set_dict 和 set_rule 系統會自動試調用 ini 中指定路徑下的詞典和規則檔案 
     $so->add_dict(ini_get('scws.default.fpath') . '/dict.utf8.xdb');
     $so->set_rule(ini_get('scws.default.fpath') . '/rules.utf8.ini'); 
     $so->set_duality(0);   //散字二元
    $so->set_ignore(0);  //忽略標點符號
    $so->set_multi(0);
  $so->send_text($keywords);
  while ($tmp = $so->get_result())//得到一個數組,裡麵包含片語和標點符號
  {
   foreach($tmp as $key=>$value)
   {
    $value=$value["word"];
    if(preg_match("/^[一-龥]{6,21}$/", $value))//去除標點符號
    {
     array_push($arr,$value);
    }
   }
  }
 }
 foreach($arr as $key=>$value)//便利得到的數組,到資料庫中匹配
 {
  if($key2!='')
  {
   $sql="select * from `$table` where `$key1`  like '%$value%' or `$key2` like '%$value%'";

  }else{
   $sql="select * from `$table` where `$key1`  like '%$value%'";
  }
  $query=mysql_query($sql);
  array_push($str,$query);
 }
 return $str;
}

include('conn.php');
$keywords="哈哈lsd djk,hdjs dd";
$table='two_key';
$key1='address';
$key2='';
$query=search($keywords,$table,$key1,$key2);
foreach($query as $key=>$value)
{
 while($row=mysql_fetch_array($value))
 {
  echo $row[id].'----'.$row[name].'-----'.$row[address].'<br>';
 }
}
?>
</body>
</html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.