生成一个CSV
文件,该文件用于导入Anki
应用程序。CSV
文件将包含以下内容:
- 正面:
泷文注音
。 - 反面:
词条内容
。
Python解译器代码
# 定义python文件路径
with open(r'------\【泷标Anki】泷文注音-基字.py', 'r', encoding='utf-8') as file:
code = file.read()
exec(code)
【泷标Anki】泷文注音-基字.py
import os
import csv
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)
## 卡片生成工具
# 定义词典文件夹路径'dictionary_folder'
dictionary_folder = r'------'
# 读取词典中的单词和含义
def read_dictionary(folder_path):
dictionary = {}
for filename in os.listdir(folder_path):
if filename.endswith('.md'):
if '%' in filename:
parts = filename[:-3].split('%')
# 确保文件名格式正确
if len(parts) == 2:
word, meaning = parts
# 如果含义已经存在于字典中,则将词条添加到已有的词条后面
if meaning in dictionary:
dictionary[meaning] += '/' + word
else:
dictionary[meaning] = word
return dictionary
# 递归读取词典中的单词和含义,包括子文件夹
def read_dictionary_recursive(folder_path):
dictionary = read_dictionary(folder_path)
for entry in os.scandir(folder_path):
if entry.is_dir(): # 如果是文件夹,则递归调用
nested_dict = read_dictionary_recursive(entry.path)
for meaning, words in nested_dict.items():
if meaning in dictionary:
dictionary[meaning] += '/' + words
else:
dictionary[meaning] = words
return dictionary
# 主函数
def main():
try:
# 提取卡片
dictionary = read_dictionary_recursive(dictionary_folder)
# 定义保存名称
output_file = r"------\dictionary_htmlspell_anki_cards.csv"
# 将词典写入到 CSV 文件
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = ['Front', 'Back']
writer =csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# 调整卡片内容格式
fontsize = input("默认不设置字号(输入数字设置字体字号):").strip()
for meaning, word in dictionary.items():
text = process_word(word)
spelltext = process_spell(text)
fonttext = (f"<span style=\"font-family: 'Hahabot'; font-size: {fontsize}px;\">{spelltext}</span>")
writer.writerow({'Front': fonttext, 'Back': meaning})
print(f"Anki 卡片已生成并保存到 {output_file}")
except Exception as e:
print(f"发生错误:{e}")
input("按 Enter 键退出...")
if __name__ == "__main__":
main()