首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >用python实现诸葛神数

用python实现诸葛神数

原创
作者头像
阿尔的代码屋
发布2024-12-24 20:23:51
发布2024-12-24 20:23:51
2190
举报

本文内容旨在使用python实现一个很简单的诸葛神数卜筮的小脚本。当然这个卜筮结果希望大家不必过分当真,明显从步骤和卦签上能感受到,这不是一个严肃正经的占卜,权当练习吧。

偶然在微信读书上被推送了《秘本诸葛神数》,大致阅读了一下,发现步骤挺简单的,就简单写了个脚本模拟了一下。

0x00 算法步骤

  1. 原文凡占卦者。必先报字。报字不拘何字。必先以三字为度。不可报四字。亦不可报二字,简单概括一下,就是这是一个用字测算的方法,需要报三个字来进行测算。
  2. 原文第一字作百数。第二字作十数。第三字作个数。: 拿到所报的字后,将第一个字来算百位数字,第二个字来算十位数,第三个字来算个位数。
  3. 原文凡字笔画。在九笔以内者照算。在十笔以外者。减十笔算。(二十笔同)若恰在十笔或二十笔。俱照零笔计算。: 用对应字的笔画数模10,即除以10取余
  4. 原文此书照大易三百八十四爻。作为三百八十四签。所报之字笔画以三百八十四为度。: 一共384条签文,组成的3位数字模384,将其对应到每一条签文中。
  5. 原文数已计定。即可查签。,通过得到的数字,在签文中找到对应签文查询签文

0x01 爬取签文

由于微信读书上的签文不全,检索之后在这里找到了完整的签文,写一个脚本将其爬取下来保存Auspicious文件夹中以待读取.

代码语言:python
复制
import requests
import re
from pathlib import Path


def get_auspicious(number: int):
    url = f"https://www.ttlingqian.com/zhuge/{number}.html"
    response = requests.get(url=url)
    content = response.content.decode("utf-8")
    pattern = r"<meta name=\"description\" content=\"(.*)\" />"
    auspicious = re.findall(pattern=pattern, string=content)[0]
    file = Path(f"Auspicious/auspicious_{number}.txt")
    with open(file=file, encoding="utf-8", mode="w") as f:
        print(f"第{number}卦:\t{auspicious}")
        f.write(auspicious)

0x02 获取汉字笔画

在unihan数据库中下载下来包含汉字笔画信息的数据文件,Unihan下载地址, 编写脚本读取出每个汉字的笔画信息以json格式保存下来

代码语言:python
复制
file = Path("Stroke/Unihan_IRGSources.txt")
output = Path("Stroke/unicode2stroke.json")
stroke_dict = dict()
with open(file,mode="r") as f:
    for line in f:
        raw_line = line.strip()
        pattern = r"(U\+.*)\skTotalStrokes.*\s(\d+)"
        result = re.findall(pattern=pattern, string=raw_line)
        if len(result) == 0:
            continue
        unicode_key = result[0][0]
        unicode_stroke = result[0][1]
        print(f"{unicode_key}: {unicode_stroke}")
        stroke_dict[unicode_key] = unicode_stroke

with open(file=output, mode="w", encoding="utf-8") as f:
    json.dump(stroke_dict,f, ensure_ascii=False, indent=4)

0x03 编写签号计算

按照算法步骤编写出签好的计算代码,非常简单直接,就不做过多介绍了.直接看代码就好

代码语言:python
复制
def get_character_stroke_count(char: str):
    unicode = "U+" + str(hex(ord(char)))[2:].upper()
    return int(unicode2stroke[unicode])


def get_number(first: str, second: str, third: str):
    hundreds = get_character_stroke_count(first) % 10
    tens = get_character_stroke_count(second) % 10
    ones = get_character_stroke_count(third) % 10
    number = (hundreds * 100 + tens * 10 + ones) % 384
    return number

0x04 封装部署

导入streamlit库,简单地做三个字符输入,再将对应签文显示出来。

代码语言:python
复制
if "__main__" == __name__:
    import streamlit as st

    st.title(f"诸葛秘数解签(仅供娱乐,切勿当真)")
    first = st.text_input(f"首字")
    second = st.text_input(f"中字")
    third = st.text_input(f"尾字")
    if st.button("求卦"):
        number = get_number(first=first, second=second, third=third)
        result = get_auspicious(number=number)
        st.write("签语:", result)

以公共仓库上传到Github,然后直接在Streamlit Share进行部署

当然也可以通过https://zhugehorary.streamlit.app/直接访问进行模拟

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 算法步骤
  • 0x01 爬取签文
  • 0x02 获取汉字笔画
  • 0x03 编写签号计算
  • 0x04 封装部署
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档