Created
October 6, 2009 11:56
-
-
Save tomohiro/202947 to your computer and use it in GitHub Desktop.
SKK の人名辞書ファイルから架空の氏名を生成する
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 | |
/** | |
* SKK の人名辞書ファイルから架空の氏名を生成する | |
* | |
* @copyright Copyright (c) 2009 Tomohiro, TAIRA <[email protected]> | |
* @url http://github.com/Tomohiro | |
* | |
* <code> | |
* $name = new FakeFullName(); | |
* echo $name->generate()->kanji(); | |
* </code> | |
*/ | |
class FakeFullName | |
{ | |
/** | |
* SKK 人名辞書の URI | |
* | |
* @var const | |
*/ | |
const DICTIONARY_URI = 'http://openlab.jp/skk/skk/dic/SKK-JISYO.jinmei'; | |
/** | |
* プロキシのアドレスとポート | |
* ex. example.com:80 | |
* | |
* @var string | |
*/ | |
public $proxy = ''; | |
/** | |
* 姓のリスト | |
* | |
* @var array | |
*/ | |
private $_lastNames = array(); | |
/** | |
* 名のリスト | |
* | |
* @var array | |
*/ | |
private $_firstNames = array(); | |
/** | |
* Name オブジェクトの雛形 | |
* | |
* @var object | |
*/ | |
private $_templateName = null; | |
/** | |
* コンストラクタ | |
* | |
* @param void | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->_genNameList(); | |
$this->_templateName = new Name(); | |
} | |
/** | |
* 姓名リストからランダムに抽出し氏名を生成する | |
* | |
* @param void | |
* @return Name $fullName | |
*/ | |
public function generate() | |
{ | |
$fullName = clone $this->_templateName; | |
$fullName->firstName = $this->_firstNames[array_rand($this->_firstNames)]; | |
$fullName->lastName = $this->_lastNames[array_rand($this->_lastNames)]; | |
return $fullName; | |
} | |
/** | |
* 辞書ファイルを元に姓と名のリストを生成する | |
* | |
* @param void | |
* @return void | |
*/ | |
private function _genNameList() | |
{ | |
$dictionary = $this->_getDictionary(); | |
foreach ($dictionary as $row) { | |
if (mb_ereg('^;', $row)) { | |
continue; | |
} | |
$nameInformationList = split('/', mb_convert_encoding($row, 'UTF-8', 'EUC-JP')); | |
$kana = trim(mb_convert_kana(array_shift($nameInformationList), 'KVC')); | |
$this->_classifyName($nameInformationList, $kana); | |
} | |
} | |
/** | |
* SKK の人名辞書を取得する | |
* | |
* @param void | |
* @return array $dictionary | |
*/ | |
private function _getDictionary() | |
{ | |
$options = array( | |
'http' => array( | |
'method' => 'GET', | |
'proxy' => "tcp://$this->proxy", | |
'request_fulluri' => true) | |
); | |
if (empty($this->proxy)) { | |
unset($options['http']['proxy']); | |
} | |
$context = stream_context_create($options); | |
$dictionary = split("\n", file_get_contents(self::DICTIONARY_URI, false, $context)); | |
return $dictionary; | |
} | |
/** | |
* 辞書ファイルから渡された情報を元に | |
* 姓と名を分類する | |
* | |
* @param array $theNameInformationList | |
* @param string $theKana | |
* @return void | |
*/ | |
private function _classifyName($theNameInformationList, $theKana) | |
{ | |
foreach ($theNameInformationList as $nameInformation) { | |
$infos = split(';', $nameInformation); | |
$name = $infos[0]; | |
$class = $infos[1]; | |
switch ($class) { | |
case '名': | |
$this->_firstNames[] = array('kanji' => $name, 'kana' => $theKana); | |
break; | |
case '姓': | |
$this->_lastNames[] = array('kanji' => $name, 'kana' => $theKana); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
/** | |
* PHP のマジックメソッドを利用した Setter | |
* | |
* @param string $theKey | |
* @param mixed $theValue | |
* @return void | |
*/ | |
public function __set($theKey, $theValue) | |
{ | |
$this->$theKey = $theValue; | |
} | |
/** | |
* PHP のマジックメソッドを利用した Getter | |
* | |
* @param string $theKey | |
* @return mixed $this->$theKey | |
*/ | |
public function __get($theKey) | |
{ | |
return $this->$theKey; | |
} | |
} | |
/** | |
* 名前を扱うクラス | |
* | |
* @copyright Copyright (c) 2009 Tomohiro, TAIRA <[email protected]> | |
* @url http://github.com/Tomohiro | |
*/ | |
class Name | |
{ | |
/** | |
* 漢字 | |
* | |
* @var string | |
*/ | |
public $kanji = null; | |
/** | |
* カタカナ | |
* | |
* @var string | |
*/ | |
public $kana = null; | |
/** | |
* 名 | |
* | |
* @var string | |
*/ | |
public $firstName = array(); | |
/** | |
* 姓 | |
* | |
* @var string | |
*/ | |
public $lastName = array(); | |
/** | |
* 漢字氏名を返す | |
* | |
* @return string | |
*/ | |
public function kanji() | |
{ | |
return "{$this->lastName['kanji']} {$this->firstName['kanji']}"; | |
} | |
/** | |
* カタカナ氏名を返す | |
* | |
* @return string | |
*/ | |
public function kana() | |
{ | |
return "{$this->lastName['kana']} {$this->firstName['kana']}"; | |
} | |
/** | |
* PHP のマジックメソッドを利用した Setter | |
* | |
* @param string $theKey | |
* @param mixed $theValue | |
* @return void | |
*/ | |
public function __set($theKey, $theValue) | |
{ | |
$this->$theKey = $theValue; | |
} | |
/** | |
* PHP のマジックメソッドを利用した Getter | |
* | |
* @param string $theKey | |
* @return mixed $this->$theKey | |
*/ | |
public function __get($theKey) | |
{ | |
return $this->$theKey; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment