Chinese word segmentation with SCWS API
http://www.xunsearch.com/scws/api.php
Get Word segmentation results with curl in 1.php
protected function Http_curl ($url, $type = "get", $res = "json", $arr =array ()) { $ch =curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_returntransfer,1); if ($type = = "Post") { curl_setopt ($ch, curlopt_post,1); curl_setopt ($ch, Curlopt_postfields, $arr); } curl_setopt ($ch, curlopt_http_version,curl_http_version_1_0); $output = curl_exec ($ch); Curl_close ($ch); if ($res = = "json") { return Json_decode ($output, True); } else{ return $output; } }
protected function Get_fenci ($str) { $url = "http://www.xunsearch.com/scws/api.php"; $type = "POST"; $arr =array ( ' data ' = $str, ' respond ' = ' json ' ); $res = $this->http_curl ($url, $type, "json", $arr); return $res; }
2. Loop the SQL where statement according to the participle
foreach ($fenci as $k = + $v) { if (empty ($where)) { $where. = "name like '%{$v}% '"; } else{ $where. = "OR name like '%{$v}% '"; } }
3. Loop the SQL Order statement according to the participle
With case, then ELSE END statement;
Sorting is also through the fuzzy query, matching the phrase, if the match to define a numeric value, these values add, the more words match, the greater the result of the added value, the greater the value of the former, the result is equal to the matching degree, and then in descending order;
protected function Fenci_order ($fenci) { $order _str; foreach ($fenci as $k + $v) { if (empty ($order _str)) { $order _str.= "(case is name like '%". $v. " % ' Then '. (1000-10* $k). " ELSE 0 END) "; } else{ $order _str.= "+ (case is name like '%". $v. " % ' then '. (1000-10* $k). " ELSE 0 END) "; } } $order _str= $order _str. " DESC "; echo count ($fenci); return $order _str; }
PHP mysql Segmentation fuzzy query and sorting according to the word segmentation matching degree