Exemplo n.º 1
0
 public function testNormalize()
 {
     $this->assertEquals('the knicks', Str::normalize('the.- knicks'));
     $this->assertEquals('', Str::normalize(',\'`~!@#$%^&*()_-{}\'"?/|\\'));
     $this->assertEquals('测试中文', Str::normalize('测试中文'));
     $this->assertEquals('测试中文', Str::normalize('"·||..?¿测试中文?¿!!!'));
 }
Exemplo n.º 2
0
 /**
  * Matches the given term against the indexed data in the Redis database.
  *
  * @param   string  $term
  * @param   array   $options
  * @return  array
  */
 public function matches($term, $options = array())
 {
     $words = array_filter(explode(' ', Str::normalize($term)), function ($w) {
         return Str::size($w) >= $this->getMinComplete() && !in_array($w, $this->getStopWords());
     });
     sort($words);
     if (empty($words)) {
         return [];
     }
     $limit = isset($options['limit']) ? $options['limit'] : 5;
     $cache = isset($options['cache']) ? $options['cache'] : true;
     $cacheExpiry = isset($options['expiry']) ? $options['expiry'] : 600;
     $cacheKey = $this->getCachePrefix() . ':' . implode('|', $words);
     if (!$cache || !$this->redis()->exists($cacheKey) || $this->redis()->exists($cacheKey) == 0) {
         $interKeys = array_map(function ($w) {
             return "{$this->getIndexPrefix()}:{$w}";
         }, $words);
         $this->redis()->zinterstore($cacheKey, $interKeys);
         $this->redis()->expire($cacheKey, $cacheExpiry);
     }
     $ids = $this->redis()->zrevrange($cacheKey, 0, $limit - 1);
     if (count($ids) === 0) {
         return [];
     }
     $results = $this->redis()->hmget($this->getDataPrefix(), $ids);
     $results = array_filter($results, function ($res) {
         return !is_null($res);
     });
     return array_map(function ($res) {
         return json_decode($res, true);
     }, $results);
 }
 protected function getResults($types, $term, $options = array())
 {
     if (!is_array($types)) {
         $types = array($types);
     }
     $types = array_map(function ($t) {
         return StrUtil::normalize($t);
     }, $types);
     $results = array();
     foreach ($types as $type) {
         $results[$type] = $this->manager->matches($type, $term, $options);
     }
     return $results;
 }
Exemplo n.º 4
0
 /**
  * Set the stop words array.
  *
  * @param   array $words
  * @return  void
  */
 public function setStopWords(array $words)
 {
     $this->stopWords = Ary::flatten(array_map(function ($w) {
         return Str::normalize($w);
     }, $words));
 }