Example #1
0
 /**
  *
  * @access public
  * @return void
  * @author majiechao
  *         @修改日期 2015-09-15 16:18:11
  */
 public function wordAction()
 {
     $text = $this->request->get('text', 'string', '');
     if (empty($text)) {
         exit('[]');
     }
     $mss = mss_create("confilter_main", -1);
     $timestamp = mss_timestamp($mss);
     $is_ready = mss_is_ready($mss);
     if ($is_ready) {
         // 如果redis dict最后更新时间大于mss实例创建时间,就重载
         $updateTime = $this->di['confilter']->get($this->dictUpdateKey);
         if ($updateTime > $timestamp) {
             mss_destroy($mss);
             $mss = mss_create("confilter_main", -1);
             $timestamp = mss_timestamp($mss);
             $is_ready = mss_is_ready($mss);
         }
     } else {
         $this->loadDict($mss, $this->dictKey);
     }
     $matches = mss_search($mss, $text);
     $word_arr = array();
     foreach ($matches as $key => $value) {
         $word_arr[] = $value[0];
     }
     $json = json_encode($word_arr);
     echo $json;
     exit;
 }
Example #2
0
 /**
  * 提取关键字
  * @return json
  * @author 彭东江
  **/
 public function word($text)
 {
     $redis = \Phalcon\DI::getDefault()->get("redis");
     if (!isset($text) || empty($text)) {
         exit;
     }
     //查询词语转换成大写
     $text = strtolower($text);
     $mss = mss_create("default", -1);
     $timestamp = mss_timestamp($mss);
     $is_ready = mss_is_ready($mss);
     if ($is_ready) {
         //如果redis dict最后更新时间大于mss实例创建时间,就重载
         $updateTime = $redis->get($this->dictUpdateKey);
         if ($updateTime > $timestamp) {
             mss_destroy($mss);
             $mss = mss_create("default");
             $timestamp = mss_timestamp($mss);
             $is_ready = mss_is_ready($mss);
         }
     }
     if (!$is_ready) {
         $this->loadDict($mss, $this->dictKey);
     }
     $mode = '/[^\\x{4e00}-\\x{9fa5}|a-z|\\d]/ui';
     //只保留中文英文数字
     $new_text = preg_replace($mode, '', $text);
     $matches = mss_search($mss, $new_text);
     $word_arr = array();
     foreach ($matches as $key => $value) {
         $word_arr[] = $value[0];
     }
     $json = json_encode($word_arr);
     return $json;
 }
Example #3
0
echo "mss_creation: " . date("Y-m-d H:i:s", $timestamp) . "\n";
//
//
echo "mms_match(): original text\n";
$ret = mss_match($mss, $text);
echo "    ", $ret ? "matched" : "not matched", "\n";
echo "\n";
//
//
echo "mms_search() array:\n";
$ret = mss_search($mss, $text);
echo "    ", "count: ", count($ret), "\n";
echo "\n";
//
//
echo "mms_search() callback: \n";
$count = 0;
$ret = mss_search($mss, $text, function ($kw, $idx, $type, $ext) {
    $ext[0]++;
    for ($i = strlen($kw) - 1; $i >= 0; $i--) {
        $ext[1][$idx + $i] = '*';
    }
}, array(&$count, &$text));
echo "    ", "count: ", $count, "\n";
echo "\n";
//
//
echo "mms_match(): modified text\n";
$ret = mss_match($mss, $text);
echo "    ", $ret ? "matched" : "not matched", "\n";
echo "\n";
Example #4
0
<?php

// header("content-type: text/plain; charset=utf-8");
$mss = mss_create();
mss_add($mss, "安居客", "公司名称");
mss_add($mss, "二手房", "通用名词");
$text = "安居客,是安居客集团旗下国内最大的专业二手房网站。自2007年上线至今的短短4年时间里,安居客凭借其“专业二手房搜索引擎”的核心竞争力在业内独树一帜。通过对用户需求的精准把握、海量的二手房房源、精准的搜索功能、强大的产品研发能力,为用户提供最佳找房体验。目前,安居客的足迹已经遍布全国超过29个城市,注册用户超过1000万。";
echo "Round: 1\n";
$matches = mss_search($mss, $text);
echo "[\n";
foreach ($matches as $match) {
    echo "  ({$match[0]}, {$match[2]}, {$match[1]})\n";
}
echo "]\n";
echo "\n";
echo "Round: 2\n";
echo "[\n";
mss_search($mss, $text, function ($kw, $idx, $type) {
    echo "  ({$kw}, {$idx}, {$type})\n";
});
echo "]\n";
echo "\n";
$matched = mss_match($mss, $text);
echo $matched ? "matched" : "not matched", "\n";