自己製作百家姓、菜市場名

edited 十月 2013 in 原創軟體分享區
其實只是一個測試的需求,想要塞一些比較實際的資料,然後就順便做了這個...

Chih-Hao Tsai 有提供一些統計資訊:
http://technology.chtsai.org/namefreq/

也有把統計基礎的原始資料釋出:
http://technology.chtsai.org/namelist/

所以就寫了下面這個簡單程式去取百家姓、菜市場名
<?php
$fhR = fopen('unique_names_2009.txt', 'r');
$firstNameStack = $lastNameStack = array();
while($line = fgets($fhR, 128)) {
    $name = mb_convert_encoding($line, 'utf8', 'cp950');
    $name = trim($name);
    if(strpos($name, '?') === FALSE && strpos($name, '*') === FALSE && mb_strlen($name, 'utf8') == 3) {
        /*
         * 只取 3 個字的
         */
        $lastName = mb_substr($name, 0, 1, 'utf8');
        $firstName = mb_substr($name, 1, 2, 'utf8');
        if(!isset($firstNameStack[$firstName])) {
            $firstNameStack[$firstName] = 1;
        } else {
            ++$firstNameStack[$firstName];
        }
        if(!isset($lastNameStack[$lastName])) {
            $lastNameStack[$lastName] = 1;
        } else {
            ++$lastNameStack[$lastName];
        }
    }
}
arsort($lastNameStack);
arsort($firstNameStack);
$fhLastName = fopen('lastname.txt', 'w');
$fhFirstName = fopen('firstname.txt', 'w');
for($i = 0; $i < 500; $i++) {
    fwrite($fhLastName, key($lastNameStack) . chr(10));
    fwrite($fhFirstName, key($firstNameStack) . chr(10));
    next($lastNameStack);
    next($firstNameStack);
}

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4601
Sign In or Register to comment.