/** * 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); }
public function testPrefixesForPhrase() { $min = 2; $swords = ['the']; $this->assertEquals(['kn', 'kni', 'knic', 'knick', 'knicks'], Str::prefixesForPhrase('the knicks', $min, $swords)); $this->assertEquals(['te', 'tes', 'test', 'testi', 'testin', 'th', 'thi', 'this'], Str::prefixesForPhrase("testin' this", $min, $swords)); $this->assertEquals(['te', 'tes', 'test', 'testi', 'testin', 'th', 'thi', 'this'], Str::prefixesForPhrase("testin' this", $min, $swords)); $this->assertEquals(['te', 'tes', 'test'], Str::prefixesForPhrase('test test', $min, $swords)); $this->assertEquals(['so', 'sou', 'soul', 'soulm', 'soulma', 'soulmat', 'soulmate'], Str::prefixesForPhrase('SoUlmATE', $min, $swords)); $this->assertEquals(['测试', '测试中', '测试中文', 'te', 'tes', 'test'], Str::prefixesForPhrase('测试中文 test', $min, $swords)); $min = 4; $this->assertEquals(['同华东生', '同华东生产', '同华东生产队', 'abcd', 'abcde'], Str::prefixesForPhrase('同华东生产队 abcde', $min, $swords)); }
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; }
/** * 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)); }
/** * Computes all the word prefixes for a given item which satisfy the * min-complete requirement and are not in the stop-words array. * * @param array $item * @return array */ protected function prefixes($item) { return Str::prefixesForPhrase($this->itemPhrase($item), $this->getMinComplete(), $this->getStopWords()); }