Skip to content

Instantly share code, notes, and snippets.

@co3k
Created September 21, 2009 14:46
Show Gist options
  • Save co3k/190271 to your computer and use it in GitHub Desktop.
Save co3k/190271 to your computer and use it in GitHub Desktop.
<?php
/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/
/**
* opNGramSearchTextAnalyzer
*
* @package OpenPNE
* @subpackage util
* @author Kousuke Ebihara <ebihara@tejimaya.com>
*/
class opNGramSearchTextAnalyzer extends Doctrine_Search_Analyzer_Standard
{
public function analyze($text)
{
$result = array();
$text = preg_replace('/[^\x21-\x7e]+/u', '', $text);
if (!$text)
{
return $result;
}
$length = Doctrine_Validator::getStringLength($text);
for ($i = 0; $i < $length; $i++)
{
$result[] = $this->substrNGram($text, $i, 2);
}
return array_unique($result);
}
protected function substrNGram($string, $offset, $length)
{
if (function_exists('iconv_substr'))
{
return iconv_substr($string, $offset, $length, 'UTF-8');
}
elseif (function_exists('mb_substr'))
{
return mb_substr($string, $offset, $length, 'UTF-8');
}
return substr(utf8_decode($string), $offset, $length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment