Spaces:
Runtime error
Runtime error
| import json | |
| class BrailleConverter: | |
| def __init__(self, path_prefix="./src/utils/"): | |
| self.path_prefix = path_prefix | |
| self.map_files = { | |
| "alpha": "alpha_map.json", | |
| "number": "number_map.json" | |
| } | |
| self.current_map = "alpha" # 初始为字母表 | |
| self.load_map(self.current_map) | |
| self.switch_character = "001111" # 假设 "⠼" 为切换到数字表的标记符 | |
| self.remaining_number_mode_count = 0 # 用于跟踪在数字模式下的剩余字符数 | |
| def load_map(self, type_map: str): | |
| """加载对应的映射表""" | |
| if type_map not in self.map_files: | |
| raise ValueError(f"Unsupported type_map '{type_map}'. Available options: {list(self.map_files.keys())}") | |
| map_file_path = self.path_prefix + self.map_files[type_map] | |
| with open(map_file_path, "r") as fl: | |
| self.data = json.load(fl) | |
| def convert_to_braille_unicode(self, str_input: str) -> str: | |
| """将输入字符串转换为盲文 Unicode""" | |
| # 如果遇到数字切换标记符,进入数字模式并将计数器设置为2 | |
| if str_input == self.switch_character: | |
| self.current_map = "number" | |
| self.load_map(self.current_map) | |
| self.remaining_number_mode_count = 1 | |
| return "floor" # 不输出标记符 | |
| # 使用当前映射表转换字符 | |
| str_output = self.data.get(str_input, "") | |
| # 如果当前表是数字表,则减少计数器 | |
| if self.current_map == "number": | |
| self.remaining_number_mode_count -= 1 | |
| # 如果计数器为0,则切回字母表 | |
| if self.remaining_number_mode_count == 0: | |
| self.current_map = "alpha" | |
| self.load_map(self.current_map) | |
| return str_output | |