Skip to content

Instantly share code, notes, and snippets.

@geta6
Created December 26, 2011 12:00
Show Gist options
  • Save geta6/1520984 to your computer and use it in GitHub Desktop.
Save geta6/1520984 to your computer and use it in GitHub Desktop.
日本語/英語から任意文字数の識別子を生成します
#!/usr/bin/php
<?php
// 辞書
$dicts = array(
array('jp'=>'英単語', 'en'=>'English Words'),
);
// 生成する識別子の文字数
$n = 4;
foreach ($dicts as $dict) :
// n文字の識別子($dict['id'])を作る
// 大文字開始文字列に展開
// 母音を出現頻度順(日本語ローマ字)に削って合成
// n文字になった時点で完了する
preg_match_all('/[A-Z][a-z]*/', $dict['en'], $reg);
$reg = $reg[0];
init:
// フィルタリング,なくてもいい
// ローマ字を識別子化する時に使えばいい
$reg = array_map(function($m){
$m = str_replace('shi','si',$m);
// :
return $m;
},$reg);
$regs = str_replace(array('a','e','i','o','u'), '', strtolower(implode($reg)));
$dict['id'] = substr($regs,0,$n);
if ($n>strlen($dict['id'])) :
$regs = str_replace(array('a','i','o','u'), '', strtolower(implode($reg)));
$dict['id'] = substr($regs,0,$n);
if ($n>strlen($dict['id'])) :
$regs = str_replace(array('a','i','o',), '', strtolower(implode($reg)));
$dict['id'] = substr($regs,0,$n);
if ($n>strlen($dict['id'])) :
$regs = str_replace(array('a','i'), '', strtolower(implode($reg)));
$dict['id'] = substr($regs,0,$n);
if ($n>strlen($dict['id'])) :
$regs = str_replace(array('a'), '', strtolower(implode($reg)));
$dict['id'] = substr($regs,0,$n);
if ($n>strlen($dict['id'])) :
$regs = implode($reg);
$dict['id'] = substr($regs,0,$n);
if ($n>strlen($dict['id'])) :
foreach(json_decode(file_get_contents("http://pipes.yahoo.com/pipes/pipe.run?_id=ohZnaGr92xG36xIFyzUFzw&_render=json&text={$dict['ja']}"))->value->items as $value) :
$reg = array(preg_replace('/[^A-Za-z]/','',(string)$value->description));
break;
endforeach;
goto init;
endif;
endif;
endif;
endif;
endif;
endif;
$dict['id'] = strtoupper($dict['id']);
$resdict[] = $dict;
endforeach;
print_r($resdict);
/*
Array
(
[0] => Array
(
[jp] => 英単語
[en] => English Words
[dis] => NGLS
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment