大须大镜 | Mayahilwa Mayakimhemot/ toolbox/ 目度 Systemedo

【泷注分割】拉丁正字注音→泷文注音

tool python

将拉丁正字写法的语句,按照泷文字母所代表的音进行切分,并渲染为泷文字母的综合程序。

示例

输入你想要切音的词句

TALUSHAL BUHHIAYUKANOL KUOTO, USUTOT OS LÄPHASA BUHNEMO KUBBAPULEH KUBHAMA ILAH CHIMAYL DHELATA. 

默认切句、字号24后1

타로상래 보해히낭옹카누래 코눙투, 농소투태 눙새 르패하사 보해네무 코배바포레해 코배하마 닝라해 칭마앵래 젱라타.
<span style="font-family: 'Hahabot'; font-size: 24px;">타로상래 보해히낭옹카누래 코눙투, 농소투태 눙새 르패하사 보해네무 코배바포레해 코배하마 닝라해 칭마앵래 젱라타.</span>

Python解译器代码

# 定义python文件路径
with open(r'------\【泷注分割】拉丁正字注音→泷文注音.py', 'r', encoding='utf-8') as file:
    code = file.read()
exec(code)

【泷注分割】拉丁正字注音→泷文注音.py

import re

## 转换工具
# 正字表和注音表
tail_odot = ['o','a','ä','e','ay','am','um','eh']
vowel_odot = ['a','i','u','e','o','ä']
consonant_odot = ['r','y','w','n','jn','m','h','f','p','b','k','g','c','x','t','d','s','z','ch','dh','sh','l','cq','dq','sq','q']

tail_odeh = '운안은엔앛암옴엫'
syllable_odeh = '낭닝농넹눙능앙잉옹엥웅응망밍몽멩뭉믕나니노네누느아이오에우으마미모메무므하히호헤후흐함힘홈헴훔흠파피포페푸프바비보베부브카키코케쿠크가기고게구그차치초체추츠참침촘쳄춤츰타티토테투트다디도데두드사시소세수스자지조제주즈창칭총쳉충층장징종젱중증상싱송셍숭승라리로레루르찰칠촐첼출츨잘질졸젤줄즐살실솔셀술슬랄릴롤렐룰를'
consonant_odeh = '냉앵맹내애매해햄패배캐개채챔태대새재챙쟁생래챌잴샐랠'

syllable_odot = []
# 大循环遍历辅音
for i in range(len(consonant_odot)):
    # 小循环遍历元音
    for j in range(len(vowel_odot)):
        # 组合当前辅音和元音
        combination = f"{consonant_odot[i]}{vowel_odot[j]}"
        # 添加到结果列表中
        syllable_odot.append(combination)
print(syllable_odot)

# 构建字典
tail_mapping = dict(zip(tail_odot,tail_odeh))
syllable_mapping = dict(zip(syllable_odot,syllable_odeh))
consonant_mapping = dict(zip(consonant_odot,consonant_odeh))
mapping = {**tail_mapping, **syllable_mapping, **consonant_mapping}
print(mapping)

# 处理字符串,替换每个'-xx'块
def process_spell(text):
    k = 3
    while k > 0:
        # 找到所有'-xx'块,其中xx是连续的字母序列
        blocks = re.findall(r'-([a-zä]{%d})'%k, text)
        # 替换每个块
        for block in blocks:
            if block in mapping:
                text = text.replace('-' + block, mapping[block])
        k -= 1
    return text


## 切音工具
# 设定字母集
vowels = 'aiueoä'
single_letter_consonants = 'rywnmhfpbkgcxtdszlq'
multi_letter_consonants = ['jn', 'ch', 'dh', 'sh', 'cq', 'dq', 'sq']
split_letter_consonants = ['jh', 'jq']

# 处理单词
def process_word(word):
    word = word.lower()  # 将单词小写化
    result = []  # 存储处理后的结果
    i = 0  # 字母在单词内的索引
    while i < len(word):
        # python截断操作包含括号前一个索引,不含后一个
        if i + 1 < len(word) and word[i:i+2] in multi_letter_consonants:
            # 处理多字母辅音
            result.append('-' + word[i:i+2])
            i += 2
        elif i + 1 < len(word) and word[i:i+2] in split_letter_consonants:
            # 处理易混辅音簇
            result.append('-' + word[i+1:i+2])
            i += 2
        elif word[i] in single_letter_consonants:
            # 处理单字母辅音
            result.append('-' + word[i])
            i += 1
        elif word[i] in vowels:
            # 处理元音
            if i == 0:
                # 首字母为元音
                result.append('-r' + word[i])
                i += 1
            elif word[i-1] in vowels:
                # 前一字母为元音
                result.append('-r' + word[i])
                i += 1
            elif not word[i-1] in single_letter_consonants:
                # 前一字符非辅音
                result.append('-r' + word[i])
                i += 1
            else:
                # 前一字母为辅音
                result.append(word[i])
                i += 1
        else:
            # 处理符号和其它字符
            result.append(word[i])
            i += 1
    return ''.join(result)


## 主函数
def main():
    try:
        sentence = input("请输入你想要转换的句子:")
        if input("默认自动切句(输入非空白内容取消切句):").strip() == '':
            print("执行自动切句。")
            text = process_word(sentence)
        else:
            print("不执行自动切句。")
            text = sentence
        print("切音完成:")
        print(text)
        fontsize = input("默认不设置字体(输入数字字号设置字体):").strip()
        if fontsize == '':
            fonttext = process_spell(text)
        else:
	        fonttext = (f"<span style=\"font-family: 'Hahabot'; font-size: {fontsize}px;\">{process_spell(text)}</span>")
        print("转换完成:")
        print(fonttext)
    except Exception as e:
        print(f"发生错误:{e}")
        input("按 Enter 键退出...")

if __name__ == "__main__":
    main()

  1. 通常,利用替换工具将空格 替换为双空格 ,在荷Hahabot字体中展现的效果比默认更好。 ↩︎