* adjust for pinyin.

This commit is contained in:
wangyidong
2016-12-06 10:33:09 +08:00
parent 673f23381b
commit 2f679cee93
2 changed files with 126 additions and 7 deletions
+35 -7
View File
@@ -113,17 +113,45 @@ class html extends baseHTML
$string = "<select name='$name' {$id} $attrib>\n";
/* The options. */
$pinyin = $app->loadClass('pinyin');
static $pinyin = null;
if(empty($pinyin))
{
$app->loadClass('pinyin', false);
helper::import(dirname(__DIR__) . '/pinyin/memoryfiledictloader.php');
$pinyin = new pinyin(new MemoryFileDictLoader(dirname(__DIR__) . '/pinyin/data/'));
}
$joinOptions = '';
$sign = ' aNd ';
foreach($options as $value)
{
if(!isset($dataKeys[$value])) $joinOptions .= $value . $sign;
}
$joinOptions = rtrim($joinOptions, $sign);
if($joinOptions)
{
$valuesPinyin = $pinyin->convert($joinOptions);
$values = explode($sign, $joinOptions);
$sign = trim($sign);
foreach($values as $value)
{
$valuePinyin = array();
$valueAbbr = '';
while($wordPinyin = array_shift($valuesPinyin))
{
if($wordPinyin == $sign or empty($wordPinyin)) break;
$valuePinyin[] = $wordPinyin;
$valueAbbr .= $wordPinyin[0];
}
$dataKeys[$value] = empty($valuePinyin) ? '' : join($valuePinyin) . ' ' . $valueAbbr;
}
}
if(is_array($selectedItems)) $selectedItems = implode(',', $selectedItems);
$selectedItems = ",$selectedItems,";
foreach($options as $key => $value)
{
if(!isset($dataKeys[$value]))
{
$dataKeys[$value] = '';
$permalink = $pinyin->permalink($value, '');
if($value != $permalink) $dataKeys[$value] = $permalink . ' ' . $pinyin->abbr($value, '');
}
$key = str_replace('item', '', $key);
$selected = strpos($selectedItems, ",$key,") !== false ? " selected='selected'" : '';
$string .= "<option value='$key'$selected data-keys='{$dataKeys[$value]}'>$value</option>\n";
+91
View File
@@ -0,0 +1,91 @@
<?php
/*
* This file is part of the overtrue/pinyin.
*
* (c) 2016 overtrue <i@overtrue.me>
*/
helper::import(__DIR__ . '/dictloaderinterface.php');
/**
* Memory Dict File loader.
*/
class MemoryFileDictLoader implements DictLoaderInterface
{
/**
* Data directory.
*
* @var string
*/
protected $path;
/**
* Words segment name.
*
* @var string
*/
protected $segmentName = 'words_%s';
/**
* Segment files.
*
* @var array
*/
protected $segments = array();
/**
* Surname cache.
*
* @var array
*/
protected $surnames = array();
/**
* Constructor.
*
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;
for ($i = 0; $i < 100; ++$i) {
$segment = $path.'/'.sprintf($this->segmentName, $i);
if (file_exists($segment)) {
$this->segments[] = (array) include $segment;
}
}
}
/**
* Load dict.
*
* @param Closure $callback
*/
public function map(Closure $callback)
{
foreach ($this->segments as $dictionary) {
$callback($dictionary);
}
}
/**
* Load surname dict.
*
* @param Closure $callback
*/
public function mapSurname(Closure $callback)
{
if (empty($this->surnames)) {
$surnames = $this->path.'/surnames';
if (file_exists($surnames)) {
$this->surnames = (array) include $surnames;
}
}
$callback($this->surnames);
}
}