- 60浏览
- 2023-04-23
八字命理:计算出生辰八字信息,获取五行缺失情况以及各个元素的旺衰情况和喜忌属性等。
姓名学原理:根据「三才」、「五格」等理论,计算名字中的五行缺失情况、笔画以及音律情况等。
词库匹配:根据指定的词库,匹配符合条件的汉字,组成符合条件的名字。
// 接收参数
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
// 汉字编码范围
$GB = [
["B0", "C5"], // 第一区间
["C5", "D7"], // 第二区间
];
// 词库及其权重
$ciKu = [
["玲", "珑", "翎", "翕", "翎", "琳", "琦", "琪", "麟", "晨", "辰", "晖", "辉", "伊", "妍", "秀", "雯", "慧", "婷", "娜", "洁", "美", "若", "馨", "咏", "怡", "茜", "筠", "蕊", "芝", "芷", "琴", "菁", "妤", "露", "颖", "婵", "婧", "嫣", "媛", "瑗", "玉", "珂", "娆", "妹", "姿"],
["依", "伶", "怡", "岚", "梦", "泽", "菀", "红", "涵", "洁", "芊", "宁", "雨", "荷", "文", "夏", "菲", "绮", "馨", "楠", "涵", "洋", "晴", "思", "诗", "吟", "敏", "雅", "儒", "俊", "亮", "志勇", "伟", "强", "康", "小", "明", "国", "建", "才", "华", "泰"],
];
// 八字数据
$eightWords = [
'year' => 1990, // 出生年份
'month' => 2, // 出生月份
'day' => 23, // 出生日
'hour' => 11, // 出生时
'sex' => 1, // 性别:男-1,女-0
];
// 起名的笔画要求,总笔画在8到15之间
$biHuaLimit = ['min' => 8, 'max' => 15];
// 定义五行属性及其喜忌
$wuxing = [
'金' => ['生' => ['水'], '克' => ['木'], '喜' => ['土'], '忌' => ['火']],
'木' => ['生' => ['火'], '克' => ['土'], '喜' => ['水', '木'], '忌' => ['金']],
'水' => ['生' => ['木'], '克' => ['火'], '喜' => ['金'], '忌' => ['土']],
'火' => ['生' => ['土'], '克' => ['水'], '喜' => ['木'], '忌' => ['金']],
'土' => ['生' => ['金'], '克' => ['木'], '喜' => ['水'], '忌' => ['火']],
];
// 八字计算,返回各种元素的值
function bazi($eightWords) {
// 省略八字计算的具体实现过程
return ['year' => '辛巳', 'month' => '乙卯', 'day' => '庚寅', 'hour' => '丁酉'];
}
// 获取汉字笔画数
function getBiHua($word) {
// 省略笔画查询的具体实现过程
return 8;
}
// 计算名字五行属性(三才)
function sanCai($surname, $name) {
// 计算姓氏的三才五格
$totalBiHua = 0;
foreach (str_split($surname) as $char) {
$totalBiHua += getBiHua($char);
}
$sanCaiScore = intval(($totalBiHua - 1) / 4) + 1;
$sanCaiScore = $sanCaiScore > 4 ? 4 : $sanCaiScore;
// 计算名字的三才五格
$nameBiHua = 0;
$nameWuXing = ['金' => 0, '木' => 0, '水' => 0, '火' => 0, '土' => 0];
foreach (str_split($name) as $char) {
$biHua = getBiHua($char);
$nameBiHua += $biHua;
// 获取汉字五行属性
$wx = getWuXing($char);
$nameWuXing[$wx] += $biHua;
}
// 计算总笔画数和名字中五行缺失的数量
$totalBiHua = $nameBiHua + $totalBiHua;
$queShiCounts = [];
foreach ($wuxing as $wx => $info) {
$count = $totalBiHua / 100 * $info['percentage'];
$queShiCounts[$wx] = round($count - $nameWuXing[$wx]);
}
return [
'san_cai_score' => $sanCaiScore,
'que_shi_counts' => $queShiCounts,
];
}
// 根据五行属性及喜忌计算候选汉字权重
function calculateCandidateWeight($queShiCounts, $wuxing) {
// 候选汉字的权重
$weights = [];
foreach ($ciKu as $words) {
foreach ($words as $word) {
// 获取汉字的五行属性
$wx = getWuXing($word);
// 计算汉字权重
$weight = 0;
if ($wx && !in_array($wx, $queShiCounts['que_shi_wuxing'])) {
// 如果汉字的五行属性符合要求,则根据喜忌计算权重
foreach ($queShiCounts[$wx] as $count) {
if ($count > 0) {
$weight += $count * $wuxing[$wx]['喜']['weight'];
} else {
$weight += abs($count) * $wuxing[$wx]['忌']['weight'];
}
}
// 将汉字权重保存到候选汉字列表中
$weights[$word] = $weight;
}
}
}
return $weights;
}
// 从候选汉字列表中获取符合笔画要求的名字
function getNameByBiHua($weights, $biHuaLimit) {
$names = [];
foreach ($weights as $word => $weight) {
$biHua = getBiHua($word);
if ($biHua >= $biHuaLimit['min'] && $biHua <= $biHuaLimit['max']) {
$names[$word] = $weight;
}
}
return $names;
}
// 从候选汉字列表中获取最符合条件的名字
function getBestName($nameWeights) {
arsort($nameWeights); // 倒序排列权重
if (count($nameWeights) > 0) {
return array_key_first($nameWeights);
} else {
return '';
}
}
// 获取汉字的五行属性
function getWuXing($word) {
// 省略根据笔画查询五行属性的代码
$wx = '木';
return $wx;
}
// 计算名字
function calculateName($eightWords, $biHuaLimit) {
// 计算八字元素及其喜忌
$bazi = bazi($eightWords);
$wuxing['年'] = getWuXing($bazi['year'][0]);
$wuxing['月'] = getWuXing($bazi['month'][0]);
$wuxing['日'] = getWuXing($bazi['day'][0]);
$wuxing['时'] = getWuXing($bazi['hour'][0]);
// 计算姓氏三才五格和名字五行缺失情况
$sanCaiInfo = sanCai('赵', $name);
// 根据五行属性及喜忌计算候选汉字权重
$weights = calculateCandidateWeight($sanCaiInfo['que_shi_counts'], $wuxing);
// 根据笔画要求获取名字列表
$names = getNameByBiHua($weights, $biHuaLimit);
// 获取最符合条件的名字
$name = getBestName($names);
return $name;
}
// 测试代码
$name = calculateName($eightWords, $biHuaLimit);
echo $name;
需要注意的是,这段代码实现了一个非常基础的自动起名方案,实际应用中还需要进一步完善。例如,需要考虑到不同词库之间的冲突问题、如何优化计算性能、如何处理人名重名等问题。版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、本文由会员转载自互联网,如果您是文章原创作者,请联系本站注明您的版权信息。