File size: 7,175 Bytes
7b0a02f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465deb2
7b0a02f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import gradio as gr
import jieba
from script.pyPengIm import pyPengIm 
from script import utils
import yaml

# 自定义CSS样式
custom_css = """
.btn1 {
  background: #2563eb;   /* 主蓝 */
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 18px;
  font-size: 14px;
  transition: background 0.2s ease;
}
.btn1:hover {
  background: #1d4ed8;   /* 深蓝 */
}

/* 按钮样式2:次要操作(灰色) */
.btn2 {
  background: #f3f4f6;   /* 浅灰背景 */
  color: #374151;        /* 深灰字 */
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 8px 18px;
  font-size: 14px;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.btn2:hover {
  background: #e5e7eb;
  border-color: #9ca3af;
}

/* 按钮样式3:成功/确认(绿色) */
.btn3 {
  background: #10b981;   /* 绿色 */
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 18px;
  font-size: 14px;
  transition: background 0.2s ease;
}
.btn3:hover {
  background: #059669;   /* 深绿 */
}

/* 按钮样式4:警告/强调(橙色) */
.btn4 {
  background: #f97316;   /* 橙色 */
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px 18px;
  font-size: 14px;
  transition: background 0.2s ease;
}
.btn4:hover {
  background: #ea580c;   /* 深橙 */
}

.btn5 {
  background: #fef2f2;   /* 浅粉背景 */
  color: #b91c1c;        /* 深红文字 */
  border: 1px solid #fecaca;
  border-radius: 6px;
  padding: 8px 18px;
  font-size: 14px;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.btn5:hover {
  background: #fee2e2;   /* hover 稍微加深 */
  border-color: #fca5a5;
}
.gradio-container input[type="text"] {
    padding: 10px 15px;
    border-radius: 8px;
    border: 2px solid #e0e0e0;
    transition: all 0.3s ease;
}

.gradio-container input[type="text"]:focus {
    outline: none;
    border-color: #6e48aa;
    box-shadow: 0 0 0 3px rgba(110, 72, 170, 0.2);
}

.cool-textbox textarea:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37,99,235,0.2);
  outline: none;
}

/* Textbox 的 label 样式 */
.cool-textbox label {
  font-weight: 600;        /* 半粗体 */
  font-size: 16px;         /* 稍小一点 */
  color: #374151;          /* 深灰,温和一点 */
  margin-bottom: 4px;      /* 和输入框保持间距 */
  display: inline-block;   /* 让 margin 生效 */
"""


pinyin_tool = pyPengIm()
accent_dict={'tc':'府城'}

with open("./dict_data/accent_convert/accent.yaml", 'r', encoding='utf-8') as file:
    accent_config = yaml.safe_load(file)
    for k,v in accent_config.items():
        accent_dict[k] = v['name']

reversed_accent_dict = {value: key for key, value in accent_dict.items()}

def auto_segment(input_text):
    return " ".join(jieba.cut(input_text))

def manual_segment(input_text):
    return " ".join(input_text.split(' '))

def single_char_query(input_text):

    result_dict = pinyin_tool.single_query(input_text.strip()[0])
    result = []
    for k, v in result_dict.items():
        result.append(k+':')
        result.append(str(v))
        result.append('\n')
    return "".join(result)

def number_conversion_0(input_text):    
    return utils.num_to_chinese(input_text)

def number_conversion_1(input_text):
    return utils.num_to_chinese_smart(input_text)
    
def clear_all():
    return "", "", ""

def to_ipa(input_text, location, return_seq):
    pinyin_seq = pinyin_tool.pinyin(input_text,accent=reversed_accent_dict[location])['pinyin_seq']
    pinyin_seq = pinyin_tool.to_IPA(pinyin_seq, blank=False)

    if return_seq:
        return pinyin_seq

    zi_at_pinyin_seq = zi_and_pinyin(input_text,pinyin_seq)
    return zi_at_pinyin_seq

def restrip(text):
    text=text.replace(' ','')
    text=text.replace('#','')
    return text

def zi_and_pinyin(text, pinyin_seq):
    if isinstance(pinyin_seq, list):
        ls = pinyin_seq
    else:
        ls = pinyin_seq.split(' ')
    
    result = []
    text = restrip(text)

    for i in range(len(text)):
        if text[i] == ls[i]:
            # 非法字符
            result.append(text[i])
        else:
            result.append(text[i]+'@'+ls[i])
    return " ".join(result)

def process_text(input_text, location, return_seq):
    # print(input_text)
    pinyin_seq = pinyin_tool.pinyin(input_text,accent=reversed_accent_dict[location],auto_split=False)['pinyin_seq']
    
    if return_seq:
        return pinyin_seq
    
    zi_at_pinyin_seq = zi_and_pinyin(input_text,pinyin_seq)
    return zi_at_pinyin_seq

def auto_translate(text):
    return pinyin_tool.to_oral(jieba.lcut(text))

def manual_translate(text):
    return pinyin_tool.to_oral(text.split(" "))

with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
    gr.Markdown("### 潮州话字素转音素处理工具")
    gr.Markdown("输入文本,选择地区,先进行分词,然后根据分词结果进行拼音转换")
    input_text = gr.Textbox(label="输入文本①", elem_classes="cool-textbox")
    
    with gr.Row():
        return_type = gr.Dropdown(
                choices=[False,True],
                label="是否只输出纯拼音序列",
                value=False
        )

        location = gr.Dropdown(
                choices=reversed_accent_dict.keys(),
                label="地区选择",
                value="府城"
        )

    with gr.Row():
        num_conv_btn_1 = gr.Button("阿拉伯数字转汉字(智能)", variant="btn1")
        num_conv_btn_0 = gr.Button("阿拉伯数字转汉字(直接)", variant="btn1")
        single_char_btn = gr.Button("单字查询(查询输入文本的第一个字)", variant='btn1')
        clear_btn = gr.Button("清空", variant='btn5')

    # with gr.Row():
    seg_output = gr.Textbox(label="分词结果", elem_classes="cool-textbox")
    pinyin_output = gr.Textbox(lines=5, label="拼音转换结果", elem_classes="cool-textbox")
    
    with gr.Row():
        auto_seg_btn = gr.Button("自动分词②", variant="btn3")
        manual_seg_btn = gr.Button("手动分词(自己手动打空格)②", variant="btn3")
        auto_translate_btn = gr.Button("替换成口语(自动分词)", variant="btn2")
        manual_translate_btn = gr.Button("替换成口语(手动分词)", variant="btn2")

    with gr.Row():
        ipa_btn = gr.Button("转国际音标③", variant="btn4")
        pinyin_btn = gr.Button("拼音转换③", variant="btn4")
    
    # 按钮事件绑定
    auto_seg_btn.click(auto_segment, input_text, seg_output)
    manual_seg_btn.click(manual_segment, input_text, seg_output)
    auto_translate_btn.click(auto_translate, input_text, seg_output)
    manual_translate_btn.click(manual_translate, input_text, seg_output)

    num_conv_btn_0.click(number_conversion_0, input_text, seg_output)
    num_conv_btn_1.click(number_conversion_1, input_text, seg_output)

    single_char_btn.click(single_char_query, input_text, pinyin_output)
    clear_btn.click(clear_all, None, [input_text, seg_output, pinyin_output])
    ipa_btn.click(to_ipa, [seg_output, location], pinyin_output)
    pinyin_btn.click(process_text, [seg_output, location, return_type], pinyin_output)

demo.launch()