Created
September 21, 2009 14:46
-
-
Save co3k/190271 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
*/ | |
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