public function testPinyin() { $test = new Pinyin(); $content = $test->getPinyin('你好拼音'); $this->comment($content); $this->assertTrue(!empty($content)); }
/** * ... * * @param $strings * @return array */ public static function generate($strings) { // TODO 逐个连接字符 $generates = []; foreach ($strings as $string) { // 添加 $name = Pinyin::getPinyin($string) . '@' . Pinyin::getPinyinFirst($string); $info = []; $account = new Account(); $account->setName($name); $account->addAmount($string, $info); $generates[] = $account; } return $generates; }
/** * Suggest constructor. * * @param $app * @param array $config */ public function __construct($connect, $prepare = []) { if (!empty($prepare)) { foreach (self::$config as $name => $value) { if (isset($prepare[$name])) { self::$config[$name] = $prepare[$name]; } } } // prepare redis conection if (isset(self::$config['database'])) { $connect->select(self::$config['database']); } else { $connect->select(RedisStore::DATABASE); } Pinyin::init(); $this->bank = new Bank($connect); }
/** * 生成搜索key * * @return string */ public function getSearchKey($keyChain = [Suggest::KEY_RAW, Suggest::KEY_PINYIN]) { $keys = []; // check have pinyin word if (Suggest::$config['cn_inlcude_pinyin'] != true) { $keyChain = [Suggest::KEY_RAW]; } // generate keys for search foreach ($keyChain as $key) { if ($key == Suggest::KEY_RAW) { // raw char $word = Analyze::clear($this->word); $keys[] = strlen($this->word) > 1 ? RedisStore::PREFIX . $this->getType() . '#' . '*' . '@' . '*' . $word . '*' : RedisStore::PREFIX . $this->getType() . '#' . '*' . '@' . $word . '*'; } if ($key == Suggest::KEY_PINYIN) { // pinyin $pinyin = Analyze::clear(Pinyin::getPinyin($this->word)); $keys[] = strlen($this->word) > 1 ? RedisStore::PREFIX . $this->getType() . '#' . '*' . $pinyin . '*' . '@' . '*' : RedisStore::PREFIX . $this->getType() . '#' . $pinyin . '*' . '@' . '*'; } } return $keys; }
/** * 缓存全拼的 模糊音 * @param int $isAll * @return array $cacheArray */ public function getCacheFuzzySoundPinyin($isAll = 0, $dict = null) { $fss = Pinyin::$fuzzySoundTranslate; //$cacheArray = $this->CacheInitials($isAll); $pinyin = Pinyin::init(); if ($dict === null) { $dict = $this->getAnalyzeDict(); } if ($isAll == 1) { //each word foreach ($dict as $words) { $stringArray = $pinyin->stringToArray($words); $linkpinyin = ''; foreach ($stringArray as $k => $word) { $tranPinyin = $pinyin->getPinyin($word); $linkpinyin = $linkpinyin . $tranPinyin; $cacheArray[$linkpinyin][] = $words; if ($k > 0) { $cacheArray[$linkpinyin][] = $words; } } } } else { //first word foreach ($dict as $v) { $stringArray = $pinyin->stringToArray($v); $key = $pinyin->getPinyin($stringArray[0]); $cacheArray[$key][] = $v; } } $selfToRelative = []; $relativeToSelf = []; //exchange fuzzy sound //self to relative foreach ($cacheArray as $key => $words) { //check exist fuzzy sound foreach ($fss['first'] as $self => $relative) { //case first self to relative if (preg_match("/^{$self}+[^h]/i", $key) === 1) { //exist relative letter $keyRelative = str_replace($self, $relative, $key); if (!array_key_exists($keyRelative, $cacheArray)) { //copy $selfToRelative[$keyRelative] = $cacheArray[$key]; } if (array_key_exists($keyRelative, $cacheArray)) { //merge $merge = array_unique(array_merge($cacheArray[$keyRelative], $cacheArray[$key])); $selfToRelative[$keyRelative] = $merge; } } } foreach ($fss['last'] as $self => $relative) { if (preg_match("/{$self}\$/i", $key) === 1) { //exist relative letter $keyRelative = str_replace($self, $relative, $key); if (!array_key_exists($keyRelative, $cacheArray)) { //copy $selfToRelative[$keyRelative] = $cacheArray[$key]; } if (array_key_exists($keyRelative, $cacheArray)) { //merge $merge = array_unique(array_merge($cacheArray[$keyRelative], $cacheArray[$key])); $selfToRelative[$keyRelative] = $merge; } } } } //relative to self foreach ($cacheArray as $key => $words) { //check exist fuzzy sound foreach ($fss['first'] as $self => $relative) { //case first self to relative if (preg_match("/^{$relative}/i", $key) === 1) { //exist relative letter $keyRelative = str_replace($relative, $self, $key); if (!array_key_exists($keyRelative, $cacheArray)) { //copy $relativeToSelf[$keyRelative] = $cacheArray[$key]; } if (array_key_exists($keyRelative, $cacheArray)) { //merge $merge = array_unique(array_merge($cacheArray[$keyRelative], $cacheArray[$key])); $relativeToSelf[$keyRelative] = $merge; } } } foreach ($fss['last'] as $self => $relative) { //case first self to relative if (preg_match("/{$relative}\$/i", $key) === 1) { //exist relative letter $keyRelative = str_replace($relative, $self, $key); if (!array_key_exists($keyRelative, $cacheArray)) { //copy $relativeToSelf[$keyRelative] = $cacheArray[$key]; } if (array_key_exists($keyRelative, $cacheArray)) { //merge $merge = array_unique(array_merge($cacheArray[$keyRelative], $cacheArray[$key])); $relativeToSelf[$keyRelative] = $merge; } } } } return $this->mergeData($relativeToSelf, $selfToRelative); }