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

【正字法标注】基字→基字(拉丁正字注音)

tool python

从一个指定的文件夹中读取词典数据,并使用这些数据来标注用户输入的句子。

Python解译器代码

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

【正字法标注】基字→基字(拉丁正字注音).py

import os

# 定义词典文件夹路径'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 annotate_sentence(sentence, dictionary):
    glyphs = sentence.split()
    annotated_sentence = ""
    for glyph in sentence:
        # 查找对应的词条
        if glyph in dictionary:
            annotated_sentence += f"{glyph}{dictionary[glyph]})"
        else:
            if glyph not in ",./;[]\\\"<>?:{}|-=_+!`~@#$%^&*(),。;‘’“”'【】、—\·!¥…()":
                annotated_sentence += f"【{glyph}】"
            else:
	            annotated_sentence += glyph
    return annotated_sentence

# 主函数
def main():
    try:
        dictionary = read_dictionary_recursive(dictionary_folder)
        # 提示用户输入句子
        sentence = input("请输入你想要标注的句子:")
        annotated_sentence = annotate_sentence(sentence, dictionary)
        print("标注后的句子:")
        print(annotated_sentence)
    except Exception as e:
        print(f"发生错误:{e}")
        input("按 Enter 键退出...")

if __name__ == "__main__":
    main()