PHP mysql Segmentation fuzzy query and sorting according to the word segmentation matching degree

Source: Internet
Author: User
Tags php mysql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.