Spaces:
Sleeping
Sleeping
File size: 4,899 Bytes
6cc5c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import gradio as gr
# 摩斯电码映射字典
MORSE_CODE_DICT = {
'A': '._', 'B': '_...', 'C': '_._.', 'D': '_..', 'E': '.',
'F': '.._.', 'G': '__.', 'H': '....', 'I': '..', 'J': '.___',
'K': '_._', 'L': '._..', 'M': '__', 'N': '_.', 'O': '___',
'P': '.__.', 'Q': '__._', 'R': '._.', 'S': '...', 'T': '_',
'U': '.._', 'V': '..._', 'W': '.__', 'X': '_.._', 'Y': '_.__', 'Z': '__..',
'1': '.____', '2': '..___', '3': '...__', '4': '...._', '5': '.....',
'6': '_....', '7': '__...', '8': '___..', '9': '____.', '0': '_____',
' ': '/', ',': '__..__', '.': '._._._', '?': '..__..',
'!': '_._..__', '-': '_...._', '/': '_.._.', '@': '.__._.',
'(': '_.__.', ')': '_.__._', '&': '._...', ':': '___...',
';': '_._._.', '=': '_..._', '+': '._._.', '"': '._.._.',
'$': '..._.._', "'": '.____.', '%': '._.._.', '<': '._.._.',
'>': '._._.', '[': '_.__.', ']': '_.__._', '{': '_.__.',
'}': '_.__._', '\\': '_..__'
}
# 创建摩斯电码到字符的反向映射
MORSE_CODE_REVERSE = {v: k for k, v in MORSE_CODE_DICT.items()}
def text_to_binary(text):
"""将文本转换为二进制"""
binary = ' '.join(format(ord(char), '08b') for char in text)
return binary
def binary_to_text(binary):
"""将二进制转换为文本"""
try:
# 删除所有空格
binary = binary.replace(' ', '')
# 检查是否只包含0和1
if not all(bit in '01' for bit in binary):
return "错误:二进制只能包含0和1"
# 确保二进制字符串长度是8的倍数
if len(binary) % 8 != 0:
return "错误:二进制长度必须是8的倍数"
text = ''
for i in range(0, len(binary), 8):
byte = binary[i:i+8]
text += chr(int(byte, 2))
return text
except Exception as e:
return f"解码错误: {str(e)}"
def text_to_morse(text):
"""将文本转换为摩斯电码"""
morse = []
for char in text.upper():
if char in MORSE_CODE_DICT:
morse.append(MORSE_CODE_DICT[char])
else:
# 对于不支持的字符,保持原样
morse.append(char)
return ' '.join(morse)
def morse_to_text(morse):
"""将摩斯电码转换为文本"""
try:
morse = morse.strip()
# 处理单词间的分隔符 '/'
words = morse.split(' / ')
result = []
for word in words:
chars = word.split(' ')
word_result = ''
for char in chars:
if char in MORSE_CODE_REVERSE:
word_result += MORSE_CODE_REVERSE[char]
elif char == '':
# 跳过空字符
continue
else:
# 对于不支持的摩斯电码,用问号替代
word_result += '?'
result.append(word_result)
return ' '.join(result)
except Exception as e:
return f"解码错误: {str(e)}"
def process_encoding(text, method):
"""根据选择的方法编码文本"""
if method == "二进制":
return text_to_binary(text)
elif method == "摩斯电码":
return text_to_morse(text)
return "请选择编码方法"
def process_decoding(code, method):
"""根据选择的方法解码文本"""
if method == "二进制":
return binary_to_text(code)
elif method == "摩斯电码":
return morse_to_text(code)
return "请选择解码方法"
# 创建 Gradio 界面
with gr.Blocks(title="文字编码解码工具") as demo:
gr.Markdown("# 文字编码与解码工具")
encode_method = gr.Radio(["二进制", "摩斯电码"], label="编码方法", value="二进制")
with gr.Tab("编码"):
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="输入文本", lines=5, placeholder="Someone tell Vedal there is a problem with my AI")
with gr.Column():
output_code = gr.Textbox(label="编码结果", lines=5)
encode_btn = gr.Button("编码")
with gr.Tab("解码"):
with gr.Row():
# decode_method = gr.Radio(["二进制", "摩斯电码"], label="解码方法", value="二进制")
with gr.Column():
input_code = gr.Textbox(label="输入编码", lines=5, placeholder="请输入要解码的编码")
with gr.Column():
output_text = gr.Textbox(label="解码结果", lines=5)
decode_btn = gr.Button("解码")
# 设置按钮点击事件
encode_btn.click(fn=process_encoding, inputs=[input_text, encode_method], outputs=output_code)
decode_btn.click(fn=process_decoding, inputs=[input_code, encode_method], outputs=output_text)
# 启动应用
if __name__ == "__main__":
demo.launch() |