Spaces:
Runtime error
Runtime error
Upload test_conver.py
Browse files- test_conver.py +44 -0
test_conver.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
class BrailleConverter:
|
| 4 |
+
def __init__(self, path_prefix="./src/utils/"):
|
| 5 |
+
self.path_prefix = path_prefix
|
| 6 |
+
self.map_files = {
|
| 7 |
+
"alpha": "alpha_map.json",
|
| 8 |
+
"number": "number_map.json"
|
| 9 |
+
}
|
| 10 |
+
self.current_map = "alpha" # 初始为字母表
|
| 11 |
+
self.load_map(self.current_map)
|
| 12 |
+
self.switch_character = "001111" # 假设 "⠼" 为切换到数字表的标记符
|
| 13 |
+
self.remaining_number_mode_count = 0 # 用于跟踪在数字模式下的剩余字符数
|
| 14 |
+
|
| 15 |
+
def load_map(self, type_map: str):
|
| 16 |
+
"""加载对应的映射表"""
|
| 17 |
+
if type_map not in self.map_files:
|
| 18 |
+
raise ValueError(f"Unsupported type_map '{type_map}'. Available options: {list(self.map_files.keys())}")
|
| 19 |
+
map_file_path = self.path_prefix + self.map_files[type_map]
|
| 20 |
+
with open(map_file_path, "r") as fl:
|
| 21 |
+
self.data = json.load(fl)
|
| 22 |
+
|
| 23 |
+
def convert_to_braille_unicode(self, str_input: str) -> str:
|
| 24 |
+
"""将输入字符串转换为盲文 Unicode"""
|
| 25 |
+
# 如果遇到数字切换标记符,进入数字模式并将计数器设置为2
|
| 26 |
+
if str_input == self.switch_character:
|
| 27 |
+
self.current_map = "number"
|
| 28 |
+
self.load_map(self.current_map)
|
| 29 |
+
self.remaining_number_mode_count = 1
|
| 30 |
+
return "floor" # 不输出标记符
|
| 31 |
+
|
| 32 |
+
# 使用当前映射表转换字符
|
| 33 |
+
str_output = self.data.get(str_input, "")
|
| 34 |
+
|
| 35 |
+
# 如果当前表是数字表,则减少计数器
|
| 36 |
+
if self.current_map == "number":
|
| 37 |
+
self.remaining_number_mode_count -= 1
|
| 38 |
+
|
| 39 |
+
# 如果计数器为0,则切回字母表
|
| 40 |
+
if self.remaining_number_mode_count == 0:
|
| 41 |
+
self.current_map = "alpha"
|
| 42 |
+
self.load_map(self.current_map)
|
| 43 |
+
|
| 44 |
+
return str_output
|