Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13
6 changes: 6 additions & 0 deletions hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main():
print("Hello from text-elementalizer!")


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion icon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# base64 encoded image
# using command pip install psgresizer & psgresizer to convert a png to base64 encoded image
icon = b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADEklEQVR4nNXWy4tcRRQG8F93z0x6FDM+IiEmjBsRXLiRGBEdl742brLzQYgiunGpuBBc6ta1ySIqrg34B4hCEBECGo3iSo2vIGQa42QmpsfFOZUu73Snbycj4oHLvbfqPL46dc5XxX8snZZ6O7EbS5jHEH/lG7qYy/dFrOJXDKY5nrvCXLcKcBeew304m2NLWMjvjQwKt+JTvJ3vpq/WAGpZxC1Yxt05dj4DSyDX5/cqvkubbZOd2IX9Iq3f4iD25nMwxwapsyttpkrbDPwhUngOaxnoNM7k/OkcW8JJUR/dNo5bKaEv0rwsinAH9uR4P7935Nxy6vbbOG6bgaHY73Vs5rOBCzm/UY2v5/+2ZuBfk/8dgDbE1ZbcrgrAWDK5Cp2ZAXRSt6xuKAquyGYVuKm7LQAK7xdyWRAtV2TeiJZvMDortgVATxwwSziEm3FKkFAvnzP4IueexU1p02sDYpp0xAoPCxb8CY+P0XsYPwgOeD5tZirIpvQqB4/ha0E8L/hn+mv9w6nzFR6tFjAxE5OYsGxNB/vwJO7ACfyIO0U9XKqCr+IXcQTfnzanUr/43NIhk1I0J4qoj1fwquD63/B9Bl5oANhIu2VxJ1jHG3hTbF3xORVAQdrFI3g3gfwuzvySnc2GXd2i50VBXsQz+LDyOZ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1jHG3hTbF3xORVAQdrFI3g3gfwuzvySnc2GXd2i50VBXsQz+LDyOZUnyv7ux2cZ6HVxJdvb8tmN19L2pLhJ1b7HSt2St+NIOng//2eVfTiWPo40fIxt/zI4L/Z9TRTRgRy/Tlyz+lOexdSFe/Al/sTLRhnYAqBbvVdEyw3woii2rinpa8h82iykj0H6XGnEuiyFRm/EeyJtR41uNa25PaWcB0RGjqbPdzLG5ZhzlUFRXhFsd1yQSj+dXZoBxKZozaHYyuOiox6sFtWpAYxbQVG8MEFnFllMn1sWUACUnl7DR3gKT4v7/c8537W19ydJR6y+g9vS1x7RFWt1zIKoWxnci7dE754zYrvOjACKbk/s+wm8hM+rBQ0nMeFDeAIPCFqdJXgTxFl8gg/wsQYTNgHUgRaNDp1rATDAN2aopXJ8XtNZ3tbn3wwbrleX6mQSAAAAAElFTkSuQmCC"
12 changes: 7 additions & 5 deletions init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
import importlib

# Install dependencies
os.system('pip install -r requirements.txt')
os.system("pip install -r requirements.txt")

# Dynamic import pypinyin
pypinyin = importlib.import_module('pypinyin')
pypinyin = importlib.import_module("pypinyin")

# Search pypinyin dict
pypinyin_dir = Path(os.path.dirname(pypinyin.__file__))
pypinyin_dict_files = list(pypinyin_dir.glob('*_dict.json'))
pypinyin_dict_files = list(pypinyin_dir.glob("*_dict.json"))

# Generate pyinstaller spec
os.system(f'pyi-makespec main.py -w -n text-elementalizer \
os.system(
f"pyi-makespec main.py -w -n text-elementalizer \
--add-data={pypinyin_dict_files[0]}:pypinyin \
--add-data={pypinyin_dict_files[1]}:pypinyin \
--icon=res/icon.ico \
')
"
)
48 changes: 24 additions & 24 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
from elementalizer import elementalizer
import icon


# 事件-更新转换结果
def txt_result_update(source, ignore_tone = True):
def txt_result_update(source, ignore_tone=True):
if ignore_tone:
result = elementalizer(source, ignore_tone = True)
result = elementalizer(source, ignore_tone=True)
else:
result = elementalizer(source, ignore_tone = False)
result = elementalizer(source, ignore_tone=False)

window['txt_result'].update(result)
window["txt_result"].update(result)


# 界面布局
layout = [
[psg.Text('请输入需要转换的文本:'),
psg.Input(key = 'txt_source', expand_x = True, enable_events = True)],

[psg.Checkbox('模糊音调', key = 'chk_ignore_tone', default = False, enable_events = True)],

[psg.Text('转换结果:'),
psg.Text(key = 'txt_result'),
psg.Button('重新生成', key = 'btn_regenerate'),
psg.Button('复制结果', key = 'btn_copy'),
psg.Text(key = 'txt_copy_hint', visible = False)]
[psg.Text("请输入需要转换的文本:"), psg.Input(key="txt_source", expand_x=True, enable_events=True)],
[psg.Checkbox("模糊音调", key="chk_ignore_tone", default=False, enable_events=True)],
[
psg.Text("转换结果:"),
psg.Text(key="txt_result"),
psg.Button("重新生成", key="btn_regenerate"),
psg.Button("复制结果", key="btn_copy"),
psg.Text(key="txt_copy_hint", visible=False),
],
]

window = psg.Window('化学名称文本转换器', layout, icon = icon.icon)
last_source = ''
window = psg.Window("化学名称文本转换器", layout, icon=icon.icon)
last_source = ""
while True:
event, values = window.read()

Expand All @@ -36,17 +36,17 @@ def txt_result_update(source, ignore_tone = True):
break

# 复制结果
if event == 'btn_copy':
psg.clipboard_set(window['txt_result'].get())
window['txt_copy_hint'].update('已复制到剪贴板', visible = True)
if event == "btn_copy":
psg.clipboard_set(window["txt_result"].get())
window["txt_copy_hint"].update("已复制到剪贴板", visible=True)

# 转换文本
if event in ['txt_source']:
current_source = values['txt_source']
if event in ["txt_source"]:
current_source = values["txt_source"]
if current_source != last_source:
txt_result_update(current_source, ignore_tone = values['chk_ignore_tone'])
txt_result_update(current_source, ignore_tone=values["chk_ignore_tone"])
last_source = current_source
elif event in ['chk_ignore_tone', 'btn_regenerate']:
txt_result_update(current_source, ignore_tone = values['chk_ignore_tone'])
elif event in ["chk_ignore_tone", "btn_regenerate"]:
txt_result_update(current_source, ignore_tone=values["chk_ignore_tone"])

window.close()
89 changes: 0 additions & 89 deletions name.py

This file was deleted.

19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[project]
name = "text-elementalizer"
version = "0.2.0"
description = "Replace the appropriate text with the name of the chemical material based on pinyin"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"pypinyin==0.52.0",
]

[dependency-groups]
dev = [
"pyinstaller==6.10.0",
"ruff==0.9.2",
"maliang==3.0.0rc6",
]

[tool.ruff]
line-length = 120
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

13 changes: 7 additions & 6 deletions elementalizer.py → src/elementalizer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from pypinyin import pinyin, lazy_pinyin, Style
from pypinyin import lazy_pinyin, Style
import name
import random

def elementalizer(source, ignore_tone = True):

def elementalizer(source, ignore_tone=True):
"""
基于汉语拼音,将字符串中的汉字转换为对应的化学词汇

Expand All @@ -18,8 +19,8 @@ def elementalizer(source, ignore_tone = True):
source_list = list(source)

# 生成源字符串的拼音列表
source_pinyin_tone = lazy_pinyin(source_list, style = Style.TONE)
source_pinyin_normal = lazy_pinyin(source_list, style = Style.NORMAL)
source_pinyin_tone = lazy_pinyin(source_list, style=Style.TONE)
source_pinyin_normal = lazy_pinyin(source_list, style=Style.NORMAL)

# 生成翻译字典
translate_map = dict()
Expand All @@ -36,7 +37,7 @@ def elementalizer(source, ignore_tone = True):
else:
for i in range(len(source_pinyin_tone)):
source_char = source_list[i]
pinyin_tone = source_pinyin_tone[i]
pinyin_tone = source_pinyin_tone[i]

if source_char in name.exclude_list:
continue
Expand All @@ -48,4 +49,4 @@ def elementalizer(source, ignore_tone = True):

# 翻译源字符串
source_translated = source.translate(translation)
return(source_translated)
return source_translated
30 changes: 30 additions & 0 deletions src/gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import maliang

from elementalizer import elementalizer as ele


def update_result():
if chk_ignore_tone.state == "normal-on":
txt_result.texts[0].text = ele(ibx_source.texts[0].text, ignore_tone=True)
else:
txt_result.texts[0].text = ele(ibx_source.texts[0].text, ignore_tone=False)


root = maliang.Tk(title="文本-化学名称-转换器")
root.center()

cv = maliang.Canvas(auto_zoom=True, keep_ratio="min", free_anchor=True)
cv.place(width=1280, height=720, x=640, y=360, anchor="center")

maliang.Text(cv, (20, 50), text="请输入需要转换的文本:", fontsize=24, anchor="w")
ibx_source = maliang.InputBox(cv, (300, 50), size=(600, 40), fontsize=24, anchor="w")
chk_ignore_tone = maliang.CheckBox(cv, (920, 50), anchor="w", default=True)
maliang.Text(cv, (960, 50), text="忽略音调", fontsize=24, anchor="w")

maliang.Text(cv, (20, 100), text="转换结果:", fontsize=24, anchor="w")
txt_result = maliang.Text(cv, (300, 100), fontsize=24, anchor="w")
btn_copy = maliang.Button(cv, (920, 100), text="复制", fontsize=24, anchor="w", command=update_result)
maliang.Text(cv, (1000, 100), text="已复制到剪贴板", fontsize=24, anchor="w")


root.mainloop()
Loading